Improving repl more
This commit is contained in:
53
repl.ts
53
repl.ts
@ -1,8 +1,8 @@
|
||||
import faux from "./builtins.ts";
|
||||
import { drawText} from "./builtins.ts";
|
||||
import { getKeysPressed, shiftKeyDown, shiftMap, K } from "./keyboard.ts";
|
||||
import { font } from "./font.ts";
|
||||
import { runCode } from "./runcode.ts";
|
||||
import { clearScreen } from "./window.ts";
|
||||
import { addToContext, evalCode } from "./runcode.ts";
|
||||
import { clearScreen, fillRect } from "./window.ts";
|
||||
|
||||
const lineHeight = 6;
|
||||
|
||||
@ -21,6 +21,25 @@ export const resetRepl = () => {
|
||||
index = 0;
|
||||
}
|
||||
|
||||
const print = (arg: unknown) => {
|
||||
textLinesAbove.push(...String(arg).split("\n").flatMap((line)=>{
|
||||
const wrap = [];
|
||||
while (line.length) {
|
||||
wrap.push(line.slice(0,32));
|
||||
line = line.slice(32);
|
||||
}
|
||||
return wrap;
|
||||
}));
|
||||
textLinesAbove = textLinesAbove.slice(-20);
|
||||
}
|
||||
|
||||
const printVal = (arg: unknown) => {
|
||||
print(JSON.stringify(arg));
|
||||
}
|
||||
|
||||
addToContext("print", print);
|
||||
addToContext("printVal", printVal);
|
||||
|
||||
const update = () => {
|
||||
for (const key of getKeysPressed()) {
|
||||
let char = String.fromCharCode(key).toLowerCase();
|
||||
@ -66,19 +85,19 @@ const update = () => {
|
||||
currentLine = lineAtHistory;
|
||||
index = currentLine.length;
|
||||
} else if (key === K.ENTER) {
|
||||
history.push(currentLine);
|
||||
historyIndex = history.length;
|
||||
textLinesAbove.push("> "+currentLine);
|
||||
if (currentLine) {
|
||||
history.push(currentLine);
|
||||
historyIndex = history.length;
|
||||
}
|
||||
print("> "+currentLine);
|
||||
try {
|
||||
const ran = runCode('return '+currentLine);
|
||||
const resultString = ran?.toString?.() ?? null;
|
||||
if (resultString !== null) {
|
||||
textLinesAbove.push(...resultString.split("\n"))
|
||||
const ran = evalCode(currentLine);
|
||||
if (ran !== undefined) {
|
||||
printVal(ran);
|
||||
}
|
||||
} catch (err) {
|
||||
textLinesAbove.push(...(err.name+":\n"+err.message).split("\n"))
|
||||
print(err.name+":\n"+err.message);
|
||||
}
|
||||
textLinesAbove = textLinesAbove.slice(-20);
|
||||
maxLineLen = 0;
|
||||
currentLine = "";
|
||||
index = 0;
|
||||
@ -89,8 +108,8 @@ const update = () => {
|
||||
|
||||
const drawTextAbove = () => {
|
||||
textLinesAbove.forEach((line, i) => {
|
||||
faux.draw_rect(0, 1+i*lineHeight, 4*(line.length+1)+1, lineHeight+1, 0);
|
||||
faux.draw_text(-1, 1+i*lineHeight, line);
|
||||
fillRect(0, 1+i*lineHeight, 4*(line.length+1)+1, lineHeight+1, 0);
|
||||
drawText(-1, 1+i*lineHeight, line);
|
||||
});
|
||||
}
|
||||
|
||||
@ -99,9 +118,9 @@ const draw = () => {
|
||||
|
||||
drawTextAbove();
|
||||
|
||||
faux.draw_rect(0, 1+textLinesAbove.length*lineHeight, 4*(2+maxLineLen+1)+1, lineHeight+1, 0);
|
||||
faux.draw_rect((2+index)*4, textLinesAbove.length*lineHeight+1, 4, lineHeight-1, 3);
|
||||
faux.draw_text(-1, 1+textLinesAbove.length*lineHeight, "> "+currentLine);
|
||||
fillRect(0, 1+textLinesAbove.length*lineHeight, 4*(2+maxLineLen+1)+1, lineHeight+1, 0);
|
||||
fillRect((2+index)*4, textLinesAbove.length*lineHeight+1, 4, lineHeight-1, 3);
|
||||
drawText(-1, 1+textLinesAbove.length*lineHeight, "> "+currentLine);
|
||||
}
|
||||
|
||||
export const repl = {
|
||||
|
Reference in New Issue
Block a user