mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-14 15:06:05 +09:00
edit.js: can read existing files; change on tty key read that arrow keys are now distinguished from ctrl-S or something
This commit is contained in:
6
assets/disk0/home/keypolltest.js
Normal file
6
assets/disk0/home/keypolltest.js
Normal file
@@ -0,0 +1,6 @@
|
||||
println("Hit Ctrl-C or Ctrl-D to exit");
|
||||
while (true) {
|
||||
let keys = con.poll_keys()
|
||||
println(keys);
|
||||
if (keys[1] == 129 && (keys[0] == 31 || keys[0] == 32)) break;
|
||||
}
|
||||
@@ -98,6 +98,7 @@ shell.replaceVarCall = function(value) {
|
||||
return replaceMap.map(it => (it.r) ? _TVDOS.variables[it.s] : it.s).join('');
|
||||
}
|
||||
shell.getPwd = function() { return shell_pwd; }
|
||||
shell.getPwdString = function() { return "\\" + (shell_pwd.concat([""])).join("\\"); }
|
||||
shell.getCurrentDrive = function() { return CURRENT_DRIVE; }
|
||||
// example input: echo "the string" > subdir\test.txt
|
||||
shell.parse = function(input) {
|
||||
@@ -422,12 +423,12 @@ if (goInteractive) {
|
||||
print(s);
|
||||
}
|
||||
// backspace
|
||||
else if (key === 8 && cmdbuf.length > 0) {
|
||||
else if (key === con.KEY_BACKSPACE && cmdbuf.length > 0) {
|
||||
cmdbuf = cmdbuf.substring(0, cmdbuf.length - 1);
|
||||
print(String.fromCharCode(key));
|
||||
}
|
||||
// enter
|
||||
else if (key === 10 || key === 13) {
|
||||
else if (key === 10 || key === con.KEY_RETURN) {
|
||||
println();
|
||||
try {
|
||||
errorlevel = 0; // reset the number
|
||||
@@ -451,7 +452,7 @@ if (goInteractive) {
|
||||
}
|
||||
}
|
||||
// up arrow
|
||||
else if (key === 19 && cmdHistory.length > 0 && cmdHistoryScroll < cmdHistory.length) {
|
||||
else if (key === con.KEY_UP && cmdHistory.length > 0 && cmdHistoryScroll < cmdHistory.length) {
|
||||
cmdHistoryScroll += 1;
|
||||
|
||||
// back the cursor in order to type new cmd
|
||||
@@ -463,7 +464,7 @@ if (goInteractive) {
|
||||
|
||||
}
|
||||
// down arrow
|
||||
else if (key === 20) {
|
||||
else if (key === con.KEY_DOWN) {
|
||||
if (cmdHistoryScroll > 0) {
|
||||
// back the cursor in order to type new cmd
|
||||
var x = 0;
|
||||
|
||||
@@ -7,15 +7,24 @@ else {
|
||||
println("File to edit?");
|
||||
filename = read();
|
||||
}
|
||||
let driveLetter = _G.shell.getCurrentDrive();
|
||||
let filePath = _G.shell.getPwdString() + filename;
|
||||
|
||||
let scroll = 0;
|
||||
//let textbuffer = ["The quick brown fox","jumps over a lazy dog 12345678901234567890", "Pack my box with", "five dozen liquor jugs", "The quick brown fox","jumps over a lazy dog 12345678901234567890", "Pack my box with", "five dozen liquor jugs"];
|
||||
let textbuffer = [""];
|
||||
let cursorRow = 0;
|
||||
let cursorCol = 0;
|
||||
let exit = false;
|
||||
let scene = -1; // -1: main, 0: filemenu, 1: editmenu , ...
|
||||
|
||||
// load existing file if it's there
|
||||
let editingExistingFile = filesystem.open(driveLetter, filePath, "R");
|
||||
if (editingExistingFile) {
|
||||
textbuffer = filesystem.readAll(driveLetter).split("\n");
|
||||
serial.println(textbuffer);
|
||||
}
|
||||
|
||||
|
||||
let windowWidth = con.getmaxyx()[1];
|
||||
let windowHeight = con.getmaxyx()[0];
|
||||
let paintWidth = windowWidth - 4;
|
||||
@@ -189,8 +198,7 @@ function gotoText() {
|
||||
// FUNCTIONING FUNCTIONS (LOL) //
|
||||
|
||||
function writeout() {
|
||||
var driveLetter = _G.shell.getCurrentDrive();
|
||||
filesystem.open(driveLetter, filename, "W");
|
||||
filesystem.open(driveLetter, filePath, "W");
|
||||
filesystem.write(driveLetter, textbuffer.join('\n'));
|
||||
}
|
||||
|
||||
@@ -236,15 +244,19 @@ function appendLine() {
|
||||
gotoText();
|
||||
}
|
||||
|
||||
reset_status();
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
drawMain();
|
||||
drawTextbuffer();
|
||||
|
||||
let bulletinShown = false;
|
||||
|
||||
// TODO for existing file, show [ Read $n Lines ]
|
||||
// TODO for new file, show [ New File ]
|
||||
displayBulletin(`Wrote ${textbuffer.length} Lines`);
|
||||
// show "welcome" message
|
||||
if (!editingExistingFile)
|
||||
displayBulletin(`New File`);
|
||||
else
|
||||
displayBulletin(`Read ${textbuffer.length} Lines`);
|
||||
|
||||
|
||||
while (!exit) {
|
||||
let key = con.getch();
|
||||
@@ -257,9 +269,9 @@ while (!exit) {
|
||||
writeout();
|
||||
displayBulletin(`Wrote ${textbuffer.length} lines`);
|
||||
}
|
||||
else if (key == 8) { // Bksp
|
||||
else if (key == con.KEY_BACKSPACE) { // Bksp
|
||||
}
|
||||
else if (key == 13) { // Return / ^M
|
||||
else if (key == con.KEY_RETURN) { // Return
|
||||
appendLine();
|
||||
}
|
||||
else if (key >= 32) { // printables (excludes \n)
|
||||
|
||||
@@ -88,24 +88,27 @@ con.move(termH + 1, 2);
|
||||
while (true) {
|
||||
// read a key
|
||||
key = con.getch();
|
||||
|
||||
//serial.println("key = "+key);
|
||||
|
||||
// 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) {
|
||||
/*up*/else if (key == con.KEY_UP) {
|
||||
scroll -= scrollSize;
|
||||
if (scroll < 0) scroll = 0;
|
||||
repaint();
|
||||
}
|
||||
/*down*/else if (key == 20) {
|
||||
/*down*/else if (key == con.KEY_DOWN) {
|
||||
scroll += scrollSize;
|
||||
if (scroll > lineToBytes.length - termH) scroll = lineToBytes.length - termH;
|
||||
repaint();
|
||||
}
|
||||
/*left*/else if (key == 21 && pan > 0) {
|
||||
/*left*/else if (key == con.KEY_LEFT && pan > 0) {
|
||||
pan -= panSize;
|
||||
repaint();
|
||||
}
|
||||
/*right*/else if (key == 22 && pan < maxPan - termW) {
|
||||
/*right*/else if (key == con.KEY_RIGHT && pan < maxPan - termW) {
|
||||
pan += panSize;
|
||||
repaint();
|
||||
}
|
||||
@@ -113,7 +116,7 @@ while (true) {
|
||||
print(String.fromCharCode(key));
|
||||
numbuf = (numbuf * 10) + (key - 48);
|
||||
}
|
||||
/*bksp*/else if (key == 8) {
|
||||
/*bksp*/else if (key == con.KEY_BACKSPACE) {
|
||||
if (numbuf > 0) print(String.fromCharCode(key));
|
||||
numbuf = (numbuf / 10)|0;
|
||||
}
|
||||
@@ -129,14 +132,14 @@ while (true) {
|
||||
}
|
||||
|
||||
|
||||
if (!(key >= 48 && key <= 57 || key == 8)) {
|
||||
if (!(key >= 48 && key <= 57 || key == con.KEY_BACKSPACE)) {
|
||||
resetKeyReadStatus();
|
||||
con.move(termH + 1, 1);
|
||||
print(":"+" ".repeat(termW - 2));
|
||||
con.move(termH + 1, 2);
|
||||
}
|
||||
|
||||
serial.println("numbuf = "+numbuf);
|
||||
//serial.println("numbuf = "+numbuf);
|
||||
}
|
||||
|
||||
con.move(termH + 1, 1);
|
||||
|
||||
Reference in New Issue
Block a user