diff --git a/assets/tvdos/command.js b/assets/tvdos/command.js index 95a58d2..fbcb7d1 100644 --- a/assets/tvdos/command.js +++ b/assets/tvdos/command.js @@ -22,9 +22,74 @@ println("Starting TVDOS..."); greet(); +let cmdHistory = []; // zeroth element is the oldest +let cmdHistoryScroll = 0; // 0 for outside-of-buffer, 1 for most recent while (true) { print(get_prompt_text()); - var s = read(); - println(); - println("String read: " + s + "@"); + + let cmdbuf = ""; + + while (true) { + let key = con.getch(); + + // printable chars + if (key >= 32 && key <= 126) { + let s = String.fromCharCode(key); + cmdbuf += s; + print(s); + } + // backspace + else if (key === 8 && cmdbuf.length > 0) { + cmdbuf = cmdbuf.substring(0, cmdbuf.length - 1); + print(String.fromCharCode(key)); + } + // enter + else if (key === 10 || key === 13) { + println(); + try { + println("You entered: " + cmdbuf); + } + catch (e) { + println(e); + } + finally { + if (cmdbuf.trim().length > 0) + cmdHistory.push(cmdbuf); + + cmdHistoryScroll = 0; + break; + } + } + // up arrow + else if (key === 19 && cmdHistory.length > 0 && cmdHistoryScroll < cmdHistory.length) { + cmdHistoryScroll += 1; + + // back the cursor in order to type new cmd + let x = 0; + for (x = 0; x < cmdbuf.length; x++) 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 + let x = 0; + for (x = 0; x < cmdbuf.length; x++) 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 + let x = 0; + for (x = 0; x < cmdbuf.length; x++) print(String.fromCharCode(8)); + cmdbuf = ""; + } + } + } } \ No newline at end of file