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()) { if (char in shiftMap) { char = shiftMap[char as keyof typeof shiftMap]; } else { char = char.toUpperCase(); } } if (char in font) { line = line.slice(0, index)+char+line.slice(index); index += 1; } else if (key === K.BACKSPACE) { if (index > 0) { line = line.slice(0, index-1)+line.slice(index); index -= 1; } } 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; } } 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 = () => { 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 = { update, draw }