rudimentary and perhaps broken LESS impl

This commit is contained in:
minjaesong
2020-11-13 14:51:51 +09:00
parent d0f46b89d7
commit 3a26e3dde7
5 changed files with 103 additions and 0 deletions

View File

@@ -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"));
};

6
assets/keytest.js Normal file
View File

@@ -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;
}

View File

@@ -410,6 +410,7 @@ if (goInteractive) {
cmdHistory.push(cmdbuf);
cmdHistoryScroll = 0;
con.curs_set(1);
break;
}

90
assets/tvdos/bin/less.js Normal file
View File

@@ -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 <filename>");
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;

View File

@@ -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()