mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-13 12:04:06 +09:00
Working line-of-text and single key input
Former-commit-id: 339650979ff2cb5ec18b52a9f3f38b281c7862c9 Former-commit-id: d5ebd860afc8d569ba9ab741b6ca7872380af949
This commit is contained in:
@@ -29,7 +29,7 @@ open class SimpleTextTerminal(
|
||||
Color(0x00, 0x00, 0x00), // 0 black
|
||||
Color(0xff, 0xff, 0xff), // 1 white
|
||||
Color(0x55, 0x55, 0x55), // 2 dim grey
|
||||
Color(0xaa, 0xaa, 0xaa), // 3 light grey
|
||||
Color(0xaa, 0xaa, 0xaa), // 3 bright grey
|
||||
|
||||
Color(0xff, 0xff, 0x00), // 4 yellow
|
||||
Color(0xff, 0x66, 0x00), // 5 orange
|
||||
@@ -39,7 +39,7 @@ open class SimpleTextTerminal(
|
||||
Color(0x33, 0x00, 0x99), // 8 purple
|
||||
Color(0x00, 0x00, 0xcc), // 9 blue
|
||||
Color(0x00, 0x99, 0xff), //10 cyan
|
||||
Color(0x66, 0xff, 0x33), //11 lime
|
||||
Color(0x55, 0xff, 0x00), //11 lime
|
||||
|
||||
Color(0x00, 0xaa, 0x00), //12 green
|
||||
Color(0x00, 0x66, 0x00), //13 dark green
|
||||
@@ -83,7 +83,7 @@ open class SimpleTextTerminal(
|
||||
override val displayH = fontH * height
|
||||
|
||||
|
||||
private val TABSIZE = 4
|
||||
var TABSIZE = 4
|
||||
|
||||
private var cursorBlinkTimer = 0
|
||||
private val cursorBlinkLen = 250
|
||||
@@ -185,6 +185,7 @@ open class SimpleTextTerminal(
|
||||
|
||||
}
|
||||
|
||||
/** Unlike lua function, this one in Zero-based. */
|
||||
override fun setCursor(x: Int, y: Int) {
|
||||
cursorX = x
|
||||
cursorY = y
|
||||
@@ -387,18 +388,31 @@ open class SimpleTextTerminal(
|
||||
/**
|
||||
* Technically, this is different from Java's InputStream
|
||||
*/
|
||||
fun openInput() {
|
||||
override fun openInput() {
|
||||
lastStreamInput = null
|
||||
lastKeyPress = null
|
||||
inputOpen = true
|
||||
if (DEBUG) println("[SimpleTextTerminal] openInput()")
|
||||
}
|
||||
|
||||
fun closeInput(): String {
|
||||
override fun closeInputKey(keyFromUI: Int): Int {
|
||||
inputOpen = false
|
||||
val ret = sb.toString()
|
||||
lastKeyPress = keyFromUI
|
||||
|
||||
if (DEBUG) println("[SimpleTextTerminal] closeInputKey(), $keyFromUI")
|
||||
return keyFromUI
|
||||
}
|
||||
|
||||
override var lastStreamInput: String? = null
|
||||
override var lastKeyPress: Int? = null
|
||||
|
||||
override fun closeInputString(): String {
|
||||
inputOpen = false
|
||||
lastStreamInput = sb.toString()
|
||||
sb = StringBuilder()
|
||||
|
||||
if (DEBUG) println("[SimpleTextTerminal] closeInput(), $ret")
|
||||
return ret
|
||||
if (DEBUG) println("[SimpleTextTerminal] closeInputString(), $lastStreamInput")
|
||||
return lastStreamInput!!
|
||||
}
|
||||
|
||||
override fun keyPressed(key: Int, c: Char) {
|
||||
|
||||
@@ -34,5 +34,12 @@ interface Teletype {
|
||||
fun newLine()
|
||||
fun scroll(amount: Int = 1)
|
||||
|
||||
fun openInput()
|
||||
fun closeInputKey(keyFromUI: Int): Int
|
||||
fun closeInputString(): String
|
||||
|
||||
fun bell(pattern: String = ".")
|
||||
|
||||
var lastStreamInput: String?
|
||||
var lastKeyPress: Int?
|
||||
}
|
||||
@@ -134,18 +134,31 @@ class TeletypeTerminal : Teletype {
|
||||
/**
|
||||
* Technically, this is different from Java's InputStream
|
||||
*/
|
||||
fun openInput() {
|
||||
override fun openInput() {
|
||||
lastStreamInput = null
|
||||
lastKeyPress = null
|
||||
inputOpen = true
|
||||
if (DEBUG) println("[SimpleTextTerminal] openInput()")
|
||||
if (DEBUG) println("[TeletypeTerminal] openInput()")
|
||||
}
|
||||
|
||||
fun closeInput(): String {
|
||||
override fun closeInputKey(keyFromUI: Int): Int {
|
||||
inputOpen = false
|
||||
val ret = sb.toString()
|
||||
lastKeyPress = keyFromUI
|
||||
|
||||
if (DEBUG) println("[TeletypeTerminal] closeInputKey(), $keyFromUI")
|
||||
return keyFromUI
|
||||
}
|
||||
|
||||
override var lastStreamInput: String? = null
|
||||
override var lastKeyPress: Int? = null
|
||||
|
||||
override fun closeInputString(): String {
|
||||
inputOpen = false
|
||||
lastStreamInput = sb.toString()
|
||||
sb = StringBuilder()
|
||||
|
||||
if (DEBUG) println("[SimpleTextTerminal] closeInput(), $ret")
|
||||
return ret
|
||||
if (DEBUG) println("[TeletypeTerminal] closeInputString(), $lastStreamInput")
|
||||
return lastStreamInput!!
|
||||
}
|
||||
|
||||
override fun keyPressed(key: Int, c: Char) {
|
||||
|
||||
@@ -1,170 +0,0 @@
|
||||
package net.torvald.terrarum.virtualcomputer.terminal;
|
||||
|
||||
import li.cil.repack.com.naef.jnlua.LuaException;
|
||||
import li.cil.repack.com.naef.jnlua.LuaRuntimeException;
|
||||
import li.cil.repack.com.naef.jnlua.LuaState;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-09-10.
|
||||
*/
|
||||
public class TerminalLuaConsole {
|
||||
// -- Static
|
||||
private static final String[] EMPTY_ARGS = new String[0];
|
||||
|
||||
/**
|
||||
* Main routine.
|
||||
*
|
||||
* @param args
|
||||
* the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
LuaConsole luaConsole = new LuaConsole(args);
|
||||
luaConsole.run();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private PrintStream out;
|
||||
|
||||
// -- State
|
||||
private LuaState luaState;
|
||||
|
||||
// -- Construction
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*/
|
||||
public TerminalLuaConsole(Terminal term) {
|
||||
this(EMPTY_ARGS, term);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with the specified command line arguments. The
|
||||
* arguments are passed to Lua as the <code>argv</code> global variable.
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public TerminalLuaConsole(String[] args, Terminal term) {
|
||||
out = new TerminalPrintStream(term);
|
||||
|
||||
luaState = new LuaState();
|
||||
|
||||
// Process arguments
|
||||
luaState.newTable(args.length, 0);
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
luaState.pushString(args[i]);
|
||||
luaState.rawSet(-2, i + 1);
|
||||
}
|
||||
luaState.setGlobal("argv");
|
||||
|
||||
// Open standard libraries
|
||||
luaState.openLibs();
|
||||
|
||||
// Set buffer mode
|
||||
luaState.load("io.stdout:setvbuf(\"no\")", "=consoleInitStdout");
|
||||
luaState.call(0, 0);
|
||||
luaState.load("io.stderr:setvbuf(\"no\")", "=consoleInitStderr");
|
||||
luaState.call(0, 0);
|
||||
}
|
||||
|
||||
// -- Properties
|
||||
/**
|
||||
* Returns the Lua state of this console.
|
||||
*
|
||||
* @return the Lua state
|
||||
*/
|
||||
public LuaState getLuaState() {
|
||||
return luaState;
|
||||
}
|
||||
|
||||
// -- Operations
|
||||
/**
|
||||
* Runs the console.
|
||||
*/
|
||||
public void run() {
|
||||
// Banner
|
||||
out.println(String.format("JNLua %s Console using Lua %s.",
|
||||
LuaState.VERSION, LuaState.LUA_VERSION));
|
||||
out.print("Type 'go' on an empty line to evaluate a chunk. ");
|
||||
out.println("Type =<expression> to print an expression.");
|
||||
|
||||
// Prepare reader
|
||||
BufferedReader bufferedReader = new BufferedReader(
|
||||
new InputStreamReader(System.in));
|
||||
try {
|
||||
// Process chunks
|
||||
chunk: while (true) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
OutputStreamWriter outWriter = new OutputStreamWriter(out,
|
||||
"UTF-8");
|
||||
boolean firstLine = true;
|
||||
|
||||
// Process lines
|
||||
while (true) {
|
||||
String line = bufferedReader.readLine();
|
||||
if (line == null) {
|
||||
break chunk;
|
||||
}
|
||||
if (line.equals("go")) {
|
||||
outWriter.flush();
|
||||
InputStream in = new ByteArrayInputStream(out
|
||||
.toByteArray());
|
||||
runChunk(in);
|
||||
continue chunk;
|
||||
}
|
||||
if (firstLine && line.startsWith("=")) {
|
||||
outWriter.write("return " + line.substring(1));
|
||||
outWriter.flush();
|
||||
InputStream in = new ByteArrayInputStream(out
|
||||
.toByteArray());
|
||||
runChunk(in);
|
||||
continue chunk;
|
||||
}
|
||||
outWriter.write(line);
|
||||
outWriter.write('\n');
|
||||
firstLine = false;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
out.print("IO error: ");
|
||||
out.print(e.getMessage());
|
||||
out.println();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a chunk of Lua code from an input stream.
|
||||
*/
|
||||
protected void runChunk(InputStream in) throws IOException {
|
||||
try {
|
||||
long start = System.nanoTime();
|
||||
luaState.setTop(0);
|
||||
luaState.load(in, "=console", "t");
|
||||
luaState.call(0, LuaState.MULTRET);
|
||||
long stop = System.nanoTime();
|
||||
for (int i = 1; i <= luaState.getTop(); i++) {
|
||||
if (i > 1) {
|
||||
out.print(", ");
|
||||
}
|
||||
switch (luaState.type(i)) {
|
||||
case BOOLEAN:
|
||||
out.print(Boolean.valueOf(luaState.toBoolean(i)));
|
||||
break;
|
||||
case NUMBER:
|
||||
case STRING:
|
||||
out.print(luaState.toString(i));
|
||||
break;
|
||||
default:
|
||||
out.print(luaState.typeName(i));
|
||||
}
|
||||
}
|
||||
out.print("\t#msec=");
|
||||
out.print(String.format("%.3f", (stop - start) / 1000000.0));
|
||||
out.println();
|
||||
} catch (LuaRuntimeException e) {
|
||||
e.printLuaStackTrace();
|
||||
} catch (LuaException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user