More repl improvements
This commit is contained in:
85
repl.ts
85
repl.ts
@ -5,18 +5,22 @@ import { runCode } from "./runcode.ts";
|
||||
|
||||
const lineHeight = 6;
|
||||
|
||||
const history: Array<string> = [];
|
||||
let historyIndex = history.length;
|
||||
let textLinesAbove: Array<string> = [];
|
||||
let maxLineLen = 0;
|
||||
let line = "";
|
||||
let currentLine = "";
|
||||
let index = 0;
|
||||
let y = 0;
|
||||
let result = "";
|
||||
|
||||
export const resetRepl = () => {
|
||||
historyIndex = history.length;
|
||||
textLinesAbove.length = 0;
|
||||
maxLineLen = 0;
|
||||
currentLine = "";
|
||||
index = 0;
|
||||
}
|
||||
|
||||
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()) {
|
||||
@ -27,11 +31,11 @@ const update = () => {
|
||||
}
|
||||
}
|
||||
if (char in font) {
|
||||
line = line.slice(0, index)+char+line.slice(index);
|
||||
currentLine = currentLine.slice(0, index)+char+currentLine.slice(index);
|
||||
index += 1;
|
||||
} else if (key === K.BACKSPACE) {
|
||||
if (index > 0) {
|
||||
line = line.slice(0, index-1)+line.slice(index);
|
||||
currentLine = currentLine.slice(0, index-1)+currentLine.slice(index);
|
||||
index -= 1;
|
||||
}
|
||||
} else if (key === K.ARROW_LEFT) {
|
||||
@ -41,31 +45,60 @@ const update = () => {
|
||||
}
|
||||
} else if (key === K.ARROW_RIGHT) {
|
||||
index += 1;
|
||||
if (index > line.length) {
|
||||
index = line.length;
|
||||
if (index > currentLine.length) {
|
||||
index = currentLine.length;
|
||||
}
|
||||
} else if (key === K.ARROW_UP) {
|
||||
historyIndex -= 1;
|
||||
if (historyIndex < 0) {
|
||||
historyIndex = -1;
|
||||
}
|
||||
const lineAtHistory = history[historyIndex] ?? "";
|
||||
currentLine = lineAtHistory;
|
||||
index = currentLine.length;
|
||||
} else if (key === K.ARROW_DOWN) {
|
||||
historyIndex += 1;
|
||||
if (historyIndex > history.length) {
|
||||
historyIndex = history.length;
|
||||
}
|
||||
const lineAtHistory = history[historyIndex] ?? "";
|
||||
currentLine = lineAtHistory;
|
||||
index = currentLine.length;
|
||||
} else if (key === K.ENTER) {
|
||||
const ran = runCode('return '+line);
|
||||
console.log('ran:', ran);
|
||||
result = ran?.toString?.();
|
||||
history.push(currentLine);
|
||||
historyIndex = history.length;
|
||||
textLinesAbove.push("> "+currentLine);
|
||||
try {
|
||||
const ran = runCode('return '+currentLine);
|
||||
const resultString = ran?.toString?.() ?? null;
|
||||
if (resultString !== null) {
|
||||
textLinesAbove.push(...resultString.split("\n"))
|
||||
}
|
||||
} catch (err) {
|
||||
textLinesAbove.push(...(err.name+":\n"+err.message).toLowerCase().split("\n"))
|
||||
}
|
||||
textLinesAbove = textLinesAbove.slice(-20);
|
||||
maxLineLen = 0;
|
||||
line = "";
|
||||
currentLine = "";
|
||||
index = 0;
|
||||
y += lineHeight;
|
||||
}
|
||||
}
|
||||
maxLineLen = Math.max(maxLineLen, line.length);
|
||||
maxLineLen = Math.max(maxLineLen, currentLine.length);
|
||||
}
|
||||
|
||||
const drawTextAbove = () => {
|
||||
textLinesAbove.forEach((line, i) => {
|
||||
faux.draw_rect(0, i*lineHeight, 4*(line.length+1)+1, lineHeight+1, 6);
|
||||
faux.draw_text(-1, i*lineHeight, line);
|
||||
});
|
||||
}
|
||||
|
||||
const draw = () => {
|
||||
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);
|
||||
}
|
||||
drawTextAbove();
|
||||
|
||||
faux.draw_rect(0, textLinesAbove.length*lineHeight, 4*(2+maxLineLen+1)+1, lineHeight+1, 6);
|
||||
faux.draw_rect((2+index)*4, textLinesAbove.length*lineHeight+1, 4, lineHeight-1, 3);
|
||||
faux.draw_text(-1, textLinesAbove.length*lineHeight, "> "+currentLine);
|
||||
}
|
||||
|
||||
export const repl = {
|
||||
|
Reference in New Issue
Block a user