less:u d commands, new app 'hexdump'

This commit is contained in:
minjaesong
2020-12-02 10:27:58 +09:00
parent 7786044d98
commit ceda5a4479
2 changed files with 64 additions and 6 deletions

View File

@@ -0,0 +1,20 @@
if (exec_args[1] === undefined) {
printerrln("Usage: hexdump <file>")
return 1;
}
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 fileContent = filesystem.readAll(_G.shell.getCurrentDrive());
let visible = "";
for (let k = 0; k < fileContent.length; k++) {
if (k > 0 && k % 16 == 0) visible += "\n";
visible += `${fileContent.charCodeAt(k).toString(16).toUpperCase().padStart(2, '0')} `;
}
println(visible);
return 0;

View File

@@ -29,6 +29,7 @@ let buf = "";
let fileContent = filesystem.readAll(_G.shell.getCurrentDrive());
let key = -1;
let panSize = termW >> 1;
let scrollSize = termH >> 3;
// initialise some helper variables
let lineToBytes = [0];
@@ -50,6 +51,12 @@ let cy = 1;
let cx = 1;
let char = -1;
let numbuf = 0;
let resetKeyReadStatus = function() {
numbuf = 0;
}
let repaint = function() {
con.move(1,1); con.clear();
@@ -75,20 +82,23 @@ let repaint = function() {
}
repaint();
con.move(termH + 1, 1);
print(":"+" ".repeat(termW - 2));
con.move(termH + 1, 2);
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;
/*up*/else if (key == 19) {
scroll -= scrollSize;
if (scroll < 0) scroll = 0;
repaint();
}
/*down*/else if (key == 20 && scroll < lineToBytes.length - termH) {
scroll += 1;
/*down*/else if (key == 20) {
scroll += scrollSize;
if (scroll > lineToBytes.length - termH) scroll = lineToBytes.length - termH;
repaint();
}
/*left*/else if (key == 21 && pan > 0) {
@@ -99,6 +109,34 @@ while (true) {
pan += panSize;
repaint();
}
/*0-9*/else if (key >= 48 && key <= 57) {
print(String.fromCharCode(key));
numbuf = (numbuf * 10) + (key - 48);
}
/*bksp*/else if (key == 8) {
if (numbuf > 0) print(String.fromCharCode(key));
numbuf = (numbuf / 10)|0;
}
/*u*/else if (key == 117) {
scroll -= numbuf;
if (scroll < 0) scroll = 0;
repaint();
}
/*d*/else if (key == 100) {
scroll += numbuf;
if (scroll > lineToBytes.length - termH) scroll = lineToBytes.length - termH;
repaint();
}
if (!(key >= 48 && key <= 57 || key == 8)) {
resetKeyReadStatus();
con.move(termH + 1, 1);
print(":"+" ".repeat(termW - 2));
con.move(termH + 1, 2);
}
serial.println("numbuf = "+numbuf);
}
con.move(termH + 1, 1);