diff --git a/assets/JS_INIT.js b/assets/JS_INIT.js index 3bb6879..49827c2 100644 --- a/assets/JS_INIT.js +++ b/assets/JS_INIT.js @@ -395,6 +395,7 @@ con.color_pair = function(fore, back) { // 0..255 con.clear = function() { print(String.fromCharCode(27,91)+"2J"); }; +// @params arg 0 to hide, nonzero to show con.curs_set = function(arg) { print(String.fromCharCode(27,91)+"?25"+(((arg|0) == 0) ? "l" : "h")); }; diff --git a/assets/keytest.js b/assets/keytest.js new file mode 100644 index 0000000..57dbc08 --- /dev/null +++ b/assets/keytest.js @@ -0,0 +1,6 @@ +println("Hit Ctrl-C or Ctrl-D to exit"); +while (true) { + let key = con.getch() + println(key); + if (key == 3 || key == 4) break; +} \ No newline at end of file diff --git a/assets/tvdos/bin/command.js b/assets/tvdos/bin/command.js index 56dba12..3d760d7 100644 --- a/assets/tvdos/bin/command.js +++ b/assets/tvdos/bin/command.js @@ -410,6 +410,7 @@ if (goInteractive) { cmdHistory.push(cmdbuf); cmdHistoryScroll = 0; + con.curs_set(1); break; } diff --git a/assets/tvdos/bin/less.js b/assets/tvdos/bin/less.js new file mode 100644 index 0000000..2690ac6 --- /dev/null +++ b/assets/tvdos/bin/less.js @@ -0,0 +1,90 @@ +if (exec_args[1] === undefined) { + println('Missing filename ("less -?" for help)'); + return 0; +} + +/*let help = "\n +SUMMARY OF COMMANDS\n +\n + h H Display this help\n + q Q Exit +\n"*/ + +if (exec_args[1].startsWith("-?")) { + println("less "); + return 0; +} + +let fileOpened = filesystem.open(_G.shell.getCurrentDrive(), _G.shell.resolvePathInput(exec_args[1]).string, "R"); +if (!fileOpened) { + printerrln(_G.shell.resolvePathInput(exec_args[1]).string+": cannot open"); + return 1; +} + +let scroll = 0; +let termW = con.getmaxyx()[1]; +let termH = con.getmaxyx()[0] - 1; +let bufsize = termW * termH; +let buf = ""; +let fileContent = filesystem.readAll(_G.shell.getCurrentDrive()); +let key = -1; + +// initialise some helper variables +let lineToBytes = [0]; +for (let i = 0; i < fileContent.length; i++) { + let char = fileContent.charCodeAt(i); + if (char == 10 || char == 13) { + lineToBytes.push(i + 1); + } +} + +serial.println(lineToBytes); + +let startAddr = -1; +let paintCur = 0; +let cy = 1; +let cx = 1; +let char = -1; + +let repaint = function() { + con.move(1,1); con.clear(); + + startAddr = lineToBytes[scroll]; + cy = 1; cx = 1; paintCur = 0; + while (cy <= termH && cx <= termW) { + char = fileContent.charCodeAt(startAddr + paintCur); + if (cy <= termH) { + con.move(cy, cx); + if (char != 10 && char != 13) + con.addch(char); + cx += 1; + } + if (char == 10 || char == 13) { + cy += 1; + cx = 1; + } + paintCur += 1; + } +} + +repaint(); +while (true) { + // read a key + con.mvaddch(termH + 1,1,58); + con.move(termH + 1, 2); + key = con.getch(); + // do something with key read + /*Q*/if (key == 113 || key == 81) break; + /*R*/else if (key == 114 || key == 82) repaint(); + /*up*/else if (key == 19 && scroll > 0) { + scroll -= 1; + repaint(); + } + /*down*/else if (key == 20 && scroll < lineToBytes.length - 1) { + scroll += 1; + repaint(); + } +} + +con.move(termH + 1, 1); +return 0; diff --git a/src/net/torvald/tsvm/VMJSR223Delegate.kt b/src/net/torvald/tsvm/VMJSR223Delegate.kt index 2bab09a..71ffea7 100644 --- a/src/net/torvald/tsvm/VMJSR223Delegate.kt +++ b/src/net/torvald/tsvm/VMJSR223Delegate.kt @@ -41,6 +41,11 @@ class VMJSR223Delegate(val vm: VM) { } fun println() = print('\n') + /** + * @return key being hit, of which: + * a-zA-Z1-9: corresponding ASCII code + * ^A-^Z: 1 through 26 + */ fun readKey(): Int { val inputStream = vm.getInputStream() var key: Int = inputStream.read()