UNPKG

@tty-pt/ndc

Version:

ndc example project

7 lines (6 loc) 2.49 kB
var g=require("@xterm/addon-fit"),O=require("@xterm/addon-web-links");var b=0;function f(w,r={}){let{proto:A=location.protocol==="https:"?"wss":"ws",port:C=window.location.port,url:B=A+"://"+window.location.hostname+":"+C}=r,a=new g.FitAddon,h=new ResizeObserver(()=>a.fit()),n=new globalThis.Terminal({convertEol:!0,fontSize:13,fontFamily:"Consolas,Liberation Mono,Menlo,Courier,monospace",allowProposedApi:!0}),s=new WebSocket(B,"binary");s.binaryType="arraybuffer";let t=r.sub,d={...r,ws:s,term:n,write:o=>n.write(o)},y=b;b++;function u(o){console.log("SEND!",y,o),t.ws.send(o)}t?Object.assign(t,{...d,onMessage:t.onMessage,onOpen:t.onOpen,onClose:t.onClose,send:u}):t={...d,onMessage:function(o,e){return!0},onOpen:function(o,e){},onClose:function(o){},send:u};let I=new TextDecoder("utf-8"),p=null,S=new Promise(o=>p=o),l=!0,c=!1;function v(o){let e=new Uint8Array(o.data);if(t.onMessage(o,e)){if(e[0]!=255){let i=I.decode(e);n.write(i)}else if(e[1]==252)switch(e[2]){case 1:l=!1,r.debug&&console.log("WONT ECHO");break;case 3:c=!1,r.debug&&console.log("WONT SGA (ICANON/not raw)");break}else if(e[1]==251)switch(e[2]){case 1:l=!0,r.debug&&console.log("WILL ECHO");break;case 3:c=!0,r.debug&&console.log("WILL SGA (not ICANON/raw)");break}else if(e[1]==250)switch(e[2]){case 31:}}else return}function L(o,e){p(),a.fit(),t.onOpen(o,e)}function N(){t.onClose(t),s=s.onclose=s.onmessage=s.onopen=null,n.dispose();let o=setInterval(()=>{f(w,{...r,sub:t})},3e3),e=t.onOpen;t.onOpen=(i,m)=>{clearInterval(o),e(i,m),t.onOpen=e}}s.onopen=L,s.onmessage=v,s.onclose=N;function E(o,e){let M=o>>8,T=o&255,_=e>>8,k=e&255,F=new Uint8Array([255,250,31,M,T,_,k,255,240]);S.then(()=>s.send(F))}function W(o){return o.scrollTop=o.scrollHeight,n.loadAddon(a),n.loadAddon(new O.WebLinksAddon),n.open(o),n.inputBuf="",n.perm="",n.onResize(({cols:e,rows:i})=>E(e,i)),h.observe(o),n.element.addEventListener("focusin",()=>{n.focused=!0}),n.element.addEventListener("focusout",()=>{n.focused=!1}),n.onData(e=>{if(r.debug&&console.log("term.onData",e,e.charAt(0),c,l),c)u(e==="\r"?`\r `:e);else if(e==="\r"||e===` `)l?n.write("\b \b".repeat(n.inputBuf.length)):n.write(` `),s.send(n.inputBuf+`\r `),n.inputBuf="";else if(e==="\x7F")c?u(e):(n.write("\b \b"),n.inputBuf=n.inputBuf.length>0?n.inputBuf.slice(0,n.inputBuf.length-1):"");else{n.inputBuf+=e,l&&n.write(e);return}n.lastInput=!1}),n}return W(w),t}window.ttyNdc={create:f};var x=document.getElementById("term");f(x); //# sourceMappingURL=cli.js.map