@@ -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' ) ;
0 commit comments