almost a real repl
This commit is contained in:
38
repl.ts
38
repl.ts
@ -1,11 +1,22 @@
|
||||
import faux from "./builtins.ts";
|
||||
import { getKeysPressed, shiftKeyDown, shiftMap, K } from "./keyboard.ts";
|
||||
import { font } from "./font.ts";
|
||||
import { runCode } from "./runcode.ts";
|
||||
|
||||
const lineHeight = 6;
|
||||
|
||||
let maxLineLen = 0;
|
||||
let line = "";
|
||||
let index = 0;
|
||||
let y = 0;
|
||||
let result = "";
|
||||
|
||||
const update = () => {
|
||||
if (result) {
|
||||
console.log("result:", result);
|
||||
y += lineHeight; // TODO: multiply if multiline
|
||||
result = "";
|
||||
}
|
||||
for (const key of getKeysPressed()) {
|
||||
let char = String.fromCharCode(key).toLowerCase();
|
||||
if (shiftKeyDown()) {
|
||||
@ -19,10 +30,9 @@ const update = () => {
|
||||
line = line.slice(0, index)+char+line.slice(index);
|
||||
index += 1;
|
||||
} else if (key === K.BACKSPACE) {
|
||||
line = line.slice(0, -1);
|
||||
index -= 1;
|
||||
if (index < 0) {
|
||||
index = 0;
|
||||
if (index > 0) {
|
||||
line = line.slice(0, index-1)+line.slice(index);
|
||||
index -= 1;
|
||||
}
|
||||
} else if (key === K.ARROW_LEFT) {
|
||||
index -= 1;
|
||||
@ -34,14 +44,28 @@ const update = () => {
|
||||
if (index > line.length) {
|
||||
index = line.length;
|
||||
}
|
||||
} else if (key === K.ENTER) {
|
||||
const ran = runCode('return '+line);
|
||||
console.log('ran:', ran);
|
||||
result = ran?.toString?.();
|
||||
maxLineLen = 0;
|
||||
line = "";
|
||||
index = 0;
|
||||
y += lineHeight;
|
||||
}
|
||||
}
|
||||
maxLineLen = Math.max(maxLineLen, line.length);
|
||||
}
|
||||
|
||||
const draw = () => {
|
||||
faux.clear_screen();
|
||||
faux.draw_rect((2+index)*4, 0, 5, 6, 3);
|
||||
faux.draw_text(0, 0, "> "+line);
|
||||
if (result) {
|
||||
faux.draw_rect(0, y, 4*result.length+1, lineHeight+1, 6);
|
||||
faux.draw_text(0, y, result);
|
||||
} else {
|
||||
faux.draw_rect(0, y, 4*(2+maxLineLen+1)+1, lineHeight+1, 6);
|
||||
faux.draw_rect((2+index)*4, y+1, 4, lineHeight-1, 3);
|
||||
faux.draw_text(0, y, "> "+line);
|
||||
}
|
||||
}
|
||||
|
||||
export const repl = {
|
||||
|
Reference in New Issue
Block a user