escape code and newline handling for Text.

This commit is contained in:
Christopher Jeffrey 2013-06-06 10:10:55 -05:00
parent 5902e224e5
commit 0477f38a2d
1 changed files with 45 additions and 22 deletions

View File

@ -648,7 +648,10 @@ Box.prototype.render = function(stop) {
, attr , attr
, ch , ch
, ci = 0 , ci = 0
, cl = this.content.length; , cl = this.content.length
, battr
, dattr
, c;
if (this.position.width) { if (this.position.width) {
xl = xi + this.width; xl = xi + this.width;
@ -665,6 +668,7 @@ Box.prototype.render = function(stop) {
yi -= this.parent.childBase; yi -= this.parent.childBase;
// if (noOverflow) ... // if (noOverflow) ...
yl = Math.min(yl, this.screen.rows - this.parent.bottom); yl = Math.min(yl, this.screen.rows - this.parent.bottom);
// yl -= this.parent.childBase; // necessary here, but not necessary in Text.render for some reason.
if (rtop - this.parent.childBase < 0) { if (rtop - this.parent.childBase < 0) {
return; return;
@ -703,12 +707,12 @@ Box.prototype.render = function(stop) {
if (stop) return ret; if (stop) return ret;
var battr = this.border battr = this.border
? ((this.border.bold << 18) + (this.border.underline << 18)) | (this.border.fg << 9) | this.border.bg ? ((this.border.bold << 18) + (this.border.underline << 18)) | (this.border.fg << 9) | this.border.bg
: 0; : 0;
//if (this.escapes) var dattr = this.screen.dattr; //if (this.escapes) dattr = this.screen.dattr;
var dattr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg; dattr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg;
attr = dattr; attr = dattr;
for (; yi < yl; yi++) { for (; yi < yl; yi++) {
@ -797,6 +801,9 @@ function Text(options) {
Text.prototype.__proto__ = Element.prototype; 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) { Text.prototype.render = function(stop) {
// NOTE: Maybe move this `hidden` check down below `stop` check and return `ret`. // NOTE: Maybe move this `hidden` check down below `stop` check and return `ret`.
if (this.hidden) return; if (this.hidden) return;
@ -811,7 +818,9 @@ Text.prototype.render = function(stop) {
, ch , ch
, ci = 0 , ci = 0
, cl = this.content.length , cl = this.content.length
, ended = -1; , ended = -1
, dattr
, c;
if (this.position.width) { if (this.position.width) {
xl = xi + this.width; xl = xi + this.width;
@ -850,15 +859,44 @@ Text.prototype.render = function(stop) {
if (stop) return ret; if (stop) return ret;
var dattr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg; dattr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg;
attr = dattr;
for (; yi < yl; yi++) { for (; yi < yl; yi++) {
if (!lines[yi]) break; if (!lines[yi]) break;
for (xi = this.left; xi < xl; xi++) { for (xi = this.left; xi < xl; xi++) {
cell = lines[yi][xi]; cell = lines[yi][xi];
if (!cell) break; if (!cell) break;
attr = dattr;
ch = this.content[ci++]; ch = this.content[ci++];
// Handle escape codes.
while (ch === '\x1b') {
if (c = /^\x1b\[(?:\d+(?:;\d+)*)?m/.exec(this.content.substring(ci - 1))) {
ci += c[0].length - 1;
attr = attrCode(c[0], attr);
ch = this.content[ci];
ci++;
}
}
// 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 (!ch) {
if (this.full) { if (this.full) {
if (ended === -1) ended = yi; if (ended === -1) ended = yi;
@ -872,21 +910,6 @@ Text.prototype.render = function(stop) {
} }
} }
// TODO: Allow newlines.
//if (ch === '\n' || ch === '\r') {
// ch = ' ';
// xl = xl - 1 - (this.border ? 1 : 0);
// for (; xi < xl; 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;
// }
// }
//}
if (attr !== cell[0] || ch !== cell[1]) { if (attr !== cell[0] || ch !== cell[1]) {
lines[yi][xi][0] = attr; lines[yi][xi][0] = attr;
lines[yi][xi][1] = ch; lines[yi][xi][1] = ch;