fantasy-console/repl.ts

49 lines
1.0 KiB
TypeScript

import faux from "./builtins.ts";
import { getKeysPressed, shiftKeyDown, shiftMap, K } from "./keyboard.ts";
import { font } from "./font.ts";
let line = "";
let index = 0;
const update = () => {
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) {
line = line.slice(0, -1);
index -= 1;
if (index < 0) {
index = 0;
}
} 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;
}
}
}
}
const draw = () => {
faux.clear_screen();
faux.draw_rect((2+index)*4, 0, 5, 6, 3);
faux.draw_text(0, 0, "> "+line);
}
export const repl = {
update, draw
}