UNPKG

neo-neo-bblessed

Version:

A fork of neo-blessed (which is a fork of blessed) with bug fixes and maintenance.

3 lines (2 loc) 6.25 kB
var h=global.setImmediate||process.nextTick.bind(process),f=require("./node"),m=require("./box");function i(e){if(!(this instanceof f))return new i(e);e=e||{},e.scrollable=!1,m.call(this,e),this.screen.program.tmux&&this.screen.program.tmuxVersion>=2&&this.screen.program.enableMouse(),this.handler=e.handler,this.shell=e.shell||process.env.SHELL||"sh",this.args=e.args||[],this.cursor=this.options.cursor,this.cursorBlink=this.options.cursorBlink,this.screenKeys=this.options.screenKeys,this.style=this.style||{},this.style.bg=this.style.bg||"default",this.style.fg=this.style.fg||"default",this.termName=e.terminal||e.term||process.env.TERM||"xterm",this.filter=e.filter||null,this.bootstrap()}i.prototype.__proto__=m.prototype,i.prototype.type="terminal",i.prototype.bootstrap=function(){var e=this,t={get document(){return t},navigator:{userAgent:"node.js"},get defaultView(){return t},get documentElement(){return t},createElement:function(){return t},get ownerDocument(){return t},addEventListener:function(){},removeEventListener:function(){},getElementsByTagName:function(){return[t]},getElementById:function(){return t},parentNode:null,offsetParent:null,appendChild:function(){},removeChild:function(){},setAttribute:function(){},getAttribute:function(){},style:{},focus:function(){},blur:function(){},console};if(t.parentNode=t,t.offsetParent=t,this.term=require("term.js")({termName:this.termName,cols:this.width-this.iwidth,rows:this.height-this.iheight,context:t,document:t,body:t,parent:t,cursorBlink:this.cursorBlink,screenKeys:this.screenKeys}),this.term.refresh=function(){e.screen.render()},this.term.keyDown=function(){},this.term.keyPress=function(){},this.term.open(t),this.screen.program.input.on("data",this._onData=function(r){e.screen.focused===e&&!e._isMouse(r)&&e.handler(r)}),this.onScreenEvent("mouse",function(r){if(e.screen.focused===e&&!(r.x<e.aleft+e.ileft)&&!(r.y<e.atop+e.itop)&&!(r.x>e.aleft-e.ileft+e.width)&&!(r.y>e.atop-e.itop+e.height)&&(e.term.x10Mouse||e.term.vt200Mouse||e.term.normalMouse||e.term.mouseEvents||e.term.utfMouse||e.term.sgrMouse||e.term.urxvtMouse)){var l=r.raw[0],c=r.x-e.aleft,a=r.y-e.atop,o;e.term.urxvtMouse?(e.screen.program.sgrMouse&&(l+=32),o="\x1B["+l+";"+(c+32)+";"+(a+32)+"M"):e.term.sgrMouse?(e.screen.program.sgrMouse||(l-=32),o="\x1B[<"+l+";"+c+";"+a+(r.action==="mousedown"?"M":"m")):(e.screen.program.sgrMouse&&(l+=32),o="\x1B[M"+String.fromCharCode(l)+String.fromCharCode(c+32)+String.fromCharCode(a+32)),e.handler(o)}}),this.on("focus",function(){e.term.focus()}),this.on("blur",function(){e.term.blur()}),this.term.on("title",function(r){e.title=r,e.emit("title",r)}),this.term.on("passthrough",function(r){e.screen.program.flush(),e.screen.program._owrite(r)}),this.on("resize",function(){h(function(){e.term.resize(e.width-e.iwidth,e.height-e.iheight)})}),this.once("render",function(){e.term.resize(e.width-e.iwidth,e.height-e.iheight)}),this.on("destroy",function(){e.kill(),e.screen.program.input.removeListener("data",e._onData)}),this.handler)return;let u;try{u=require("node-pty")}catch(r){throw new Error("node-pty is required for Terminal widget functionality. Install it with: npm install node-pty")}this.pty=u.fork(this.shell,this.args,{name:this.termName,cols:this.width-this.iwidth,rows:this.height-this.iheight,cwd:process.env.HOME,env:this.options.env||process.env}),this.on("resize",function(){h(function(){try{e.pty.resize(e.width-e.iwidth,e.height-e.iheight)}catch(r){}})}),this.handler=function(r){e.pty.write(r),e.screen.render()},this.pty.on("data",function(r){e.filter&&(r=e.filter(r)),e.write(r),e.screen.render()}),this.pty.on("exit",function(r){e.emit("exit",r||null)}),this.onScreenEvent("keypress",function(){e.screen.render()}),this.screen._listenKeys(this)},i.prototype.write=function(e){return this.term.write(e)},i.prototype.render=function(){var e=this._render();if(e){this.dattr=this.sattr(this.style);for(var t=e.xi+this.ileft,u=e.xl-this.iright,r=e.yi+this.itop,l=e.yl-this.ibottom,c,a=this.term.lines.length-(l-r),o=Math.max(r,0);o<l;o++){var s=this.screen.lines[o];if(!s||!this.term.lines[a+o-r])break;o===r+this.term.y&&this.term.cursorState&&this.screen.focused===this&&(this.term.ydisp===this.term.ybase||this.term.selectMode)&&!this.term.cursorHidden?c=t+this.term.x:c=-1;for(var n=Math.max(t,0);n<u&&!(!s[n]||!this.term.lines[a+o-r][n-t]);n++){if(s[n][0]=this.term.lines[a+o-r][n-t][0],n===c)if(this.cursor==="line"){s[n][0]=this.dattr,s[n][1]="\u2502";continue}else this.cursor==="underline"?s[n][0]=this.dattr|2<<18:(this.cursor==="block"||!this.cursor)&&(s[n][0]=this.dattr|8<<18);s[n][1]=this.term.lines[a+o-r][n-t][1],(s[n][0]>>9&511)===257&&(s[n][0]&=-261633,s[n][0]|=(this.dattr>>9&511)<<9),(s[n][0]&511)===256&&(s[n][0]&=-512,s[n][0]|=this.dattr&511)}s.dirty=!0}return e}},i.prototype._isMouse=function(e){var t=e;return Buffer.isBuffer(t)&&(t[0]>127&&t[1]===void 0?(t[0]-=128,t="\x1B"+t.toString("utf-8")):t=t.toString("utf-8")),e[0]===27&&e[1]===91&&e[2]===77||/^\x1b\[M([\x00\u0020-\uffff]{3})/.test(t)||/^\x1b\[(\d+;\d+;\d+)M/.test(t)||/^\x1b\[<(\d+;\d+;\d+)([mM])/.test(t)||/^\x1b\[<(\d+;\d+;\d+;\d+)&w/.test(t)||/^\x1b\[24([0135])~\[(\d+),(\d+)\]\r/.test(t)||/^\x1b\[(O|I)/.test(t)},i.prototype.setScroll=i.prototype.scrollTo=function(e){return this.term.ydisp=e,this.emit("scroll")},i.prototype.getScroll=function(){return this.term.ydisp},i.prototype.scroll=function(e){return this.term.scrollDisp(e),this.emit("scroll")},i.prototype.resetScroll=function(){return this.term.ydisp=0,this.term.ybase=0,this.emit("scroll")},i.prototype.getScrollHeight=function(){return this.term.rows-1},i.prototype.getScrollPerc=function(){return this.term.ydisp/this.term.ybase*100},i.prototype.setScrollPerc=function(e){return this.setScroll(e/100*this.term.ybase|0)},i.prototype.screenshot=function(e,t,u,r){return e=0+(e||0),t!=null?t=0+(t||0):t=this.term.lines[0].length,u=0+(u||0),r!=null?r=0+(r||0):r=this.term.lines.length,this.screen.screenshot(e,t,u,r,this.term)},i.prototype.kill=function(){this.pty&&(this.pty.destroy(),this.pty.kill()),this.term.refresh=function(){},this.term.write("\x1B[H\x1B[J"),this.term._blink&&clearInterval(this.term._blink),this.term.destroy()},module.exports=i; //# sourceMappingURL=terminal.js.map