shell: Buffer output

This commit is contained in:
Remko Tronçon 2022-06-10 14:51:03 +02:00
parent c79c2f647e
commit 48219d1dd0

View file

@ -23,15 +23,29 @@ consoleEl.addEventListener("click", () => {
let currentConsoleEl; let currentConsoleEl;
let currentConsoleElIsInput = false; let currentConsoleElIsInput = false;
function output(s, isInput) { let outputBuffer = [];
function flush() {
if (outputBuffer.length == 0) {
return;
}
currentConsoleEl.appendChild(document.createTextNode(outputBuffer.join("")));
outputBuffer = [];
document.querySelector(".cursor").scrollIntoView(false);
}
function output(s, isInput, forceFlush = false) {
if (currentConsoleEl != null && currentConsoleElIsInput !== isInput) {
flush();
}
if (currentConsoleEl == null || currentConsoleElIsInput !== isInput) { if (currentConsoleEl == null || currentConsoleElIsInput !== isInput) {
currentConsoleEl = document.createElement("span"); currentConsoleEl = document.createElement("span");
currentConsoleEl.className = isInput ? "in" : "out"; currentConsoleEl.className = isInput ? "in" : "out";
currentConsoleElIsInput = isInput; currentConsoleElIsInput = isInput;
consoleEl.insertBefore(currentConsoleEl, cursorEl); consoleEl.insertBefore(currentConsoleEl, cursorEl);
} }
currentConsoleEl.appendChild(document.createTextNode(s)); outputBuffer.push(s);
document.querySelector(".cursor").scrollIntoView(false); if (forceFlush || isInput || s.endsWith("\n")) {
flush();
}
} }
function unoutput(isInput) { function unoutput(isInput) {
if ( if (
@ -99,7 +113,7 @@ forth.onEmit = (c) => {
clearConsole(); clearConsole();
output("Loading core ... ", false); output("Loading core ... ", false, true);
forth.load().then( forth.load().then(
() => { () => {
clearConsole(); clearConsole();