Skip to content

Commit 1627dd4

Browse files
committed
Show text when initializing the interpreter
1 parent 1ba7486 commit 1627dd4

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

electra_web/index.html

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,29 @@ <h1>Electra</h1>
244244
const outputEl = document.getElementById('output');
245245
const themeBtn = document.getElementById('theme-toggle');
246246
let worker = null;
247-
248-
function appendOutput(text) {
249-
outputEl.value += text;
250-
outputEl.scrollTop = outputEl.scrollHeight;
251-
}
247+
let programOutput = '';
252248

253249
function setOutput(text) {
254250
outputEl.value = text;
255251
outputEl.scrollTop = outputEl.scrollHeight;
256252
}
257253

254+
function appendOutput(text) {
255+
// outputEl.value += text;
256+
// outputEl.scrollTop = outputEl.scrollHeight;
257+
setOutput(outputEl.value + text);
258+
}
259+
260+
function appendProgramOutput(text) {
261+
appendOutput(text.slice(programOutput.length));
262+
programOutput = text;
263+
}
264+
265+
function clearOutput() {
266+
programOutput = '';
267+
setOutput(programOutput);
268+
}
269+
258270
function resetButton() {
259271
btn.textContent = 'Run';
260272
btn.classList.remove('running');
@@ -264,6 +276,7 @@ <h1>Electra</h1>
264276
if (btn.textContent === 'Run') {
265277
worker = new Worker("worker.js", { type: "module" });
266278
outputEl.value = '';
279+
programOutput = '';
267280
worker.postMessage({
268281
type: 'start',
269282
data: {
@@ -273,8 +286,10 @@ <h1>Electra</h1>
273286
});
274287
worker.onmessage = (e) => {
275288
const { type, data } = e.data;
276-
if (type === 'output') setOutput(String(data));
277-
else if (type === 'log') console.log(data);
289+
if (type === 'output') appendProgramOutput(String(data));
290+
else if (type === 'clog') console.log(data);
291+
else if (type === 'log') appendOutput(data);
292+
else if (type === 'clear') clearOutput();
278293
else if (type === 'final') { appendOutput(data); resetButton(); }
279294
};
280295
worker.onerror = (e) => appendOutput('Error: ' + e.message + '\n');

electra_web/worker.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ onmessage = async (e) => {
1313
input = data.input;
1414

1515
if(!Electra) {
16+
postMessage({ type: "log", data: "[Initializing the interpreter]\n" });
1617
Electra = await createModule();
18+
postMessage({ type: "clear", data: "" });
1719
}
1820

1921
loop();

0 commit comments

Comments
 (0)