tricu

A lazy functional language based on Tree Calculus
Log | Files | Refs | README | LICENSE

terminal-host.mjs (1222B)


      1 // Test-only host for complete native/compiler goldens. Production uses the DOM
      2 // terminal view. null means explicit EOF; an empty string is one empty line.
      3 export function terminalHost(Terminal, input) {
      4  let terminal, send, submitted=false, settled=false;
      5  const forbidden=()=>{throw Error('v2 golden invoked a legacy text callback');};
      6  return {
      7   before:async()=>{},after(){},read:forbidden,write:forbidden,running(){},
      8   get terminalText(){
      9    if(!settled)throw Error('terminal did not settle');
     10    const snapshot=terminal.output.snapshot();
     11    if(snapshot.error.bytes.length)throw Error('unexpected stderr');
     12    return new TextDecoder('utf-8',{fatal:true}).decode(new Uint8Array(snapshot.output.bytes));
     13   },
     14   structured:{
     15    open(run,command){submitted=false;settled=false;terminal=new Terminal(run);send=command;},
     16    event(event){
     17     let reply=terminal.handle(event);
     18     if(!reply&&terminal.waiting){
     19      const {run,id}=terminal.waiting;
     20      reply=submitted||input===null?terminal.endInput(run,id):terminal.line(run,id,input);
     21      submitted=true;
     22     }
     23     if(reply)send({run:reply.run,kind:'reply',reply});
     24    },
     25    close(_run,acknowledged){settled=acknowledged;if(settled)terminal.finish();},
     26   },
     27  };
     28 }