shell: Support file loading from disk

This commit is contained in:
Remko Tronçon 2022-12-18 17:25:29 +01:00
parent b644725476
commit c6deb5eba3

View file

@ -61,32 +61,9 @@ function unoutput(isInput) {
function startConsole() {
let inputbuffer = [];
document.addEventListener("keydown", (ev) => {
// console.log("keydown", ev);
if (ev.key === "Enter") {
output(" ", true);
forth.interpret(inputbuffer.join(""));
inputbuffer = [];
} else if (ev.key === "Backspace") {
if (inputbuffer.length > 0) {
inputbuffer = inputbuffer.slice(0, inputbuffer.length - 1);
unoutput(true);
}
} else if (ev.key.length === 1 && !ev.metaKey && !ev.ctrlKey) {
output(ev.key, true);
inputbuffer.push(ev.key);
} else {
console.log("ignoring key %s", ev.key);
}
if (ev.key === " ") {
ev.preventDefault();
}
});
document.addEventListener("paste", (event) => {
let paste = (event.clipboardData || window.clipboardData).getData("text");
const commands = paste.split("\n");
// console.log("paste", paste, commands);
function load(s) {
const commands = s.split("\n");
let newInputBuffer = [];
if (commands.length > 0) {
newInputBuffer.push(commands.pop());
@ -102,6 +79,52 @@ function startConsole() {
flush();
}
inputbuffer = newInputBuffer;
}
document.addEventListener("keydown", (ev) => {
// console.log("keydown", ev);
if (ev.key === "Enter") {
output(" ", true);
forth.interpret(inputbuffer.join(""));
inputbuffer = [];
} else if (ev.key === "Backspace") {
if (inputbuffer.length > 0) {
inputbuffer = inputbuffer.slice(0, inputbuffer.length - 1);
unoutput(true);
}
} else if (ev.key.length === 1 && !ev.metaKey && !ev.ctrlKey) {
output(ev.key, true);
inputbuffer.push(ev.key);
} else if (ev.key === "o" && (ev.metaKey || ev.ctrlKey)) {
if (!window.showOpenFilePicker) {
window.alert("File loading not supported on this browser");
return;
}
(async () => {
const [fh] = await window.showOpenFilePicker({
types: [
{
description: "Forth source files",
accept: {
"text/plain": [".fs", ".f", ".fth", ".f4th", ".fr"],
},
},
],
excludeAcceptAllOption: true,
multiple: false,
});
load(await (await fh.getFile()).text());
})();
} else {
console.log("ignoring key %s", ev.key);
}
if (ev.key === " ") {
ev.preventDefault();
}
});
document.addEventListener("paste", (event) => {
load(event.clipboardData || window.clipboardData).getData("text");
});
}
@ -117,9 +140,10 @@ forth.onEmit = withCharacterBuffer((c) => {
clearConsole();
(async () => {
output("Loading core ... ", false, true);
forth.load().then(
() => {
try {
await forth.load();
clearConsole();
startConsole();
@ -143,8 +167,7 @@ forth.load().then(
forth.interpret(command);
}
}
},
(e) => {
} catch (e) {
console.error(e);
const errorEl = document.createElement("span");
errorEl.className = "error";
@ -153,4 +176,4 @@ forth.load().then(
inputEl.remove();
consoleEl.appendChild(errorEl);
}
);
})();