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() { function startConsole() {
let inputbuffer = []; 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) => { function load(s) {
let paste = (event.clipboardData || window.clipboardData).getData("text"); const commands = s.split("\n");
const commands = paste.split("\n");
// console.log("paste", paste, commands);
let newInputBuffer = []; let newInputBuffer = [];
if (commands.length > 0) { if (commands.length > 0) {
newInputBuffer.push(commands.pop()); newInputBuffer.push(commands.pop());
@ -102,6 +79,52 @@ function startConsole() {
flush(); flush();
} }
inputbuffer = newInputBuffer; 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(); clearConsole();
output("Loading core ... ", false, true); (async () => {
forth.load().then( output("Loading core ... ", false, true);
() => { try {
await forth.load();
clearConsole(); clearConsole();
startConsole(); startConsole();
@ -143,8 +167,7 @@ forth.load().then(
forth.interpret(command); forth.interpret(command);
} }
} }
}, } catch (e) {
(e) => {
console.error(e); console.error(e);
const errorEl = document.createElement("span"); const errorEl = document.createElement("span");
errorEl.className = "error"; errorEl.className = "error";
@ -153,4 +176,4 @@ forth.load().then(
inputEl.remove(); inputEl.remove();
consoleEl.appendChild(errorEl); consoleEl.appendChild(errorEl);
} }
); })();