From 0477f38a2de70113b2e556326f26448d5b9c97ce Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 6 Jun 2013 10:10:55 -0500 Subject: [PATCH] escape code and newline handling for Text. --- lib/widget.js | 67 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/lib/widget.js b/lib/widget.js index 1e7f8e5..245d04f 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -648,7 +648,10 @@ Box.prototype.render = function(stop) { , attr , ch , ci = 0 - , cl = this.content.length; + , cl = this.content.length + , battr + , dattr + , c; if (this.position.width) { xl = xi + this.width; @@ -665,6 +668,7 @@ Box.prototype.render = function(stop) { yi -= this.parent.childBase; // if (noOverflow) ... 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) { return; @@ -703,12 +707,12 @@ Box.prototype.render = function(stop) { 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 : 0; - //if (this.escapes) var dattr = this.screen.dattr; - var dattr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg; + //if (this.escapes) dattr = this.screen.dattr; + dattr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg; attr = dattr; for (; yi < yl; yi++) { @@ -797,6 +801,9 @@ function Text(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; @@ -811,7 +818,9 @@ Text.prototype.render = function(stop) { , ch , ci = 0 , cl = this.content.length - , ended = -1; + , ended = -1 + , dattr + , c; if (this.position.width) { xl = xi + this.width; @@ -850,15 +859,44 @@ Text.prototype.render = function(stop) { 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++) { if (!lines[yi]) break; for (xi = this.left; xi < xl; xi++) { cell = lines[yi][xi]; if (!cell) break; - attr = dattr; + 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 (this.full) { 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]) { lines[yi][xi][0] = attr; lines[yi][xi][1] = ch;