UNPKG

neo-neo-bblessed

Version:

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

13 lines (12 loc) 33.7 kB
var B=require("assert"),x=require("../colors"),m=require("../unicode"),C=global.setImmediate||process.nextTick.bind(process),E=require("../helpers"),S=require("./node");function h(t){var e=this;if(!(this instanceof S))return new h(t);if(t=t||{},t.scrollable&&!this._ignore&&this.type!=="scrollable-box"){var i=require("./scrollablebox");return Object.getOwnPropertyNames(i.prototype).forEach(function(s){s!=="type"&&Object.defineProperty(this,s,Object.getOwnPropertyDescriptor(i.prototype,s))},this),this._ignore=!0,i.call(this,t),delete this._ignore,this}S.call(this,t),this.name=t.name,t.position=t.position||{left:t.left,right:t.right,top:t.top,bottom:t.bottom,width:t.width,height:t.height},(t.position.width==="shrink"||t.position.height==="shrink")&&(t.position.width==="shrink"&&delete t.position.width,t.position.height==="shrink"&&delete t.position.height,t.shrink=!0),this.position=t.position,this.noOverflow=t.noOverflow,this.dockBorders=t.dockBorders,this.shadow=t.shadow,this.style=t.style,this.style||(this.style={},this.style.fg=t.fg,this.style.bg=t.bg,this.style.bold=t.bold,this.style.underline=t.underline,this.style.blink=t.blink,this.style.inverse=t.inverse,this.style.invisible=t.invisible,this.style.transparent=t.transparent),this.hidden=t.hidden||!1,this.fixed=t.fixed||!1,this.align=t.align||"left",this.valign=t.valign||"top",this.wrap=t.wrap!==!1,this.shrink=t.shrink,this.fixed=t.fixed,this.ch=t.ch||" ",(typeof t.padding=="number"||!t.padding)&&(t.padding={left:t.padding,top:t.padding,right:t.padding,bottom:t.padding}),this.padding={left:t.padding.left||0,top:t.padding.top||0,right:t.padding.right||0,bottom:t.padding.bottom||0},this.border=t.border,this.border&&(typeof this.border=="string"&&(this.border={type:this.border}),this.border.type=this.border.type||"bg",this.border.type==="ascii"&&(this.border.type="line"),this.border.ch=this.border.ch||" ",this.style.border=this.style.border||this.border.style,this.style.border||(this.style.border={},this.style.border.fg=this.border.fg,this.style.border.bg=this.border.bg),this.border.left==null&&(this.border.left=!0),this.border.top==null&&(this.border.top=!0),this.border.right==null&&(this.border.right=!0),this.border.bottom==null&&(this.border.bottom=!0)),t.clickable&&this.screen._listenMouse(this),(t.input||t.keyable)&&this.screen._listenKeys(this),this.parseTags=t.parseTags||t.tags,this.setContent(t.content||"",!0),t.label&&this.setLabel(t.label),t.hoverText&&this.setHover(t.hoverText),this.on("newListener",function(l){l==="mouse"||l==="click"||l==="mouseover"||l==="mouseout"||l==="mousedown"||l==="mouseup"||l==="mousewheel"||l==="wheeldown"||l==="wheelup"||l==="mousemove"?e.screen._listenMouse(e):(l==="keypress"||l.indexOf("key ")===0)&&e.screen._listenKeys(e)}),this.on("resize",function(){e.parseContent()}),this.on("attach",function(){e.parseContent()}),this.on("detach",function(){delete e.lpos}),t.hoverBg!=null&&(t.hoverEffects=t.hoverEffects||{},t.hoverEffects.bg=t.hoverBg),this.style.hover&&(t.hoverEffects=this.style.hover),this.style.focus&&(t.focusEffects=this.style.focus),t.effects&&(t.effects.hover&&(t.hoverEffects=t.effects.hover),t.effects.focus&&(t.focusEffects=t.effects.focus)),[["hoverEffects","mouseover","mouseout","_htemp"],["focusEffects","focus","blur","_ftemp"]].forEach(function(s){var l=s[0],f=s[1],n=s[2],r=s[3];e.screen.setEffects(e,e,f,n,e.options[l],r)}),this.options.draggable&&(this.draggable=!0),t.focused&&this.focus()}h.prototype.__proto__=S.prototype,h.prototype.type="element",h.prototype.__defineGetter__("focused",function(){return this.screen.focused===this}),h.prototype.sattr=function(t,e,i){var s=t.bold,l=t.underline,f=t.blink,n=t.inverse,r=t.invisible;return e==null&&i==null&&(e=t.fg,i=t.bg),typeof s=="function"&&(s=s(this)),typeof l=="function"&&(l=l(this)),typeof f=="function"&&(f=f(this)),typeof n=="function"&&(n=n(this)),typeof r=="function"&&(r=r(this)),typeof e=="function"&&(e=e(this)),typeof i=="function"&&(i=i(this)),(r?16:0)<<18|(n?8:0)<<18|(f?4:0)<<18|(l?2:0)<<18|(s?1:0)<<18|x.convert(e)<<9|x.convert(i)},h.prototype.onScreenEvent=function(t,e){var i=this._slisteners=this._slisteners||[];i.push({type:t,handler:e}),this.screen.on(t,e)},h.prototype.onceScreenEvent=function(t,e){var i=this._slisteners=this._slisteners||[],s={type:t,handler:e};i.push(s),this.screen.once(t,function(){var l=i.indexOf(s);return~l&&i.splice(l,1),e.apply(this,arguments)})},h.prototype.removeScreenEvent=function(t,e){for(var i=this._slisteners=this._slisteners||[],s=0;s<i.length;s++){var l=i[s];if(l.type===t&&l.handler===e){i.splice(s,1),this._slisteners.length===0&&delete this._slisteners;break}}this.screen.removeListener(t,e)},h.prototype.free=function(){for(var t=this._slisteners=this._slisteners||[],e=0;e<t.length;e++){var i=t[e];this.screen.removeListener(i.type,i.handler)}delete this._slisteners},h.prototype.hide=function(){this.hidden||(this.clearPos(),this.hidden=!0,this.emit("hide"),this.screen.focused===this&&this.screen.rewindFocus())},h.prototype.show=function(){this.hidden&&(this.hidden=!1,this.emit("show"))},h.prototype.toggle=function(){return this.hidden?this.show():this.hide()},h.prototype.focus=function(){return this.screen.focused=this},h.prototype.setContent=function(t,e,i){e||this.clearPos(),this.content=t||"",this.parseContent(i),this.emit("set content")},h.prototype.getContent=function(){return this._clines?this._clines.fake.join(` `):""},h.prototype.setText=function(t,e){return t=t||"",t=t.replace(/\x1b\[[\d;]*m/g,""),this.setContent(t,e,!0)},h.prototype.getText=function(){return this.getContent().replace(/\x1b\[[\d;]*m/g,"")},h.prototype.parseContent=function(t){if(this.detached)return!1;var e=this.width-this.iwidth;if(this._clines==null||this._clines.width!==e||this._clines.content!==this.content){var i=this.content;return i=i.replace(/[\x00-\x08\x0b-\x0c\x0e-\x1a\x1c-\x1f\x7f]/g,"").replace(/\x1b(?!\[[\d;]*m)/g,"").replace(/\r\n|\r/g,` `).replace(/\t/g,this.screen.tabc),this.screen.fullUnicode?(i=i.replace(m.chars.all,"$1"),this.screen.program.isiTerm2&&(i=i.replace(m.chars.combining,""))):(i=i.replace(m.chars.all,"??"),i=i.replace(m.chars.combining,""),i=i.replace(m.chars.surrogate,"?")),t||(i=this._parseTags(i)),this._clines=this._wrapContent(i,e),this._clines.width=e,this._clines.content=this.content,this._clines.attr=this._parseAttr(this._clines),this._clines.ci=[],this._clines.reduce(function(s,l){return this._clines.ci.push(s),s+l.length+1}.bind(this),0),this._pcontent=this._clines.join(` `),this.emit("parsed content"),!0}return this._clines.attr=this._parseAttr(this._clines)||this._clines.attr,!1},h.prototype._parseTags=function(t){if(!this.parseTags||!/{\/?[\w\-,;!#]*}/.test(t))return t;for(var e=this.screen.program,i="",s,l=[],f=[],n=[],r,a,p,o,d;;){if(!d&&(r=/^{escape}/.exec(t))){t=t.substring(r[0].length),d=!0;continue}if(d&&(r=/^([\s\S]+?){\/escape}/.exec(t))){t=t.substring(r[0].length),i+=r[1],d=!1;continue}if(d){i+=t;break}if(r=/^{(\/?)([\w\-,;!#]*)}/.exec(t)){if(t=t.substring(r[0].length),a=r[1]==="/",p=r[2].replace(/-/g," "),p==="open"){i+="{";continue}else if(p==="close"){i+="}";continue}p.slice(-3)===" bg"?s=l:p.slice(-3)===" fg"?s=f:s=n,a?p?(o=e._attr(p,!1),o==null?i+=r[0]:(s.pop(),s.length?i+=e._attr(s[s.length-1]):i+=o)):(i+=e._attr("normal"),l.length=0,f.length=0,n.length=0):p?(o=e._attr(p),o==null?i+=r[0]:(s.push(p),i+=o)):i+=r[0];continue}if(r=/^[\s\S]+?(?={\/?[\w\-,;!#]*})/.exec(t)){t=t.substring(r[0].length),i+=r[0];continue}i+=t;break}return i},h.prototype._parseAttr=function(t){var e=this.sattr(this.style),i=e,s=[],l,f,n,r;if(!(Array.isArray(t.attr)&&t.attr.length>0&&t.attr[0]===i)){for(n=0;n<t.length;n++)for(l=t[n],s[n]=i,f=0;f<l.length;f++)l[f]==="\x1B"&&(r=/^\x1b\[[\d;]*m/.exec(l.substring(f)))&&(i=this.screen.attrCode(r[0],i,e),f+=r[0].length-1);return s}},h.prototype._align=function(t,e,i){if(!i)return t;var s=t.replace(/\x1b\[[\d;]*m/g,""),l=s.length,f=e-l;if(this.shrink&&(f=0),l===0||f<0)return t;if(i==="center")return f=Array((f/2|0)+1).join(" "),f+t+f;if(i==="right")return f=Array(f+1).join(" "),f+t;if(this.parseTags&&~t.indexOf("{|}")){var n=t.split("{|}"),r=s.split("{|}");return f=Math.max(e-r[0].length-r[1].length,0),f=Array(f+1).join(" "),n[0]+f+n[1]}return t},h.prototype._wrapContent=function(t,e){var i=this.parseTags,s=this.align,l=this.wrap,f=0,n=[],r=[],a=[],p=0,o,d,g,y,c,b,u,_,v;if(_=t.split(` `),!t)return a.push(t),a.rtof=[0],a.ftor=[[0]],a.fake=_,a.real=a,a.mwidth=0,a;this.scrollbar&&f++,this.type==="textarea"&&f++,e>f&&(e-=f);t:for(;p<_.length;p++){for(o=_[p],d=s,r.push([]),i&&((g=/^{(left|center|right)}/.exec(o))&&(o=o.substring(g[0].length),d=s=g[1]!=="left"?g[1]:null),(g=/{\/(left|center|right)}$/.exec(o))&&(o=o.slice(0,-g[0].length),s=this.align));o.length>e;){for(c=0,y=0;c<o.length;c++){for(;o[c]==="\x1B";)for(;o[c]&&o[c++]!=="m";);if(!o[c])break;if(++y===e){if(c++,!l){v=o.substring(c).match(/\x1b\[[^m]*m/g),v=v?v.join(""):"",a.push(this._align(o.substring(0,c)+v,e,d)),r[p].push(a.length-1),n.push(p);continue t}if(this.screen.fullUnicode){if(c!==o.length){m.isSurrogate(o,c)&&c--;for(var w=0,k=0;k<c;k++)m.isSurrogate(o,k)&&(w++,k++);for(c+=w,u=c;u>c-10&&u>0&&(u--,!(o[u]===" "||o[u]===""||m.isSurrogate(o,u-1)&&o[u+1]!==""||m.isCombining(o,u))););(o[u]===" "||o[u]===""||m.isSurrogate(o,u-1)&&o[u+1]!==""||m.isCombining(o,u))&&(c=u+1)}}else if(c!==o.length){for(u=c;u>c-10&&u>0&&o[--u]!==" ";);o[u]===" "&&(c=u+1)}break}}if(b=o.substring(0,c),o=o.substring(c),a.push(this._align(b,e,d)),r[p].push(a.length-1),n.push(p),o==="")continue t;if(/^(?:\x1b[\[\d;]*m)+$/.test(o)){a[a.length-1]+=o;continue t}}a.push(this._align(o,e,d)),r[p].push(a.length-1),n.push(p)}return a.rtof=n,a.ftor=r,a.fake=_,a.real=a,a.mwidth=a.reduce(function(P,L){return L=L.replace(/\x1b\[[\d;]*m/g,""),L.length>P?L.length:P},0),a},h.prototype.__defineGetter__("visible",function(){var t=this;do if(t.detached||t.hidden)return!1;while(t=t.parent);return!0}),h.prototype.__defineGetter__("_detached",function(){var t=this;do{if(t.type==="screen")return!1;if(!t.parent)return!0}while(t=t.parent);return!1}),h.prototype.enableMouse=function(){this.screen._listenMouse(this)},h.prototype.enableKeys=function(){this.screen._listenKeys(this)},h.prototype.enableInput=function(){this.screen._listenMouse(this),this.screen._listenKeys(this)},h.prototype.__defineGetter__("draggable",function(){return this._draggable===!0}),h.prototype.__defineSetter__("draggable",function(t){return t?this.enableDrag(t):this.disableDrag()}),h.prototype.enableDrag=function(t){var e=this;return this._draggable?!0:(typeof t!="function"&&(t=function(){return!0}),this.enableMouse(),this.on("mousedown",this._dragMD=function(i){e.screen._dragging||t(i)&&(e.screen._dragging=e,e._drag={x:i.x-e.aleft,y:i.y-e.atop},e.setFront())}),this.onScreenEvent("mouse",this._dragM=function(i){if(e.screen._dragging===e){if(i.action!=="mousedown"&&i.action!=="mousemove"){delete e.screen._dragging,delete e._drag;return}if(e.parent){var s=e._drag.x,l=e._drag.y,f=e.parent.aleft,n=e.parent.atop,r=i.x-f-s,a=i.y-n-l;e.position.right!=null&&(e.position.left!=null&&(e.width="100%-"+(e.parent.width-e.width)),e.position.right=null),e.position.bottom!=null&&(e.position.top!=null&&(e.height="100%-"+(e.parent.height-e.height)),e.position.bottom=null),e.rleft=r,e.rtop=a,e.screen.render()}}}),this._draggable=!0)},h.prototype.disableDrag=function(){return this._draggable?(delete this.screen._dragging,delete this._drag,this.removeListener("mousedown",this._dragMD),this.removeScreenEvent("mouse",this._dragM),this._draggable=!1):!1},h.prototype.key=function(){return this.screen.program.key.apply(this,arguments)},h.prototype.onceKey=function(){return this.screen.program.onceKey.apply(this,arguments)},h.prototype.unkey=h.prototype.removeKey=function(){return this.screen.program.unkey.apply(this,arguments)},h.prototype.setIndex=function(t){if(this.parent){t<0&&(t=this.parent.children.length+t),t=Math.max(t,0),t=Math.min(t,this.parent.children.length-1);var e=this.parent.children.indexOf(this);if(~e){var i=this.parent.children.splice(e,1)[0];this.parent.children.splice(t,0,i)}}},h.prototype.setFront=function(){return this.setIndex(-1)},h.prototype.setBack=function(){return this.setIndex(0)},h.prototype.clearPos=function(t,e){if(!this.detached){var i=this._getCoords(t);i&&this.screen.clearRegion(i.xi,i.xl,i.yi,i.yl,e)}},h.prototype.setLabel=function(t){var e=this,i=require("./box");if(typeof t=="string"&&(t={text:t}),this._label){this._label.setContent(t.text),t.side!=="right"?(this._label.rleft=2+(this.border?-1:0),this._label.position.right=void 0,this.screen.autoPadding||(this._label.rleft=2)):(this._label.rright=2+(this.border?-1:0),this._label.position.left=void 0,this.screen.autoPadding||(this._label.rright=2));return}this._label=new i({screen:this.screen,parent:this,content:t.text,top:-this.itop,tags:this.parseTags,shrink:!0,style:this.style.label}),t.side!=="right"?this._label.rleft=2-this.ileft:this._label.rright=2-this.iright,this._label._isLabel=!0,this.screen.autoPadding||(t.side!=="right"?this._label.rleft=2:this._label.rright=2,this._label.rtop=0);var s=function(){e._label.rtop=(e.childBase||0)-e.itop,e.screen.autoPadding||(e._label.rtop=e.childBase||0),e.screen.render()};this.on("scroll",this._labelScroll=function(){s()}),this.on("resize",this._labelResize=function(){C(function(){s()})})},h.prototype.removeLabel=function(){this._label&&(this.removeListener("scroll",this._labelScroll),this.removeListener("resize",this._labelResize),this._label.detach(),delete this._labelScroll,delete this._labelResize,delete this._label)},h.prototype.setHover=function(t){typeof t=="string"&&(t={text:t}),this._hoverOptions=t,this.enableMouse(),this.screen._initHover()},h.prototype.removeHover=function(){delete this._hoverOptions,!(!this.screen._hoverText||this.screen._hoverText.detached)&&(this.screen._hoverText.detach(),this.screen.render())},h.prototype._getPos=function(){var t=this.lpos;return B.ok(t),t.aleft!=null||(t.aleft=t.xi,t.atop=t.yi,t.aright=this.screen.cols-t.xl,t.abottom=this.screen.rows-t.yl,t.width=t.xl-t.xi,t.height=t.yl-t.yi),t},h.prototype._getWidth=function(t){var e=t?this.parent._getPos():this.parent,i=this.position.width,s,l;return typeof i=="string"?(i==="half"&&(i="50%"),l=i.split(/(?=\+|-)/),i=l[0],i=+i.slice(0,-1)/100,i=e.width*i|0,i+=+(l[1]||0),i):(i==null&&(s=this.position.left||0,typeof s=="string"&&(s==="center"&&(s="50%"),l=s.split(/(?=\+|-)/),s=l[0],s=+s.slice(0,-1)/100,s=e.width*s|0,s+=+(l[1]||0)),i=e.width-(this.position.right||0)-s,this.screen.autoPadding&&((this.position.left!=null||this.position.right==null)&&this.position.left!=="center"&&(i-=this.parent.ileft),i-=this.parent.iright)),i)},h.prototype.__defineGetter__("width",function(){return this._getWidth(!1)}),h.prototype._getHeight=function(t){var e=t?this.parent._getPos():this.parent,i=this.position.height,s,l;return typeof i=="string"?(i==="half"&&(i="50%"),l=i.split(/(?=\+|-)/),i=l[0],i=+i.slice(0,-1)/100,i=e.height*i|0,i+=+(l[1]||0),i):(i==null&&(s=this.position.top||0,typeof s=="string"&&(s==="center"&&(s="50%"),l=s.split(/(?=\+|-)/),s=l[0],s=+s.slice(0,-1)/100,s=e.height*s|0,s+=+(l[1]||0)),i=e.height-(this.position.bottom||0)-s,this.screen.autoPadding&&((this.position.top!=null||this.position.bottom==null)&&this.position.top!=="center"&&(i-=this.parent.itop),i-=this.parent.ibottom)),i)},h.prototype.__defineGetter__("height",function(){return this._getHeight(!1)}),h.prototype._getLeft=function(t){var e=t?this.parent._getPos():this.parent,i=this.position.left||0,s;return typeof i=="string"&&(i==="center"&&(i="50%"),s=i.split(/(?=\+|-)/),i=s[0],i=+i.slice(0,-1)/100,i=e.width*i|0,i+=+(s[1]||0),this.position.left==="center"&&(i-=this._getWidth(t)/2|0)),this.position.left==null&&this.position.right!=null?this.screen.cols-this._getWidth(t)-this._getRight(t):(this.screen.autoPadding&&(this.position.left!=null||this.position.right==null)&&this.position.left!=="center"&&(i+=this.parent.ileft),(e.aleft||0)+i)},h.prototype.__defineGetter__("aleft",function(){return this._getLeft(!1)}),h.prototype._getRight=function(t){var e=t?this.parent._getPos():this.parent,i;return this.position.right==null&&this.position.left!=null?(i=this.screen.cols-(this._getLeft(t)+this._getWidth(t)),this.screen.autoPadding&&(i+=this.parent.iright),i):(i=(e.aright||0)+(this.position.right||0),this.screen.autoPadding&&(i+=this.parent.iright),i)},h.prototype.__defineGetter__("aright",function(){return this._getRight(!1)}),h.prototype._getTop=function(t){var e=t?this.parent._getPos():this.parent,i=this.position.top||0,s;return typeof i=="string"&&(i==="center"&&(i="50%"),s=i.split(/(?=\+|-)/),i=s[0],i=+i.slice(0,-1)/100,i=e.height*i|0,i+=+(s[1]||0),this.position.top==="center"&&(i-=this._getHeight(t)/2|0)),this.position.top==null&&this.position.bottom!=null?this.screen.rows-this._getHeight(t)-this._getBottom(t):(this.screen.autoPadding&&(this.position.top!=null||this.position.bottom==null)&&this.position.top!=="center"&&(i+=this.parent.itop),(e.atop||0)+i)},h.prototype.__defineGetter__("atop",function(){return this._getTop(!1)}),h.prototype._getBottom=function(t){var e=t?this.parent._getPos():this.parent,i;return this.position.bottom==null&&this.position.top!=null?(i=this.screen.rows-(this._getTop(t)+this._getHeight(t)),this.screen.autoPadding&&(i+=this.parent.ibottom),i):(i=(e.abottom||0)+(this.position.bottom||0),this.screen.autoPadding&&(i+=this.parent.ibottom),i)},h.prototype.__defineGetter__("abottom",function(){return this._getBottom(!1)}),h.prototype.__defineGetter__("rleft",function(){return this.aleft-this.parent.aleft}),h.prototype.__defineGetter__("rright",function(){return this.aright-this.parent.aright}),h.prototype.__defineGetter__("rtop",function(){return this.atop-this.parent.atop}),h.prototype.__defineGetter__("rbottom",function(){return this.abottom-this.parent.abottom}),h.prototype.__defineSetter__("width",function(t){if(this.position.width!==t)return/^\d+$/.test(t)&&(t=+t),this.emit("resize"),this.clearPos(),this.position.width=t}),h.prototype.__defineSetter__("height",function(t){if(this.position.height!==t)return/^\d+$/.test(t)&&(t=+t),this.emit("resize"),this.clearPos(),this.position.height=t}),h.prototype.__defineSetter__("aleft",function(t){var e;if(typeof t=="string"&&(t==="center"?(t=this.screen.width/2|0,t-=this.width/2|0):(e=t.split(/(?=\+|-)/),t=e[0],t=+t.slice(0,-1)/100,t=this.screen.width*t|0,t+=+(e[1]||0))),t-=this.parent.aleft,this.position.left!==t)return this.emit("move"),this.clearPos(),this.position.left=t}),h.prototype.__defineSetter__("aright",function(t){if(t-=this.parent.aright,this.position.right!==t)return this.emit("move"),this.clearPos(),this.position.right=t}),h.prototype.__defineSetter__("atop",function(t){var e;if(typeof t=="string"&&(t==="center"?(t=this.screen.height/2|0,t-=this.height/2|0):(e=t.split(/(?=\+|-)/),t=e[0],t=+t.slice(0,-1)/100,t=this.screen.height*t|0,t+=+(e[1]||0))),t-=this.parent.atop,this.position.top!==t)return this.emit("move"),this.clearPos(),this.position.top=t}),h.prototype.__defineSetter__("abottom",function(t){if(t-=this.parent.abottom,this.position.bottom!==t)return this.emit("move"),this.clearPos(),this.position.bottom=t}),h.prototype.__defineSetter__("rleft",function(t){if(this.position.left!==t)return/^\d+$/.test(t)&&(t=+t),this.emit("move"),this.clearPos(),this.position.left=t}),h.prototype.__defineSetter__("rright",function(t){if(this.position.right!==t)return this.emit("move"),this.clearPos(),this.position.right=t}),h.prototype.__defineSetter__("rtop",function(t){if(this.position.top!==t)return/^\d+$/.test(t)&&(t=+t),this.emit("move"),this.clearPos(),this.position.top=t}),h.prototype.__defineSetter__("rbottom",function(t){if(this.position.bottom!==t)return this.emit("move"),this.clearPos(),this.position.bottom=t}),h.prototype.__defineGetter__("ileft",function(){return(this.border?1:0)+this.padding.left}),h.prototype.__defineGetter__("itop",function(){return(this.border?1:0)+this.padding.top}),h.prototype.__defineGetter__("iright",function(){return(this.border?1:0)+this.padding.right}),h.prototype.__defineGetter__("ibottom",function(){return(this.border?1:0)+this.padding.bottom}),h.prototype.__defineGetter__("iwidth",function(){return(this.border?2:0)+this.padding.left+this.padding.right}),h.prototype.__defineGetter__("iheight",function(){return(this.border?2:0)+this.padding.top+this.padding.bottom}),h.prototype.__defineGetter__("tpadding",function(){return this.padding.left+this.padding.top+this.padding.right+this.padding.bottom}),h.prototype.__defineGetter__("left",function(){return this.rleft}),h.prototype.__defineGetter__("right",function(){return this.rright}),h.prototype.__defineGetter__("top",function(){return this.rtop}),h.prototype.__defineGetter__("bottom",function(){return this.rbottom}),h.prototype.__defineSetter__("left",function(t){return this.rleft=t}),h.prototype.__defineSetter__("right",function(t){return this.rright=t}),h.prototype.__defineSetter__("top",function(t){return this.rtop=t}),h.prototype.__defineSetter__("bottom",function(t){return this.rbottom=t}),h.prototype._getShrinkBox=function(t,e,i,s,l){if(!this.children.length)return{xi:t,xl:t+1,yi:i,yl:i+1};var f,n,r,a=t,p=t+1,o=i,d=i+1,g;for(l&&(g=this.lpos,this.lpos={xi:t,xl:e,yi:i,yl:s}),f=0;f<this.children.length;f++)n=this.children[f],r=n._getCoords(l),r&&(n.position.left==null&&n.position.right!=null&&(r.xl=t+(r.xl-r.xi),r.xi=t,this.screen.autoPadding&&(r.xl+=this.ileft,r.xi+=this.ileft)),n.position.top==null&&n.position.bottom!=null&&(r.yl=i+(r.yl-r.yi),r.yi=i,this.screen.autoPadding&&(r.yl+=this.itop,r.yi+=this.itop)),r.xi<a&&(a=r.xi),r.xl>p&&(p=r.xl),r.yi<o&&(o=r.yi),r.yl>d&&(d=r.yl));return l&&(this.lpos=g),this.position.width==null&&(this.position.left==null||this.position.right==null)&&(this.position.left==null&&this.position.right!=null?(t=e-(p-a),this.screen.autoPadding?t-=this.ileft:t-=this.padding.left+this.padding.right):(e=p,this.screen.autoPadding?e+=this.iright:(e+=this.padding.left+this.padding.right,this.type==="list-table"&&(e-=this.padding.left+this.padding.right,e+=this.iright)))),this.position.height==null&&(this.position.top==null||this.position.bottom==null)&&(!this.scrollable||this._isList)&&(this._isList&&(o=0-this.itop,d=this.items.length+this.ibottom),this.position.top==null&&this.position.bottom!=null?(i=s-(d-o),this.screen.autoPadding?i-=this.itop:i-=this.padding.top+this.padding.bottom):(s=d,this.screen.autoPadding?s+=this.ibottom:s+=this.padding.top+this.padding.bottom)),{xi:t,xl:e,yi:i,yl:s}},h.prototype._getShrinkContent=function(t,e,i,s){var l=this._clines.length,f=this._clines.mwidth||1;return this.position.width==null&&(this.position.left==null||this.position.right==null)&&(this.position.left==null&&this.position.right!=null?t=e-f-this.iwidth:e=t+f+this.iwidth),this.position.height==null&&(this.position.top==null||this.position.bottom==null)&&(!this.scrollable||this._isList)&&(this.position.top==null&&this.position.bottom!=null?i=s-l-this.iheight:s=i+l+this.iheight),{xi:t,xl:e,yi:i,yl:s}},h.prototype._getShrink=function(t,e,i,s,l){var f=this._getShrinkBox(t,e,i,s,l),n=this._getShrinkContent(t,e,i,s,l),r=e,a=s;return f.xl-f.xi>n.xl-n.xi?(t=f.xi,e=f.xl):(t=n.xi,e=n.xl),f.yl-f.yi>n.yl-n.yi?(i=f.yi,s=f.yl):(i=n.yi,s=n.yl),e<r&&this.position.left==="center"&&(r=(r-e)/2|0,t+=r,e+=r),s<a&&this.position.top==="center"&&(a=(a-s)/2|0,i+=a,s+=a),{xi:t,xl:e,yi:i,yl:s}},h.prototype._getCoords=function(t,e){if(!this.hidden){var i=this._getLeft(t),s=i+this._getWidth(t),l=this._getTop(t),f=l+this._getHeight(t),n=this.childBase||0,r=this,a=this.fixed,p,o,d,g,y,c,b,u;for(this.shrink&&(p=this._getShrink(i,s,l,f,t),i=p.xi,s=p.xl,l=p.yi,f=p.yl);r=r.parent;)if(r.scrollable){if(a){a=!1;continue}break}var _=r;if(r&&!e){if(b=_.lpos,!b)return;if(l-=b.base,f-=b.base,u=_.border?1:0,this._isLabel&&(u=0),l<b.yi+u){if(f-1<b.yi+u)return;y=!0,o=b.yi-l,this.border&&o--,_.border&&o++,n+=o,l+=o}else if(f>b.yl-u){if(l>b.yl-1-u)return;c=!0,o=f-b.yl,this.border&&o--,_.border&&o++,f-=o}if(l>=f||(i<r.lpos.xi&&(i=r.lpos.xi,d=!0,this.border&&i--,_.border&&i++),s>r.lpos.xl&&(s=r.lpos.xl,g=!0,this.border&&s++,_.border&&s--),i>=s))return}return this.noOverflow&&this.parent.lpos&&(i<this.parent.lpos.xi+this.parent.ileft&&(i=this.parent.lpos.xi+this.parent.ileft),s>this.parent.lpos.xl-this.parent.iright&&(s=this.parent.lpos.xl-this.parent.iright),l<this.parent.lpos.yi+this.parent.itop&&(l=this.parent.lpos.yi+this.parent.itop),f>this.parent.lpos.yl-this.parent.ibottom&&(f=this.parent.lpos.yl-this.parent.ibottom)),{xi:i,xl:s,yi:l,yl:f,base:n,noleft:d,noright:g,notop:y,nobot:c,renders:this.screen.renders}}},h.prototype.render=function(){this._emit("prerender"),this.parseContent();var t=this._getCoords(!0);if(!t){delete this.lpos;return}if(t.xl-t.xi<=0){t.xl=Math.max(t.xl,t.xi);return}if(t.yl-t.yi<=0){t.yl=Math.max(t.yl,t.yi);return}var e=this.screen.lines,i=t.xi,s=t.xl,l=t.yi,f=t.yl,n,r,a,p,o,d=this._pcontent,g=this._clines.ci[t.base],y,c,b,u,_,v=this.ch;if(t.base>=this._clines.ci.length&&(g=this._pcontent.length),this.lpos=t,this.border&&this.border.type==="line"&&(this.screen._borderStops[t.yi]=!0,this.screen._borderStops[t.yl-1]=!0),c=this.sattr(this.style),p=c,g>0&&(p=this._clines.attr[Math.min(t.base,this._clines.length-1)]),this.border&&(i++,s--,l++,f--),this.tpadding||this.valign&&this.valign!=="top")if(this.style.transparent)for(r=Math.max(l,0);r<f&&e[r];r++)for(n=Math.max(i,0);n<s&&e[r][n];n++)e[r][n][0]=x.blend(p,e[r][n][0]),e[r].dirty=!0;else this.screen.fillRegion(c,v,i,s,l,f);for(this.tpadding&&(i+=this.padding.left,s-=this.padding.right,l+=this.padding.top,f-=this.padding.bottom),(this.valign==="middle"||this.valign==="bottom")&&(u=f-l,this._clines.length<u&&(this.valign==="middle"?(u=u/2|0,u-=this._clines.length/2|0):this.valign==="bottom"&&(u-=this._clines.length),g-=u*(s-i))),r=l;r<f;r++){if(!e[r]){if(r>=this.screen.height||f<this.ibottom)break;continue}for(n=i;n<s;n++){if(a=e[r][n],!a){if(n>=this.screen.width||s<this.iright)break;continue}for(o=d[g++]||v;o==="\x1B"&&(b=/^\x1b\[[\d;]*m/.exec(d.substring(g-1)));)g+=b[0].length-1,p=this.screen.attrCode(b[0],p,c),this.parent._isList&&this.parent.interactive&&this.parent.items[this.parent.selected]===this&&this.parent.options.invertSelected!==!1&&(p=p&-261633|c&261632),o=d[g]||v,g++;if(o===" "&&(o=v),o===` `){if(n===i&&r!==l&&d[g-2]!==` `){n--;continue}for(o=v;n<s&&(a=e[r][n],!!a);n++)this.style.transparent?(e[r][n][0]=x.blend(p,e[r][n][0]),d[g]&&(e[r][n][1]=o),e[r].dirty=!0):(p!==a[0]||o!==a[1])&&(e[r][n][0]=p,e[r][n][1]=o,e[r].dirty=!0);continue}if(this.screen.fullUnicode&&d[g-1]){var w=m.codePointAt(d,g-1);if(m.combining[w]){w>65535&&(o=d[g-1]+d[g],g++),n-1>=i?e[r][n-1][1]+=o:r-1>=l&&(e[r-1][s-1][1]+=o),n--;continue}w>65535&&(o=d[g-1]+d[g],g++)}this._noFill||(this.style.transparent?(e[r][n][0]=x.blend(p,e[r][n][0]),d[g]&&(e[r][n][1]=o),e[r].dirty=!0):(p!==a[0]||o!==a[1])&&(e[r][n][0]=p,e[r][n][1]=o,e[r].dirty=!0))}}if(this.scrollbar&&(_=Math.max(this._clines.length,this._scrollBottom())),(t.notop||t.nobot)&&(_=-1/0),this.scrollbar&&f-l<_&&(n=s-1,this.scrollbar.ignoreBorder&&this.border&&n++,this.alwaysScroll?r=this.childBase/(_-(f-l)):r=(this.childBase+this.childOffset)/(_-1),r=l+((f-l)*r|0),r>=f&&(r=f-1),a=e[r]&&e[r][n],a&&(this.track&&(o=this.track.ch||" ",p=this.sattr(this.style.track,this.style.track.fg||this.style.fg,this.style.track.bg||this.style.bg),this.screen.fillRegion(p,o,n,n+1,l,f)),o=this.scrollbar.ch||" ",p=this.sattr(this.style.scrollbar,this.style.scrollbar.fg||this.style.fg,this.style.scrollbar.bg||this.style.bg),(p!==a[0]||o!==a[1])&&(e[r][n][0]=p,e[r][n][1]=o,e[r].dirty=!0))),this.border&&(i--,s++,l--,f++),this.tpadding&&(i-=this.padding.left,s+=this.padding.right,l-=this.padding.top,f+=this.padding.bottom),this.border){for(y=this.sattr(this.style.border),r=l,t.notop&&(r=-1),n=i;n<s&&e[r];n++)if(!(t.noleft&&n===i)&&!(t.noright&&n===s-1)&&(a=e[r][n],!!a)){if(this.border.type==="line")if(n===i)if(o="\u250C",this.border.left)this.border.top||(o="\u2502");else if(this.border.top)o="\u2500";else continue;else if(n===s-1)if(o="\u2510",this.border.right)this.border.top||(o="\u2502");else if(this.border.top)o="\u2500";else continue;else o="\u2500";else this.border.type==="bg"&&(o=this.border.ch);if(!this.border.top&&n!==i&&n!==s-1&&(o=" ",c!==a[0]||o!==a[1])){e[r][n][0]=c,e[r][n][1]=o,e[r].dirty=!0;continue}(y!==a[0]||o!==a[1])&&(e[r][n][0]=y,e[r][n][1]=o,e[r].dirty=!0)}for(r=l+1;r<f-1;r++)e[r]&&(a=e[r][i],a&&(this.border.left?(this.border.type==="line"?o="\u2502":this.border.type==="bg"&&(o=this.border.ch),t.noleft||(y!==a[0]||o!==a[1])&&(e[r][i][0]=y,e[r][i][1]=o,e[r].dirty=!0)):(o=" ",(c!==a[0]||o!==a[1])&&(e[r][i][0]=c,e[r][i][1]=o,e[r].dirty=!0))),a=e[r][s-1],a&&(this.border.right?(this.border.type==="line"?o="\u2502":this.border.type==="bg"&&(o=this.border.ch),t.noright||(y!==a[0]||o!==a[1])&&(e[r][s-1][0]=y,e[r][s-1][1]=o,e[r].dirty=!0)):(o=" ",(c!==a[0]||o!==a[1])&&(e[r][s-1][0]=c,e[r][s-1][1]=o,e[r].dirty=!0))));for(r=f-1,t.nobot&&(r=-1),n=i;n<s&&e[r];n++)if(!(t.noleft&&n===i)&&!(t.noright&&n===s-1)&&(a=e[r][n],!!a)){if(this.border.type==="line")if(n===i)if(o="\u2514",this.border.left)this.border.bottom||(o="\u2502");else if(this.border.bottom)o="\u2500";else continue;else if(n===s-1)if(o="\u2518",this.border.right)this.border.bottom||(o="\u2502");else if(this.border.bottom)o="\u2500";else continue;else o="\u2500";else this.border.type==="bg"&&(o=this.border.ch);if(!this.border.bottom&&n!==i&&n!==s-1){o=" ",(c!==a[0]||o!==a[1])&&(e[r][n][0]=c,e[r][n][1]=o,e[r].dirty=!0);continue}(y!==a[0]||o!==a[1])&&(e[r][n][0]=y,e[r][n][1]=o,e[r].dirty=!0)}}if(this.shadow){for(r=Math.max(l+1,0);r<f+1&&e[r];r++)for(n=s;n<s+2&&e[r][n];n++)e[r][n][0]=x.blend(e[r][n][0]),e[r].dirty=!0;for(r=f;r<f+1&&e[r];r++)for(n=Math.max(i+1,0);n<s&&e[r][n];n++)e[r][n][0]=x.blend(e[r][n][0]),e[r].dirty=!0}return this.children.forEach(function(k){k.screen._ci!==-1&&(k.index=k.screen._ci++),k.render()}),this._emit("render",[t]),t},h.prototype._render=h.prototype.render,h.prototype.insertLine=function(t,e){for(typeof e=="string"&&(e=e.split(` `)),(t!==t||t==null)&&(t=this._clines.ftor.length),t=Math.max(t,0);this._clines.fake.length<t;)this._clines.fake.push(""),this._clines.ftor.push([this._clines.push("")-1]),this._clines.rtof(this._clines.fake.length-1);var i=this._clines.length,s,l;t>=this._clines.ftor.length?(l=this._clines.ftor[this._clines.ftor.length-1],l=l[l.length-1]+1):l=this._clines.ftor[t][0];for(var f=0;f<e.length;f++)this._clines.fake.splice(t+f,0,e[f]);if(this.setContent(this._clines.fake.join(` `),!0),s=this._clines.length-i,s>0){var n=this._getCoords();if(!n)return;var r=n.yl-n.yi-this.iheight,a=this.childBase||0,p=l>=a&&l-a<r;n&&p&&this.screen.cleanSides(this)&&this.screen.insertLine(s,n.yi+this.itop+l-a,n.yi,n.yl-this.ibottom-1)}},h.prototype.deleteLine=function(t,e){e=e||1,(t!==t||t==null)&&(t=this._clines.ftor.length-1),t=Math.max(t,0),t=Math.min(t,this._clines.ftor.length-1);for(var i=this._clines.length,s,l=this._clines.ftor[t][0];e--;)this._clines.fake.splice(t,1);this.setContent(this._clines.fake.join(` `),!0),s=i-this._clines.length;var f=0;if(s>0){var n=this._getCoords();if(!n)return;f=n.yl-n.yi-this.iheight;var r=this.childBase||0,a=l>=r&&l-r<f;n&&a&&this.screen.cleanSides(this)&&this.screen.deleteLine(s,n.yi+this.itop+l-r,n.yi,n.yl-this.ibottom-1)}this._clines.length<f&&this.clearPos()},h.prototype.insertTop=function(t){var e=this._clines.rtof[this.childBase||0];return this.insertLine(e,t)},h.prototype.insertBottom=function(t){var e=(this.childBase||0)+this.height-this.iheight,i=Math.min(e,this._clines.length),s=this._clines.rtof[i-1]+1;return this.insertLine(s,t)},h.prototype.deleteTop=function(t){var e=this._clines.rtof[this.childBase||0];return this.deleteLine(e,t)},h.prototype.deleteBottom=function(t){var e=(this.childBase||0)+this.height-1-this.iheight,i=Math.min(e,this._clines.length-1),s=this._clines.rtof[i];return t=t||1,this.deleteLine(s-(t-1),t)},h.prototype.setLine=function(t,e){for(t=Math.max(t,0);this._clines.fake.length<t;)this._clines.fake.push("");return this._clines.fake[t]=e,this.setContent(this._clines.fake.join(` `),!0)},h.prototype.setBaseLine=function(t,e){var i=this._clines.rtof[this.childBase||0];return this.setLine(i+t,e)},h.prototype.getLine=function(t){return t=Math.max(t,0),t=Math.min(t,this._clines.fake.length-1),this._clines.fake[t]},h.prototype.getBaseLine=function(t){var e=this._clines.rtof[this.childBase||0];return this.getLine(e+t)},h.prototype.clearLine=function(t){return t=Math.min(t,this._clines.fake.length-1),this.setLine(t,"")},h.prototype.clearBaseLine=function(t){var e=this._clines.rtof[this.childBase||0];return this.clearLine(e+t)},h.prototype.unshiftLine=function(t){return this.insertLine(0,t)},h.prototype.shiftLine=function(t){return this.deleteLine(0,t)},h.prototype.pushLine=function(t){return this.content?this.insertLine(this._clines.fake.length,t):this.setLine(0,t)},h.prototype.popLine=function(t){return this.deleteLine(this._clines.fake.length-1,t)},h.prototype.getLines=function(){return this._clines.fake.slice()},h.prototype.getScreenLines=function(){return this._clines.slice()},h.prototype.strWidth=function(t){return t=this.parseTags?E.stripTags(t):t,this.screen.fullUnicode?m.strWidth(t):E.dropUnicode(t).length},h.prototype.screenshot=function(t,e,i,s){return t=this.lpos.xi+this.ileft+(t||0),e!=null?e=this.lpos.xi+this.ileft+(e||0):e=this.lpos.xl-this.iright,i=this.lpos.yi+this.itop+(i||0),s!=null?s=this.lpos.yi+this.itop+(s||0):s=this.lpos.yl-this.ibottom,this.screen.screenshot(t,e,i,s)},module.exports=h; //# sourceMappingURL=element.js.map