command history navigation sorta works

This commit is contained in:
minjaesong
2020-05-19 11:54:13 +09:00
parent 619257ecf2
commit a1e053c53e

View File

@@ -1,8 +1,14 @@
function readConsoleInput() { println("JS Console");
var cmdHistory = []; // zeroth element is the oldest
var cmdHistoryScroll = 0; // 0 for outside-of-buffer, 1 for most recent
while (true) {
print("JS> ");
var cmdbuf = ""; var cmdbuf = "";
var key = -1;
while (key != 10 && key != 13) { while (true) {
key = vm.readKey(); var key = vm.readKey();
// printable chars // printable chars
if (key >= 32 && key <= 126) { if (key >= 32 && key <= 126) {
var s = String.fromCharCode(key); var s = String.fromCharCode(key);
@@ -10,35 +16,54 @@ function readConsoleInput() {
print(s); print(s);
} }
// backspace // backspace
else if (key == 8 && cmdbuf.length > 0) { else if (key === 8 && cmdbuf.length > 0) {
cmdbuf = cmdbuf.substring(0, cmdbuf.length - 1); cmdbuf = cmdbuf.substring(0, cmdbuf.length - 1);
print(String.fromCharCode(key)); print(String.fromCharCode(key));
} }
// up down key // enter
else if (key >= 19 && key <= 20) { else if (key === 10 || key === 13) {
return key; println();
} try {
// left right key println(eval(cmdbuf));
else if (key >= 19 && key <= 20) { }
// catch (e) {
} println(e);
} }
return cmdbuf; finally {
} if (cmdbuf.trim().length > 0)
cmdHistory.push(cmdbuf);
println("JS Console"); cmdHistoryScroll = 0;
while (true) { break;
print("JS> "); }
var cmdbuf = readConsoleInput();
if (typeof cmdbuf == "string") {
println();
try {
println(eval(cmdbuf));
} }
catch (e) { // up arrow
println(e); else if (key === 19 && cmdHistory.length > 0 && cmdHistoryScroll < cmdHistory.length) {
cmdHistoryScroll += 1;
// back the cursor in order to type new cmd
for (xx = 0; xx < cmdbuf.length; xx++) print(String.fromCharCode(8));
cmdbuf = cmdHistory[cmdHistory.length - cmdHistoryScroll];
// re-type the new command
print(cmdbuf);
}
// down arrow
else if (key === 20) {
if (cmdHistoryScroll > 0) {
// back the cursor in order to type new cmd
for (xx = 0; xx < cmdbuf.length; xx++) print(String.fromCharCode(8));
cmdbuf = cmdHistory[cmdHistory.length - cmdHistoryScroll];
// re-type the new command
print(cmdbuf);
cmdHistoryScroll -= 1;
}
else {
// back the cursor in order to type new cmd
for (xx = 0; xx < cmdbuf.length; xx++) print(String.fromCharCode(8));
cmdbuf = "";
}
} }
} }
} }