2023-05-02 18:17:31 -07:00
|
|
|
import faux from "./builtins.ts";
|
2023-05-03 13:44:28 -07:00
|
|
|
import { getKeysPressed, shiftKeyDown, shiftMap, K } from "./keyboard.ts";
|
2023-05-02 18:17:31 -07:00
|
|
|
import { font } from "./font.ts";
|
2023-05-03 15:17:27 -07:00
|
|
|
import { runCode } from "./runcode.ts";
|
2023-05-02 18:17:31 -07:00
|
|
|
|
2023-05-03 15:17:27 -07:00
|
|
|
const lineHeight = 6;
|
|
|
|
|
|
|
|
let maxLineLen = 0;
|
2023-05-02 18:17:31 -07:00
|
|
|
let line = "";
|
2023-05-03 13:44:28 -07:00
|
|
|
let index = 0;
|
2023-05-03 15:17:27 -07:00
|
|
|
let y = 0;
|
|
|
|
let result = "";
|
2023-05-02 18:17:31 -07:00
|
|
|
|
|
|
|
const update = () => {
|
2023-05-03 15:17:27 -07:00
|
|
|
if (result) {
|
|
|
|
console.log("result:", result);
|
|
|
|
y += lineHeight; // TODO: multiply if multiline
|
|
|
|
result = "";
|
|
|
|
}
|
2023-05-02 18:17:31 -07:00
|
|
|
for (const key of getKeysPressed()) {
|
2023-05-02 18:44:27 -07:00
|
|
|
let char = String.fromCharCode(key).toLowerCase();
|
|
|
|
if (shiftKeyDown()) {
|
|
|
|
if (char in shiftMap) {
|
|
|
|
char = shiftMap[char as keyof typeof shiftMap];
|
|
|
|
} else {
|
|
|
|
char = char.toUpperCase();
|
|
|
|
}
|
|
|
|
}
|
2023-05-02 18:17:31 -07:00
|
|
|
if (char in font) {
|
2023-05-03 13:44:28 -07:00
|
|
|
line = line.slice(0, index)+char+line.slice(index);
|
|
|
|
index += 1;
|
|
|
|
} else if (key === K.BACKSPACE) {
|
2023-05-03 15:17:27 -07:00
|
|
|
if (index > 0) {
|
|
|
|
line = line.slice(0, index-1)+line.slice(index);
|
|
|
|
index -= 1;
|
2023-05-03 13:44:28 -07:00
|
|
|
}
|
|
|
|
} else if (key === K.ARROW_LEFT) {
|
|
|
|
index -= 1;
|
|
|
|
if (index < 0) {
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
} else if (key === K.ARROW_RIGHT) {
|
|
|
|
index += 1;
|
|
|
|
if (index > line.length) {
|
|
|
|
index = line.length;
|
|
|
|
}
|
2023-05-03 15:17:27 -07:00
|
|
|
} else if (key === K.ENTER) {
|
|
|
|
const ran = runCode('return '+line);
|
|
|
|
console.log('ran:', ran);
|
|
|
|
result = ran?.toString?.();
|
|
|
|
maxLineLen = 0;
|
|
|
|
line = "";
|
|
|
|
index = 0;
|
|
|
|
y += lineHeight;
|
2023-05-02 18:17:31 -07:00
|
|
|
}
|
|
|
|
}
|
2023-05-03 15:17:27 -07:00
|
|
|
maxLineLen = Math.max(maxLineLen, line.length);
|
2023-05-02 18:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const draw = () => {
|
2023-05-03 15:17:27 -07:00
|
|
|
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);
|
|
|
|
}
|
2023-05-02 18:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export const repl = {
|
|
|
|
update, draw
|
|
|
|
}
|