UNPKG

@tty-pt/ndc

Version:

ndc example project

7 lines (6 loc) 2.71 kB
var b=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var H=Object.prototype.hasOwnProperty;var M=(r,t)=>{for(var l in t)b(r,l,{get:t[l],enumerable:!0})},D=(r,t,l,w)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of z(t))!H.call(r,c)&&c!==l&&b(r,c,{get:()=>t[c],enumerable:!(w=I(t,c))||w.enumerable});return r};var G=r=>D(b({},"__esModule",{value:!0}),r);var U={};M(U,{create:()=>m,default:()=>R});module.exports=G(U);var y=require("@xterm/addon-fit"),C=require("@xterm/addon-web-links");var B=0;function m(r,t={}){let{proto:l=location.protocol==="https:"?"wss":"ws",port:w=window.location.port,url:c=l+"://"+window.location.hostname+":"+w}=t,s=t.sub??{onMessage:function(n,o){return!0},onOpen:function(n,o){},onClose:function(n){},cols:80,rows:25},p=new y.FitAddon,L=new ResizeObserver(()=>p.fit()),e=new globalThis.Terminal({convertEol:!0,fontSize:13,fontFamily:"Consolas,Liberation Mono,Menlo,Courier,monospace",allowProposedApi:!0}),i={},P=n=>e.write(n),j=B;B++;function g(n){i.send(n)}let O=new TextDecoder("utf-8"),d=!1,f=!0,a=!1;function T(n){let o=new Uint8Array(n.data);if(s.onMessage(n,o)){if(o[0]!=255){let u=O.decode(o);e.write(u)}else if(o[1]==252)switch(o[2]){case 1:f=!1,t.debug&&console.log("WONT ECHO");break;case 3:a=!1,t.debug&&console.log("WONT SGA (ICANON/not raw)");break}else if(o[1]==251)switch(o[2]){case 1:f=!0,t.debug&&console.log("WILL ECHO");break;case 3:a=!0,t.debug&&console.log("WILL SGA (not ICANON/raw)");break}else if(o[1]==250)switch(o[2]){case 31:}}else return}function A(n,o){if(s.cols=n,s.rows=o,!d)return;let u=255,S=250,W=31,_=240,k=n>>8,v=n&255,x=o>>8,E=o&255,F=new Uint8Array([u,S,W,k,v,x,E,u,_]);i.send(F)}function N(n){return n.scrollTop=n.scrollHeight,e.loadAddon(p),e.loadAddon(new C.WebLinksAddon),e.open(n),e.inputBuf="",e.perm="",e.onResize(({cols:o,rows:u})=>A(o,u)),L.observe(n),e.element.addEventListener("focusin",()=>{e.focused=!0}),e.element.addEventListener("focusout",()=>{e.focused=!1}),e.onData(o=>{if(t.debug&&console.log("term.onData",o,o.charAt(0),a,f),a)g(o==="\r"?`\r `:o);else if(o==="\r"||o===` `)f?e.write("\b \b".repeat(e.inputBuf.length)):e.write(` `),i.send(e.inputBuf+`\r `),e.inputBuf="";else if(o==="\x7F")a?g(o):(e.write("\b \b"),e.inputBuf=e.inputBuf.length>0?e.inputBuf.slice(0,e.inputBuf.length-1):"");else{e.inputBuf+=o,f&&e.write(o);return}e.lastInput=!1}),e}N(r);function h(){i=new WebSocket(c,"binary"),i.binaryType="arraybuffer",i.onopen=()=>{d=!0,A(s.cols,s.rows),s.onOpen(e,i)},i.onmessage=T,i.onclose=()=>{d=!1,s.onClose(),s.timeout=setTimeout(()=>{clearTimeout(s.timeout),h()},3e3)}}return h(),s}window.ttyNdc={create:m};var R={create:m}; //# sourceMappingURL=ndc.js.map