UNPKG

neo-neo-bblessed

Version:

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

8 lines (7 loc) 26.8 kB
var E=require("path"),L=require("fs"),I=require("child_process"),R=require("os"),v=require("../colors"),K=require("../program"),O=require("../unicode"),B=global.setImmediate||process.nextTick.bind(process),q=require("../helpers"),T=require("./node"),A=require("./log"),H=require("./element"),F=require("./box");function o(e){var r=this;if(!(this instanceof T))return new o(e);o.bind(this),e=e||{},e.rsety&&e.listen&&(e={program:e}),this.program=e.program,this.program?(this.program.setupTput(),this.program.useBuffer=!0,this.program.zero=!0,this.program.options.resizeTimeout=e.resizeTimeout,e.forceUnicode!=null&&(this.program.tput.features.unicode=e.forceUnicode,this.program.tput.unicode=e.forceUnicode)):this.program=K({input:e.input,output:e.output,log:e.log,debug:e.debug,dump:e.dump,terminal:e.terminal||e.term,resizeTimeout:e.resizeTimeout,forceUnicode:e.forceUnicode,tput:!0,buffer:!0,zero:!0}),this.tput=this.program.tput,T.call(this,e),this.autoPadding=e.autoPadding!==!1,this.tabc=Array((e.tabSize||4)+1).join(" "),this.dockBorders=e.dockBorders,this.ignoreLocked=e.ignoreLocked||[],this._unicode=this.tput.unicode||this.tput.numbers.U8===1,this.fullUnicode=this.options.fullUnicode&&this._unicode,this.dattr=262143,this.renders=0,this.position={left:this.left=this.aleft=this.rleft=0,right:this.right=this.aright=this.rright=0,top:this.top=this.atop=this.rtop=0,bottom:this.bottom=this.abottom=this.rbottom=0,get height(){return r.height},get width(){return r.width}},this.ileft=0,this.itop=0,this.iright=0,this.ibottom=0,this.iheight=0,this.iwidth=0,this.padding={left:0,top:0,right:0,bottom:0},this.hover=null,this.history=[],this.clickable=[],this.keyable=[],this.grabKeys=!1,this.lockKeys=!1,this.focused,this._buf="",this._ci=-1,e.title&&(this.title=e.title),e.cursor=e.cursor||{artificial:e.artificialCursor,shape:e.cursorShape,blink:e.cursorBlink,color:e.cursorColor},this.cursor={artificial:e.cursor.artificial||!1,shape:e.cursor.shape||"block",blink:e.cursor.blink||!1,color:e.cursor.color||null,_set:!1,_state:1,_hidden:!0},this.program.on("resize",function(){r.alloc(),r.render(),function t(s){s.emit("resize"),s.children.forEach(t)}(r)}),this.program.on("focus",function(){r.emit("focus")}),this.program.on("blur",function(){r.emit("blur")}),this.program.on("warning",function(t){r.emit("warning",t)}),this.on("newListener",function(s){(s==="keypress"||s.indexOf("key ")===0||s==="mouse")&&((s==="keypress"||s.indexOf("key ")===0)&&r._listenKeys(),s==="mouse"&&r._listenMouse()),(s==="mouse"||s==="click"||s==="mouseover"||s==="mouseout"||s==="mousedown"||s==="mouseup"||s==="mousewheel"||s==="wheeldown"||s==="wheelup"||s==="mousemove")&&r._listenMouse()}),this.setMaxListeners(1/0),this.enter(),this.postEnter()}o.global=null,o.total=0,o.instances=[],o.bind=function(e){o.global||(o.global=e),~o.instances.indexOf(e)||(o.instances.push(e),e.index=o.total,o.total++),!o._bound&&(o._bound=!0,process.on("uncaughtException",o._exceptionHandler=function(r){o.instances.slice().forEach(function(t){t.destroy()}),r=r||new Error("Uncaught Exception."),process.stderr.write((r.stack||r)+` `),B(function(){process.exit(1)})}),["SIGTERM","SIGINT","SIGQUIT"].forEach(function(r){var t="_"+r.toLowerCase()+"Handler";process.on(r,o[t]=function(){process.listeners(r).length>1||B(function(){process.exit(0)})})}),process.on("exit",o._exitHandler=function(){o.instances.slice().forEach(function(r){r.destroy()})}))},o.prototype.__proto__=T.prototype,o.prototype.type="screen",o.prototype.__defineGetter__("title",function(){return this.program.title}),o.prototype.__defineSetter__("title",function(e){return this.program.title=e}),o.prototype.__defineGetter__("terminal",function(){return this.program.terminal}),o.prototype.__defineSetter__("terminal",function(e){return this.setTerminal(e),this.program.terminal}),o.prototype.setTerminal=function(e){var r=!!this.program.isAlt;r&&(this._buf="",this.program._buf="",this.leave()),this.program.setTerminal(e),this.tput=this.program.tput,r&&this.enter()},o.prototype.enter=function(){if(!this.program.isAlt){if(this.cursor._set||(this.options.cursor.shape&&this.cursorShape(this.cursor.shape,this.cursor.blink),this.options.cursor.color&&this.cursorColor(this.cursor.color)),process.platform==="win32")try{I.execSync("cls",{stdio:"ignore",timeout:1e3})}catch(e){}this.program.alternateBuffer(),this.program.put.keypad_xmit(),this.program.csr(0,this.height-1),this.program.hideCursor(),this.program.cup(0,0),this.tput.strings.ena_acs&&this.program._write(this.tput.enacs()),this.alloc()}},o.prototype.leave=function(){if(this.program.isAlt&&(this.program.put.keypad_local(),(this.program.scrollTop!==0||this.program.scrollBottom!==this.rows-1)&&this.program.csr(0,this.height-1),this.program.showCursor(),this.alloc(),this._listenedMouse&&this.program.disableMouse(),this.program.normalBuffer(),this.cursor._set&&this.cursorReset(),this.program.flush(),process.platform==="win32"))try{I.execSync("cls",{stdio:"ignore",timeout:1e3})}catch(e){}},o.prototype.postEnter=function(){var e=this;this.options.debug&&(this.debugLog=new A({screen:this,parent:this,hidden:!0,draggable:!0,left:"center",top:"center",width:"30%",height:"30%",border:"line",label:" {bold}Debug Log{/bold} ",tags:!0,keys:!0,vi:!0,mouse:!0,scrollbar:{ch:" ",track:{bg:"yellow"},style:{inverse:!0}}}),this.debugLog.toggle=function(){e.debugLog.hidden?(e.saveFocus(),e.debugLog.show(),e.debugLog.setFront(),e.debugLog.focus()):(e.debugLog.hide(),e.restoreFocus()),e.render()},this.debugLog.key(["q","escape"],e.debugLog.toggle),this.key("f12",e.debugLog.toggle)),this.options.warnings&&this.on("warning",function(r){var t=new F({screen:e,parent:e,left:"center",top:"center",width:"shrink",padding:1,height:"shrink",align:"center",valign:"middle",border:"line",label:" {red-fg}{bold}WARNING{/} ",content:"{bold}"+r+"{/bold}",tags:!0});e.render();var s=setTimeout(function(){t.destroy(),e.render()},1500);s.unref&&s.unref()})},o.prototype._destroy=o.prototype.destroy,o.prototype.destroy=function(){this.leave();var e=o.instances.indexOf(this);~e&&(o.instances.splice(e,1),o.total--,o.global=o.instances[0],o.total===0&&(o.global=null,process.removeListener("uncaughtException",o._exceptionHandler),process.removeListener("SIGTERM",o._sigtermHandler),process.removeListener("SIGINT",o._sigintHandler),process.removeListener("SIGQUIT",o._sigquitHandler),process.removeListener("exit",o._exitHandler),delete o._exceptionHandler,delete o._sigtermHandler,delete o._sigintHandler,delete o._sigquitHandler,delete o._exitHandler,delete o._bound),this.destroyed=!0,this.emit("destroy"),this._destroy()),this.program.destroy()},o.prototype.log=function(...e){return this.program.log.apply(this.program,arguments)},o.prototype.debug=function(...e){return this.debugLog&&this.debugLog.log.apply(this.debugLog,arguments),this.program.debug.apply(this.program,arguments)},o.prototype._listenMouse=function(e){var r=this;e&&!~this.clickable.indexOf(e)&&(e.clickable=!0,this.clickable.push(e)),!this._listenedMouse&&(this._listenedMouse=!0,this.program.enableMouse(),this.options.sendFocus&&this.program.setMouse({sendFocus:!0},!0),this.on("render",function(){r._needsClickableSort=!0}),this.program.on("mouse",function(t){if(!r.lockKeys){r._needsClickableSort&&(r.clickable=q.hsort(r.clickable),r._needsClickableSort=!1);for(var s=0,i,u,n;s<r.clickable.length;s++)if(i=r.clickable[s],!(i.detached||!i.visible)&&(n=i.lpos,!!n&&t.x>=n.xi&&t.x<n.xl&&t.y>=n.yi&&t.y<n.yl)){i.emit("mouse",t),t.action==="mousedown"?r.mouseDown=i:t.action==="mouseup"?((r.mouseDown||i).emit("click",t),r.mouseDown=null):t.action==="mousemove"&&(r.hover&&i.index>r.hover.index&&(u=!1),r.hover!==i&&!u&&(r.hover&&r.hover.emit("mouseout",t),i.emit("mouseover",t),r.hover=i),u=!0),i.emit(t.action,t);break}(t.action==="mousemove"||t.action==="mousedown"||t.action==="mouseup")&&r.hover&&!u&&(r.hover.emit("mouseout",t),r.hover=null),r.emit("mouse",t),r.emit(t.action,t)}}),this.on("element click",function(t){t.clickable===!0&&t.options.autoFocus!==!1&&t.focus()}))},o.prototype.enableMouse=function(e){this._listenMouse(e)},o.prototype._listenKeys=function(e){var r=this;e&&!~this.keyable.indexOf(e)&&(e.keyable=!0,this.keyable.push(e)),!this._listenedKeys&&(this._listenedKeys=!0,this.program.on("keypress",function(t,s){if(!(r.lockKeys&&!~r.ignoreLocked.indexOf(s.full))){var i=r.focused,u=r.grabKeys;(!u||~r.ignoreLocked.indexOf(s.full))&&(r.emit("keypress",t,s),r.emit("key "+s.full,t,s)),!(r.grabKeys!==u||r.lockKeys)&&i&&i.keyable&&(i.emit("keypress",t,s),i.emit("key "+s.full,t,s))}}))},o.prototype.enableKeys=function(e){this._listenKeys(e)},o.prototype.enableInput=function(e){this._listenMouse(e),this._listenKeys(e)},o.prototype._initHover=function(){var e=this;this._hoverText||(this._hoverText=new F({screen:this,left:0,top:0,tags:!1,height:"shrink",width:"shrink",border:"line",style:{border:{fg:"default"},bg:"default",fg:"default"}}),this.on("mousemove",function(r){e._hoverText.detached||(e._hoverText.rleft=r.x+1,e._hoverText.rtop=r.y,e.render())}),this.on("element mouseover",function(r,t){r._hoverOptions&&(e._hoverText.parseTags=r.parseTags,e._hoverText.setContent(r._hoverOptions.text),e.append(e._hoverText),e._hoverText.rleft=t.x+1,e._hoverText.rtop=t.y,e.render())}),this.on("element mouseout",function(){e._hoverText.detached||(e._hoverText.detach(),e.render())}),this.on("element mouseup",function(r){e._hoverText.getContent()&&r._hoverOptions&&(e.append(e._hoverText),e.render())}))},o.prototype.__defineGetter__("cols",function(){return this.program.cols}),o.prototype.__defineGetter__("rows",function(){return this.program.rows}),o.prototype.__defineGetter__("width",function(){return this.program.cols}),o.prototype.__defineGetter__("height",function(){return this.program.rows}),o.prototype.alloc=function(e){var r,t;for(this.lines=[],t=0;t<this.rows;t++){for(this.lines[t]=[],r=0;r<this.cols;r++)this.lines[t][r]=[this.dattr," "];this.lines[t].dirty=!!e}for(this.olines=[],t=0;t<this.rows;t++)for(this.olines[t]=[],r=0;r<this.cols;r++)this.olines[t][r]=[this.dattr," "];this.program.clear()},o.prototype.realloc=function(){return this.alloc(!0)},o.prototype.render=function(){var e=this;this.destroyed||(this.emit("prerender"),this._borderStops={},this._ci=0,this.children.forEach(function(r){r.index=e._ci++,r.render()}),this._ci=-1,this.screen.dockBorders&&this._dockBorders(),this.draw(0,this.lines.length-1),this.focused&&this.focused._updateCursor&&this.focused._updateCursor(!0),this.renders++,this.emit("render"))},o.prototype.blankLine=function(e,r){for(var t=[],s=0;s<this.cols;s++)t[s]=[this.dattr,e||" "];return t.dirty=r,t},o.prototype.insertLine=function(e,r,t,s){if(!(!this.tput.strings.change_scroll_region||!this.tput.strings.delete_line||!this.tput.strings.insert_line)){this._buf+=this.tput.csr(t,s),this._buf+=this.tput.cup(r,0),this._buf+=this.tput.il(e),this._buf+=this.tput.csr(0,this.height-1);for(var i=s+1;e--;)this.lines.splice(r,0,this.blankLine()),this.lines.splice(i,1),this.olines.splice(r,0,this.blankLine()),this.olines.splice(i,1)}},o.prototype.deleteLine=function(e,r,t,s){if(!(!this.tput.strings.change_scroll_region||!this.tput.strings.delete_line||!this.tput.strings.insert_line)){this._buf+=this.tput.csr(t,s),this._buf+=this.tput.cup(r,0),this._buf+=this.tput.dl(e),this._buf+=this.tput.csr(0,this.height-1);for(var i=s+1;e--;)this.lines.splice(i,0,this.blankLine()),this.lines.splice(r,1),this.olines.splice(i,0,this.blankLine()),this.olines.splice(r,1)}},o.prototype.insertLineNC=function(e,r,t,s){if(!(!this.tput.strings.change_scroll_region||!this.tput.strings.delete_line)){this._buf+=this.tput.csr(t,s),this._buf+=this.tput.cup(t,0),this._buf+=this.tput.dl(e),this._buf+=this.tput.csr(0,this.height-1);for(var i=s+1;e--;)this.lines.splice(i,0,this.blankLine()),this.lines.splice(r,1),this.olines.splice(i,0,this.blankLine()),this.olines.splice(r,1)}},o.prototype.deleteLineNC=function(e,r,t,s){if(!(!this.tput.strings.change_scroll_region||!this.tput.strings.delete_line)){this._buf+=this.tput.csr(t,s),this._buf+=this.tput.cup(s,0),this._buf+=Array(e+1).join(` `),this._buf+=this.tput.csr(0,this.height-1);for(var i=s+1;e--;)this.lines.splice(i,0,this.blankLine()),this.lines.splice(r,1),this.olines.splice(i,0,this.blankLine()),this.olines.splice(r,1)}},o.prototype.insertBottom=function(e,r){return this.deleteLine(1,e,e,r)},o.prototype.insertTop=function(e,r){return this.insertLine(1,e,e,r)},o.prototype.deleteBottom=function(e,r){return this.clearRegion(0,this.width,r,r)},o.prototype.deleteTop=function(e,r){return this.deleteLine(1,e,e,r)},o.prototype.cleanSides=function(e){var r=e.lpos;if(!r)return!1;if(r._cleanSides!=null)return r._cleanSides;if(r.xi<=0&&r.xl>=this.width)return r._cleanSides=!0;if(this.options.fastCSR)return r.yi<0||r.yl>this.height?r._cleanSides=!1:this.width-(r.xl-r.xi)<40?r._cleanSides=!0:r._cleanSides=!1;if(!this.options.smartCSR)return!1;var t=r.yi+e.itop,s=r.yl-e.ibottom,i,u,n,h;if(r.yi<0||r.yl>this.height)return r._cleanSides=!1;if(r.xi-1<0||r.xl>this.width)return r._cleanSides=!0;for(n=r.xi-1;n>=0&&this.olines[t];n--)for(i=this.olines[t][n],h=t;h<s&&!(!this.olines[h]||!this.olines[h][n]);h++)if(u=this.olines[h][n],u[0]!==i[0]||u[1]!==i[1])return r._cleanSides=!1;for(n=r.xl;n<this.width&&this.olines[t];n++)for(i=this.olines[t][n],h=t;h<s&&!(!this.olines[h]||!this.olines[h][n]);h++)if(u=this.olines[h][n],u[0]!==i[0]||u[1]!==i[1])return r._cleanSides=!1;return r._cleanSides=!0},o.prototype._dockBorders=function(){var e=this.lines,r=this._borderStops,t,s,i,u;for(r=Object.keys(r).map(function(n){return+n}).sort(function(n,h){return n-h}),t=0;t<r.length;t++)if(s=r[t],!!e[s])for(i=0;i<this.width;i++)u=e[s][i][1],M[u]&&(e[s][i][1]=this._getAngle(e,i,s),e[s].dirty=!0)},o.prototype._getAngle=function(e,r,t){var s=0,i=e[t][r][0],u=e[t][r][1];if(e[t][r-1]&&U[e[t][r-1][1]]){if(!this.options.ignoreDockContrast&&e[t][r-1][0]!==i)return u;s|=8}if(e[t-1]&&j[e[t-1][r][1]]){if(!this.options.ignoreDockContrast&&e[t-1][r][0]!==i)return u;s|=4}if(e[t][r+1]&&P[e[t][r+1][1]]){if(!this.options.ignoreDockContrast&&e[t][r+1][0]!==i)return u;s|=2}if(e[t+1]&&z[e[t+1][r][1]]){if(!this.options.ignoreDockContrast&&e[t+1][r][0]!==i)return u;s|=1}return x[s]||u},o.prototype.draw=function(e,r){var t,s,i,u,n,h,a,f,l,c,b="",d,g,w,C,p,_=-1,k=-1,m,y;for(this._buf&&(b+=this._buf,this._buf=""),s=e;s<=r;s++)if(i=this.lines[s],m=this.olines[s],!(!i.dirty&&!(this.cursor.artificial&&s===this.program.y))){for(i.dirty=!1,u="",a=this.dattr,t=0;t<i.length;t++){if(h=i[t][0],n=i[t][1],this.cursor.artificial&&!this.cursor._hidden&&this.cursor._state&&t===this.program.x&&s===this.program.y){var S=this._cursorAttr(this.cursor,h);S.ch&&(n=S.ch),h=S.attr}if(this.options.useBCE&&n===" "&&(this.tput.bools.back_color_erase||(h&511)===(this.dattr&511))&&(h>>18&8)===(this.dattr>>18&8)){for(w=!0,C=!1,p=t;p<i.length;p++){if(i[p][0]!==h||i[p][1]!==" "){w=!1;break}(i[p][0]!==m[p][0]||i[p][1]!==m[p][1])&&(C=!0)}if(w&&C){for(_=-1,k=-1,h!==a&&(u+=this.codeAttr(h),a=h),u+=this.tput.cup(s,t),u+=this.tput.el(),p=t;p<i.length;p++)m[p][0]=h,m[p][1]=" ";break}}if(h===m[t][0]&&n===m[t][1]){_===-1&&(_=t,k=s);continue}else _!==-1&&(this.tput.strings.parm_right_cursor?u+=s===k?this.tput.cuf(t-_):this.tput.cup(s,t):u+=this.tput.cup(s,t),_=-1,k=-1);m[t][0]=h,m[t][1]=n,h!==a&&(a!==this.dattr&&(u+="\x1B[m"),h!==this.dattr&&(u+="\x1B[",l=h&511,f=h>>9&511,c=h>>18,c&1&&(u+="1;"),c&2&&(u+="4;"),c&4&&(u+="5;"),c&8&&(u+="7;"),c&16&&(u+="8;"),l!==511&&(l=this._reduceColor(l),l<16?(l<8?l+=40:l<16&&(l-=8,l+=100),u+=l+";"):u+="48;5;"+l+";"),f!==511&&(f=this._reduceColor(f),f<16?(f<8?f+=30:f<16&&(f-=8,f+=90),u+=f+";"):u+="38;5;"+f+";"),u[u.length-1]===";"&&(u=u.slice(0,-1)),u+="m")),this.fullUnicode&&O.charWidth(i[t][1])===2&&(t===i.length-1||M[i[t+1][1]]?(n=" ",m[t][1]="\0"):(m[t][1]="\0",m[++t][1]="\0")),this.tput.strings.enter_alt_charset_mode&&!this.tput.brokenACS&&(this.tput.acscr[n]||y)?this.tput.acscr[n]?y?n=this.tput.acscr[n]:(n=this.tput.smacs()+this.tput.acscr[n],y=!0):y&&(n=this.tput.rmacs()+n,y=!1):!this.tput.unicode&&this.tput.numbers.U8!==1&&n>"~"&&(n=this.tput.utoa[n]||"?"),u+=n,a=h}a!==this.dattr&&(u+="\x1B[m"),u&&(b+=this.tput.cup(s,0)+u)}y&&(b+=this.tput.rmacs(),y=!1),b&&(d="",g="",d+=this.tput.sc(),g+=this.tput.rc(),this.program.cursorHidden||(d+=this.tput.civis(),g+=this.tput.cnorm()),this.program._write(d+b+g))},o.prototype._reduceColor=function(e){return v.reduce(e,this.tput.colors)},o.prototype.attrCode=function(e,r,t){var s=r>>18&511,i=r>>9&511,u=r&511,n,h;for(e=e.slice(2,-1).split(";"),e[0]||(e[0]="0"),h=0;h<e.length;h++)switch(n=+e[h]||0,n){case 0:u=t&511,i=t>>9&511,s=t>>18&511;break;case 1:s|=1;break;case 22:s=t>>18&511;break;case 4:s|=2;break;case 24:s=t>>18&511;break;case 5:s|=4;break;case 25:s=t>>18&511;break;case 7:s|=8;break;case 27:s=t>>18&511;break;case 8:s|=16;break;case 28:s=t>>18&511;break;case 39:i=t>>9&511;break;case 49:u=t&511;break;case 100:i=t>>9&511,u=t&511;break;default:if(n===48&&+e[h+1]==5){h+=2,u=+e[h];break}else if(n===48&&+e[h+1]==2){h+=2,u=v.match(+e[h],+e[h+1],+e[h+2]),u===-1&&(u=t&511),h+=2;break}else if(n===38&&+e[h+1]==5){h+=2,i=+e[h];break}else if(n===38&&+e[h+1]==2){h+=2,i=v.match(+e[h],+e[h+1],+e[h+2]),i===-1&&(i=t>>9&511),h+=2;break}n>=40&&n<=47?u=n-40:n>=100&&n<=107?(u=n-100,u+=8):n===49?u=t&511:n>=30&&n<=37?i=n-30:n>=90&&n<=97?(i=n-90,i+=8):n===39?i=t>>9&511:n===100&&(i=t>>9&511,u=t&511);break}return s<<18|i<<9|u},o.prototype.codeAttr=function(e){var r=e>>18&511,t=e>>9&511,s=e&511,i="";return r&1&&(i+="1;"),r&2&&(i+="4;"),r&4&&(i+="5;"),r&8&&(i+="7;"),r&16&&(i+="8;"),s!==511&&(s=this._reduceColor(s),s<16?(s<8?s+=40:s<16&&(s-=8,s+=100),i+=s+";"):i+="48;5;"+s+";"),t!==511&&(t=this._reduceColor(t),t<16?(t<8?t+=30:t<16&&(t-=8,t+=90),i+=t+";"):i+="38;5;"+t+";"),i[i.length-1]===";"&&(i=i.slice(0,-1)),"\x1B["+i+"m"},o.prototype.focusOffset=function(e){var r=this.keyable.filter(function(s){return!s.detached&&s.visible}).length;if(!(!r||!e)){var t=this.keyable.indexOf(this.focused);if(~t){if(e>0)for(;e--;)++t>this.keyable.length-1&&(t=0),(this.keyable[t].detached||!this.keyable[t].visible)&&e++;else for(e=-e;e--;)--t<0&&(t=this.keyable.length-1),(this.keyable[t].detached||!this.keyable[t].visible)&&e++;return this.keyable[t].focus()}}},o.prototype.focusPrev=o.prototype.focusPrevious=function(){return this.focusOffset(-1)},o.prototype.focusNext=function(){return this.focusOffset(1)},o.prototype.focusPush=function(e){if(e){var r=this.history[this.history.length-1];this.history.length===10&&this.history.shift(),this.history.push(e),this._focus(e,r)}},o.prototype.focusPop=function(){var e=this.history.pop();return this.history.length&&this._focus(this.history[this.history.length-1],e),e},o.prototype.saveFocus=function(){return this._savedFocus=this.focused},o.prototype.restoreFocus=function(){if(this._savedFocus)return this._savedFocus.focus(),delete this._savedFocus,this.focused},o.prototype.rewindFocus=function(){for(var e=this.history.pop(),r;this.history.length;)if(r=this.history.pop(),!r.detached&&r.visible)return this.history.push(r),this._focus(r,e),r;e&&e.emit("blur")},o.prototype._focus=function(e,r){for(var t=e;(t=t.parent)&&!t.scrollable;);if(t&&!t.detached){var s=e.screen.height-t.atop-t.itop-t.abottom-t.ibottom;e.rtop<t.childBase?(t.scrollTo(e.rtop),e.screen.render()):e.rtop+e.height-e.ibottom>t.childBase+s&&(t.scrollTo(e.rtop-(t.height-e.height)+t.itop,!0),e.screen.render())}r&&r.emit("blur",e),e.emit("focus",r)},o.prototype.__defineGetter__("focused",function(){return this.history[this.history.length-1]}),o.prototype.__defineSetter__("focused",function(e){return this.focusPush(e)}),o.prototype.clearRegion=function(e,r,t,s,i){return this.fillRegion(this.dattr," ",e,r,t,s,i)},o.prototype.fillRegion=function(e,r,t,s,i,u,n){var h=this.lines,a,f;for(t<0&&(t=0),i<0&&(i=0);i<u&&h[i];i++)for(f=t;f<s&&(a=h[i][f],!!a);f++)(n||e!==a[0]||r!==a[1])&&(h[i][f][0]=e,h[i][f][1]=r,h[i].dirty=!0)},o.prototype.key=function(){return this.program.key.apply(this,arguments)},o.prototype.onceKey=function(){return this.program.onceKey.apply(this,arguments)},o.prototype.unkey=o.prototype.removeKey=function(){return this.program.unkey.apply(this,arguments)},o.prototype.spawn=function(e,r,t){Array.isArray(r)||(t=r,r=[]);var s=this,i=s.program,u=require("child_process").spawn,n=i.mouseEnabled,h;t=t||{},t.stdio=t.stdio||"inherit",i.lsaveCursor("spawn"),i.normalBuffer(),i.showCursor(),n&&i.disableMouse();var a=i.output.write;i.output.write=function(){},i.input.pause(),i.input.setRawMode&&i.input.setRawMode(!1);var f=function(){f.done||(f.done=!0,i.input.setRawMode&&i.input.setRawMode(!0),i.input.resume(),i.output.write=a,i.alternateBuffer(),n&&(i.enableMouse(),s.options.sendFocus&&s.program.setMouse({sendFocus:!0},!0)),s.alloc(),s.render(),s.program.lrestoreCursor("spawn",!0))};return h=u(e,r,t),h.on("error",f),h.on("exit",f),h},o.prototype.exec=function(e,r,t,s){var i=this.spawn(e,r,t);return i.on("error",function(u){if(s)return s(u,!1)}),i.on("exit",function(u){if(s)return s(null,u===0)}),i},o.prototype.readEditor=function(e,r){typeof e=="string"&&(e={editor:e}),r||(r=e,e=null),r||(r=function(){}),e=e||{};var t=this,s=e.editor||process.env.EDITOR||"vi",i=e.name||process.title||"blessed",u=Math.random().toString(36).split(".").pop(),n=E.join(R.tmpdir(),i+"."+u),h=[n],a;a={stdio:"inherit",env:process.env,cwd:process.env.HOME};function f(l){return e.value?L.writeFile(n,e.value,l):l()}return f(function(l){return l?r(l):t.exec(s,h,a,function(c,b){return c?r(c):L.readFile(n,"utf8",function(d,g){return L.unlink(n,function(){return b?d?r(d):r(null,g):r(new Error("Unsuccessful."))})})})})},o.prototype.displayImage=function(e,r){if(!e)return r?r(new Error("No image.")):void 0;e=E.resolve(process.cwd(),e),~e.indexOf("://")||(e="file://"+e);var t=["w3m","-T","text/html"],s='<title>press q to exit</title><img align="center" src="'+e+'">',i={stdio:["pipe",1,2],env:process.env,cwd:process.env.HOME},u=this.spawn(t[0],t.slice(1),i);u.on("error",function(n){if(r)return r(n)}),u.on("exit",function(n){if(r)return n!==0?r(new Error("Exit Code: "+n)):r(null,n===0)}),u.stdin.write(s+` `),u.stdin.end()},o.prototype.setEffects=function(e,r,t,s,i,u){if(i){var n={};if(u&&(e[u]=n),typeof e!="function"){var h=e;e=function(){return h}}r.on(t,function(){var a=e();Object.keys(i).forEach(function(f){var l=i[f];if(l!==null&&typeof l=="object"){n[f]=n[f]||{},Object.keys(l).forEach(function(c){var b=l[c];n[f][c]=a.style[f][c],a.style[f][c]=b});return}n[f]=a.style[f],a.style[f]=l}),a.screen.render()}),r.on(s,function(){var a=e();Object.keys(i).forEach(function(f){var l=i[f];if(l!==null&&typeof l=="object"){n[f]=n[f]||{},Object.keys(l).forEach(function(c){n[f].hasOwnProperty(c)&&(a.style[f][c]=n[f][c])});return}n.hasOwnProperty(f)&&(a.style[f]=n[f])}),a.screen.render()})}},o.prototype.sigtstp=function(e){var r=this;this.program.sigtstp(function(){r.alloc(),r.render(),r.program.lrestoreCursor("pause",!0),e&&e()})},o.prototype.copyToClipboard=function(e){return this.program.copyToClipboard(e)},o.prototype.cursorShape=function(e,r){var t=this;if(this.cursor.shape=e||"block",this.cursor.blink=r||!1,this.cursor._set=!0,this.cursor.artificial){if(!this.program.hideCursor_old){var s=this.program.hideCursor;this.program.hideCursor_old=this.program.hideCursor,this.program.hideCursor=function(){s.call(t.program),t.cursor._hidden=!0,t.renders&&t.render()}}if(!this.program.showCursor_old){var i=this.program.showCursor;this.program.showCursor_old=this.program.showCursor,this.program.showCursor=function(){t.cursor._hidden=!1,t.program._exiting&&i.call(t.program),t.renders&&t.render()}}return this._cursorBlink||(this._cursorBlink=setInterval(function(){t.cursor.blink&&(t.cursor._state^=1,t.renders&&t.render())},500),this._cursorBlink.unref&&this._cursorBlink.unref()),!0}return this.program.cursorShape(this.cursor.shape,this.cursor.blink)},o.prototype.cursorColor=function(e){return this.cursor.color=e!=null?v.convert(e):null,this.cursor._set=!0,this.cursor.artificial?!0:this.program.cursorColor(v.ncolors[this.cursor.color])},o.prototype.cursorReset=o.prototype.resetCursor=function(){return this.cursor.shape="block",this.cursor.blink=!1,this.cursor.color=null,this.cursor._set=!1,this.cursor.artificial?(this.cursor.artificial=!1,this.program.hideCursor_old&&(this.program.hideCursor=this.program.hideCursor_old,delete this.program.hideCursor_old),this.program.showCursor_old&&(this.program.showCursor=this.program.showCursor_old,delete this.program.showCursor_old),this._cursorBlink&&(clearInterval(this._cursorBlink),delete this._cursorBlink),!0):this.program.cursorReset()},o.prototype._cursorAttr=function(e,r){var t=r||this.dattr,s,i;return e.shape==="line"?(t&=-261633,t|=3584,i="\u2502"):e.shape==="underline"?(t&=-261633,t|=3584,t|=2<<18):e.shape==="block"?(t&=-261633,t|=3584,t|=8<<18):typeof e.shape=="object"&&e.shape&&(s=H.prototype.sattr.call(e,e.shape),(e.shape.bold||e.shape.underline||e.shape.blink||e.shape.inverse||e.shape.invisible)&&(t&=~(511<<18),t|=(s>>18&511)<<18),e.shape.fg&&(t&=-261633,t|=(s>>9&511)<<9),e.shape.bg&&(t&=-512,t|=s&511),e.shape.ch&&(i=e.shape.ch)),e.color!=null&&(t&=-261633,t|=e.color<<9),{ch:i,attr:t}},o.prototype.screenshot=function(e,r,t,s,i){e==null&&(e=0),r==null&&(r=this.cols),t==null&&(t=0),s==null&&(s=this.rows),e<0&&(e=0),t<0&&(t=0);var u,n,h,a,f,l,c,b=this.dattr;i&&(this.dattr=i.defAttr);var d="";for(n=t;n<s&&(h=i?i.lines[n]:this.lines[n],!!h);n++){for(a="",c=this.dattr,u=e;u<r&&h[u];u++){if(l=h[u][0],f=h[u][1],l!==c&&(c!==this.dattr&&(a+="\x1B[m"),l!==this.dattr)){var g=l;i&&((g>>9&511)===257&&(g|=261632),(g&511)===256&&(g|=511)),a+=this.codeAttr(g)}this.fullUnicode&&O.charWidth(h[u][1])===2&&(u===r-1?f=" ":u++),a+=f,c=l}c!==this.dattr&&(a+="\x1B[m"),a&&(d+=(n>0?` `:"")+a)}return d=d.replace(/(?:\s*\x1b\[40m\s*\x1b\[m\s*)*$/,"")+` `,i&&(this.dattr=b),d},o.prototype._getPos=function(){return this};var M={"\u2518":!0,"\u2510":!0,"\u250C":!0,"\u2514":!0,"\u253C":!0,"\u251C":!0,"\u2524":!0,"\u2534":!0,"\u252C":!0,"\u2502":!0,"\u2500":!0},U={"\u250C":!0,"\u2514":!0,"\u253C":!0,"\u251C":!0,"\u2534":!0,"\u252C":!0,"\u2500":!0},j={"\u2510":!0,"\u250C":!0,"\u253C":!0,"\u251C":!0,"\u2524":!0,"\u252C":!0,"\u2502":!0},P={"\u2518":!0,"\u2510":!0,"\u253C":!0,"\u2524":!0,"\u2534":!0,"\u252C":!0,"\u2500":!0},z={"\u2518":!0,"\u2514":!0,"\u253C":!0,"\u251C":!0,"\u2524":!0,"\u2534":!0,"\u2502":!0},x={"0000":"","0001":"\u2502","0010":"\u2500","0011":"\u250C","0100":"\u2502","0101":"\u2502","0110":"\u2514","0111":"\u251C",1e3:"\u2500",1001:"\u2510",1010:"\u2500",1011:"\u252C",1100:"\u2518",1101:"\u2524",1110:"\u2534",1111:"\u253C"};Object.keys(x).forEach(function(e){x[parseInt(e,2)]=x[e],delete x[e]}),module.exports=o; //# sourceMappingURL=screen.js.map