escape codes and newline handling.

This commit is contained in:
Christopher Jeffrey 2013-06-06 07:57:50 -05:00
parent 2cf1e5ee44
commit b7f18f6f61
2 changed files with 61 additions and 16 deletions

View File

@ -671,7 +671,20 @@ Box.prototype.render = function(stop) {
var cb = this.childBase
, xxl = xl - (this.border ? 1 : 0)
, xxi;
while (cb--) for (xxi = xi + (this.border ? 1 : 0); xxi < xxl; xxi++) ci++;
while (cb--) {
for (xxi = xi + (this.border ? 1 : 0); xxi < xxl; xxi++) {
if (this.content[ci] === '\n' || this.content[ci] === '\r') {
ci++;
if (!cb) break;
cb--;
} else if (this.content[ci] === '\x1b') {
for (; ci < this.content.length; ci++) {
if (this.content[ci] === 'm') break;
}
}
ci++;
}
}
}
var ret = {
@ -683,6 +696,8 @@ Box.prototype.render = function(stop) {
if (stop) return ret;
var lastEscape, hasEscapes, c;
for (; yi < yl; yi++) {
if (!lines[yi]) break;
for (xi = this.left; xi < xl; xi++) {
@ -708,22 +723,52 @@ Box.prototype.render = function(stop) {
} else {
attr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg;
ch = this.content[ci++] || ' ';
// Handle escape codes.
// NOTE: We could also change around `attr`, that might be cleaner.
// NOTE: Currently, this will not work with newline handling.
if (lastEscape) {
ch = lastEscape + ch;
lastEscape = '';
}
if (ch === '\x1b') {
hasEscapes = true;
if (c = /^\x1b\[\d+(?:;\d+)*m/.exec(this.content.substring(ci - 1))) {
ci += c[0].length - 1;
if (!this.content[c[0].length]) {
// Problem: No character to put here
// needs to wrap around below.
lastEscape = c[0];
ch = ' ';
} else {
ch = c[0] + this.content[ci];
}
ci++;
}
}
if (hasEscapes && xi === xl - 1) {
ch += '\x1b[m';
}
}
// 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;
// }
// }
//}
// Handle newlines.
if (ch === '\n' || ch === '\r') {
ch = ' ';
if (hasEscapes) ch += '\x1b[m';
var xxl = xl - (this.border ? 1 : 0);
for (; xi < xxl; xi++) {
attr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg;
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 (this.border) xi--;
continue;
}
if (attr !== cell[0] || ch !== cell[1]) {
lines[yi][xi][0] = attr;

View File

@ -136,7 +136,7 @@ var progress = new blessed.ProgressBar({
screen.append(progress);
var lorem = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
var lorem = 'Lorem ipsum \x1b[41mdolor sit amet, \nconsectetur adipisicing elit, \x1b[43msed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
var stext = new blessed.ScrollableText({
screen: screen,