neo-neo-bblessed
Version:
A fork of neo-blessed (which is a fork of blessed) with bug fixes and maintenance.
5 lines (3 loc) • 3.59 kB
JavaScript
var p=require("./node"),y=require("./box");function d(s){var a=this;if(!(this instanceof p))return new d(s);s=s||{},s.shrink=!0,s.style=s.style||{},s.style.border=s.style.border||{},s.style.header=s.style.header||{},s.style.cell=s.style.cell||{},s.align=s.align||"center",delete s.height,y.call(this,s),this.pad=s.pad!=null?s.pad:2,this.setData(s.rows||s.data),this.on("attach",function(){a.setContent(""),a.setData(a.rows)}),this.on("resize",function(){a.setContent(""),a.setData(a.rows),a.screen.render()})}d.prototype.__proto__=y.prototype,d.prototype.type="table",d.prototype._calculateMaxes=function(){var s=this,a=[];if(!this.detached){this.rows=this.rows||[],this.rows.forEach(function(e){e.forEach(function(l,b){var h=s.strWidth(l);(!a[b]||a[b]<h)&&(a[b]=h)})});var t=a.reduce(function(e,l){return e+l},0);if(t+=a.length+1,this.width<t&&delete this.position.width,this.position.width!=null){var i=this.width-t,r=i/a.length|0,n=i%a.length;a=a.map(function(e,l){return l===a.length-1?e+r+n:e+r})}else a=a.map(function(e){return e+s.pad});return this._maxes=a}},d.prototype.setRows=d.prototype.setData=function(s){var a=this,t="",i=this.align;this.rows=s||[],this._calculateMaxes(),this._maxes&&(this.rows.forEach(function(r,n){var e=n===a.rows.length-1;r.forEach(function(l,b){var h=a._maxes[b],f=a.strWidth(l);for(b!==0&&(t+=" ");f<h;)i==="center"?(l=" "+l+" ",f+=2):i==="left"?(l=l+" ",f+=1):i==="right"&&(l=" "+l,f+=1);f>h&&(i==="center"?(l=l.substring(1),f--):i==="left"?(l=l.slice(0,-1),f--):i==="right"&&(l=l.substring(1),f--)),t+=l}),e||(t+=`
`)}),delete this.align,this.setContent(t),this.align=i)},d.prototype.render=function(){var s=this,a=this._render();if(a){if(this._calculateMaxes(),!this._maxes)return a;for(var t=this.screen.lines,i=a.xi,r=a.yi,n,e,l,b=this.sattr(this.style),h=this.sattr(this.style.header),f=this.sattr(this.style.cell),o=this.sattr(this.style.border),v=a.xl-a.xi-this.iright,w=a.yl-a.yi-this.ibottom,u=this.itop;u<w&&t[r+u];u++)for(var c=this.ileft;c<v&&t[r+u][i+c];c++)t[r+u][i+c][0]===b&&(u===this.itop?t[r+u][i+c][0]=h:t[r+u][i+c][0]=f,t[r+u].dirty=!0);if(!this.border||this.options.noCellBorders)return a;for(e=0,l=0;l<s.rows.length+1&&t[r+e];l++)n=0,s._maxes.forEach(function(m,g){if(n+=m,g===0){if(!t[r+e][i+0])return;e===0||e/2===s.rows.length?t[r+e][i+0][0]=o:(t[r+e][i+0][0]=o,t[r+e][i+0][1]="\u251C",s.border.left||(t[r+e][i+0][1]="\u2500")),t[r+e].dirty=!0}else if(g===s._maxes.length-1){if(!t[r+e][i+n+1])return;e===0||e/2===s.rows.length?(n++,t[r+e][i+n][0]=o):(n++,t[r+e][i+n][0]=o,t[r+e][i+n][1]="\u2524",s.border.right||(t[r+e][i+n][1]="\u2500")),t[r+e].dirty=!0;return}if(t[r+e][i+n+1]){if(e===0)n++,t[r+e][i+n][0]=o,t[r+e][i+n][1]="\u252C",s.border.top||(t[r+e][i+n][1]="\u2502");else if(e/2===s.rows.length)n++,t[r+e][i+n][0]=o,t[r+e][i+n][1]="\u2534",s.border.bottom||(t[r+e][i+n][1]="\u2502");else{if(s.options.fillCellBorders){var x=(e<=2?h:f)&511;n++,t[r+e][i+n][0]=o&-512|x}else n++,t[r+e][i+n][0]=o;t[r+e][i+n][1]="\u253C"}t[r+e].dirty=!0}}),e+=2;for(e=1;e<s.rows.length*2&&t[r+e];e++)n=0,s._maxes.slice(0,-1).forEach(function(m){if(n+=m,!!t[r+e][i+n+1])if(e%2!==0){if(s.options.fillCellBorders){var g=(e<=2?h:f)&511;n++,t[r+e][i+n][0]=o&-512|g}else n++,t[r+e][i+n][0]=o;t[r+e][i+n][1]="\u2502",t[r+e].dirty=!0}else n++}),n=1,s._maxes.forEach(function(m){for(;m--;){if(e%2===0){if(!t[r+e]||!t[r+e][i+n+1])break;if(s.options.fillCellBorders){var g=(e<=2?h:f)&511;t[r+e][i+n][0]=o&-512|g}else t[r+e][i+n][0]=o;t[r+e][i+n][1]="\u2500",t[r+e].dirty=!0}n++}n++});return a}},module.exports=d;
//# sourceMappingURL=table.js.map