replace Text with Box element .shrink=true.

This commit is contained in:
Christopher Jeffrey 2013-06-14 03:35:45 -05:00
parent b0b949b301
commit 4f199c26aa
1 changed files with 3 additions and 143 deletions

View File

@ -1232,151 +1232,11 @@ outer:
*/
function Text(options) {
Element.call(this, options);
this.full = options.full;
this.align = options.align || 'left';
options.shrink = true;
Box.call(this, options);
}
Text.prototype.__proto__ = Element.prototype;
// TODO: Remove duplication of code by reusing box.render
// better. Possibly remove Text altogether.
// TODO: Maybe just remove escape code and newline handling from here.
Text.prototype.render = function(stop) {
// NOTE: Maybe move this `hidden` check down below `stop` check and return `ret`.
if (this.hidden) return;
var lines = this.screen.lines
, xi_ = this.left
, xi
, xl = this.screen.cols - this.right
, yi = this.top
, yl = this.screen.rows - this.bottom
, cell
, attr
, ch
, content = this._pcontent || this.content
, ci = this.contentIndex || 0
, cl = content.length
, ended = -1
, dattr
, c;
if (this.position.width) {
xl = xi_ + this.width;
}
if (this.position.height) {
yl = yi + this.height;
}
if (this.full) {
//xl -= this.parent.border ? 2 : 0;
}
if (this.parent.childBase != null && ~this.parent.items.indexOf(this)) {
var rtop = this.rtop - (this.parent.border ? 1 : 0)
, visible = this.parent.height - (this.parent.border ? 2 : 0);
yi -= this.parent.childBase;
yl = Math.min(yl, this.screen.rows - this.parent.bottom - (this.parent.border ? 1 : 0));
if (rtop - this.parent.childBase < 0) {
return;
}
if (rtop - this.parent.childBase >= visible) {
return;
}
}
if (this.align === 'center' || this.align === 'right') {
//var width = this.width - (this.border ? 2 : 0);
var width = this.width;
var ncontent = content.split('\n')[0].replace(/\x1b\[[^m]*m/g, '');
if (ncontent.length < width) {
var s = width - ncontent.length;
if (this.align === 'center') {
//xi_ += s / 2 | 0;
content = Array(((s / 2 | 0) - (this.border ? 1 : 0)) + 1).join(' ') + content;
} else {
//xi_ += s;
content = Array((s - (this.border ? 2 : 0)) + 1).join(' ') + content;
}
}
}
var ret = {
xi: xi_,
xl: xl,
yi: yi,
yl: yl
};
if (stop) return ret;
dattr = sattr(this, this.fg, this.bg);
attr = dattr;
for (; yi < yl; yi++) {
if (!lines[yi]) break;
for (xi = xi_; xi < xl; xi++) {
cell = lines[yi][xi];
if (!cell) break;
ch = content[ci++];
// Handle escape codes.
while (ch === '\x1b') {
if (c = /^\x1b\[[\d;]*m/.exec(content.substring(ci - 1))) {
ci += c[0].length - 1;
attr = attrCode(c[0], attr);
ch = content[ci];
ci++;
} else {
break;
}
}
// Handle newlines.
if (ch === '\t') ch = ' ';
if (ch === '\n' || ch === '\r') {
ch = ' ';
var xxl = xl;
for (; xi < xxl; xi++) {
cell = lines[yi][xi];
if (!cell) break;
if (attr !== cell[0] || ch !== cell[1]) {
lines[yi][xi][0] = attr;
lines[yi][xi][1] = ch;
lines[yi].dirty = true;
}
}
continue;
}
if (!ch) {
if (this.full) {
if (ended === -1) ended = yi;
if (yi === ended) {
ch = ' ';
} else {
break;
}
} else {
break;
}
}
if (attr !== cell[0] || ch !== cell[1]) {
lines[yi][xi][0] = attr;
lines[yi][xi][1] = ch;
lines[yi].dirty = true;
}
}
}
return ret;
};
Text.prototype.__proto__ = Box.prototype;
/**
* Line