More repl improvements
This commit is contained in:
parent
1482288b0c
commit
ce7da27cc3
43
index.ts
43
index.ts
@ -4,27 +4,48 @@ import {
|
|||||||
clearScreen,
|
clearScreen,
|
||||||
} from "./window.ts";
|
} from "./window.ts";
|
||||||
import { codeSheet } from "./sheet.ts";
|
import { codeSheet } from "./sheet.ts";
|
||||||
import { refreshKeyboard } from "./keyboard.ts";
|
import { refreshKeyboard, keyPressed, K } from "./keyboard.ts";
|
||||||
import { repl } from "./repl.ts";
|
import { repl, resetRepl } from "./repl.ts";
|
||||||
|
import { addToContext } from "./runcode.ts";
|
||||||
|
|
||||||
const game = codeSheet(0);
|
const game = codeSheet(0);
|
||||||
|
|
||||||
game.init();
|
game.init();
|
||||||
|
|
||||||
let mode: "play" | "edit" = "edit";
|
let mode: "play" | "edit" | "repl" = "repl";
|
||||||
|
|
||||||
|
addToContext("play", () => {mode = "play"});
|
||||||
|
|
||||||
clearScreen();
|
clearScreen();
|
||||||
|
|
||||||
await mainloop((_t) => {
|
await mainloop((_t) => {
|
||||||
// TODO: use t
|
// TODO: use t
|
||||||
if (mode === "play") {
|
if (keyPressed(K.ESCAPE)) {
|
||||||
game.update();
|
console.log('pressed escape');
|
||||||
game.draw();
|
if (mode === "play") {
|
||||||
frame();
|
resetRepl();
|
||||||
} else if (mode === "edit") {
|
}
|
||||||
repl.update();
|
if (mode === "edit") {
|
||||||
repl.draw();
|
clearScreen();
|
||||||
frame();
|
}
|
||||||
|
mode = ({
|
||||||
|
play: "repl",
|
||||||
|
edit: "repl",
|
||||||
|
repl: "edit",
|
||||||
|
} as const)[mode];
|
||||||
|
} else {
|
||||||
|
if (mode === "play") {
|
||||||
|
game.update();
|
||||||
|
game.draw();
|
||||||
|
frame();
|
||||||
|
} else if (mode === "repl") {
|
||||||
|
repl.update();
|
||||||
|
repl.draw();
|
||||||
|
frame();
|
||||||
|
} else if (mode === "edit") {
|
||||||
|
clearScreen();
|
||||||
|
frame();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
refreshKeyboard();
|
refreshKeyboard();
|
||||||
});
|
});
|
||||||
|
@ -97,14 +97,14 @@ export const refreshKeyboard = () => {
|
|||||||
|
|
||||||
export const keyPressed = (key: string | number) => {
|
export const keyPressed = (key: string | number) => {
|
||||||
if (typeof key === "string") {
|
if (typeof key === "string") {
|
||||||
key = key.charCodeAt(0);
|
key = key.toUpperCase().charCodeAt(0);
|
||||||
}
|
}
|
||||||
return keyboard.has(key) && keyboard.get(key)?.repeat!;
|
return keyboard.has(key) && (keyboard.get(key)?.first! || keyboard.get(key)?.repeat!);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const keyDown = (key: string | number) => {
|
export const keyDown = (key: string | number) => {
|
||||||
if (typeof key === "string") {
|
if (typeof key === "string") {
|
||||||
key = key.charCodeAt(0);
|
key = key.toUpperCase().charCodeAt(0);
|
||||||
}
|
}
|
||||||
return keyboard.has(key) && keyboard.get(key)?.held!;
|
return keyboard.has(key) && keyboard.get(key)?.held!;
|
||||||
}
|
}
|
||||||
@ -115,7 +115,7 @@ export const shiftKeyDown = () => {
|
|||||||
|
|
||||||
export const keyReleased = (key: string | number) => {
|
export const keyReleased = (key: string | number) => {
|
||||||
if (typeof key === "string") {
|
if (typeof key === "string") {
|
||||||
key = key.charCodeAt(0);
|
key = key.toUpperCase().charCodeAt(0);
|
||||||
}
|
}
|
||||||
return keyboard.has(key) && !keyboard.get(key)?.held!;
|
return keyboard.has(key) && !keyboard.get(key)?.held!;
|
||||||
}
|
}
|
||||||
|
85
repl.ts
85
repl.ts
@ -5,18 +5,22 @@ import { runCode } from "./runcode.ts";
|
|||||||
|
|
||||||
const lineHeight = 6;
|
const lineHeight = 6;
|
||||||
|
|
||||||
|
const history: Array<string> = [];
|
||||||
|
let historyIndex = history.length;
|
||||||
|
let textLinesAbove: Array<string> = [];
|
||||||
let maxLineLen = 0;
|
let maxLineLen = 0;
|
||||||
let line = "";
|
let currentLine = "";
|
||||||
let index = 0;
|
let index = 0;
|
||||||
let y = 0;
|
|
||||||
let result = "";
|
export const resetRepl = () => {
|
||||||
|
historyIndex = history.length;
|
||||||
|
textLinesAbove.length = 0;
|
||||||
|
maxLineLen = 0;
|
||||||
|
currentLine = "";
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
const update = () => {
|
const update = () => {
|
||||||
if (result) {
|
|
||||||
console.log("result:", result);
|
|
||||||
y += lineHeight; // TODO: multiply if multiline
|
|
||||||
result = "";
|
|
||||||
}
|
|
||||||
for (const key of getKeysPressed()) {
|
for (const key of getKeysPressed()) {
|
||||||
let char = String.fromCharCode(key).toLowerCase();
|
let char = String.fromCharCode(key).toLowerCase();
|
||||||
if (shiftKeyDown()) {
|
if (shiftKeyDown()) {
|
||||||
@ -27,11 +31,11 @@ const update = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (char in font) {
|
if (char in font) {
|
||||||
line = line.slice(0, index)+char+line.slice(index);
|
currentLine = currentLine.slice(0, index)+char+currentLine.slice(index);
|
||||||
index += 1;
|
index += 1;
|
||||||
} else if (key === K.BACKSPACE) {
|
} else if (key === K.BACKSPACE) {
|
||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
line = line.slice(0, index-1)+line.slice(index);
|
currentLine = currentLine.slice(0, index-1)+currentLine.slice(index);
|
||||||
index -= 1;
|
index -= 1;
|
||||||
}
|
}
|
||||||
} else if (key === K.ARROW_LEFT) {
|
} else if (key === K.ARROW_LEFT) {
|
||||||
@ -41,31 +45,60 @@ const update = () => {
|
|||||||
}
|
}
|
||||||
} else if (key === K.ARROW_RIGHT) {
|
} else if (key === K.ARROW_RIGHT) {
|
||||||
index += 1;
|
index += 1;
|
||||||
if (index > line.length) {
|
if (index > currentLine.length) {
|
||||||
index = line.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) {
|
} else if (key === K.ENTER) {
|
||||||
const ran = runCode('return '+line);
|
history.push(currentLine);
|
||||||
console.log('ran:', ran);
|
historyIndex = history.length;
|
||||||
result = ran?.toString?.();
|
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;
|
maxLineLen = 0;
|
||||||
line = "";
|
currentLine = "";
|
||||||
index = 0;
|
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 = () => {
|
const draw = () => {
|
||||||
if (result) {
|
drawTextAbove();
|
||||||
faux.draw_rect(0, y, 4*result.length+1, lineHeight+1, 6);
|
|
||||||
faux.draw_text(0, y, result);
|
faux.draw_rect(0, textLinesAbove.length*lineHeight, 4*(2+maxLineLen+1)+1, lineHeight+1, 6);
|
||||||
} else {
|
faux.draw_rect((2+index)*4, textLinesAbove.length*lineHeight+1, 4, lineHeight-1, 3);
|
||||||
faux.draw_rect(0, y, 4*(2+maxLineLen+1)+1, lineHeight+1, 6);
|
faux.draw_text(-1, textLinesAbove.length*lineHeight, "> "+currentLine);
|
||||||
faux.draw_rect((2+index)*4, y+1, 4, lineHeight-1, 3);
|
|
||||||
faux.draw_text(0, y, "> "+line);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const repl = {
|
export const repl = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user