better escape/newline handling.

This commit is contained in:
Christopher Jeffrey 2013-06-06 09:24:03 -05:00
parent cbda45ae11
commit 5902e224e5
2 changed files with 98 additions and 36 deletions

View File

@ -306,6 +306,7 @@ Screen.prototype.draw = function(start, end) {
if (bgColor < 8) {
bgColor += 40;
} else if (bgColor < 16) {
bgColor -= 8;
bgColor += 100;
}
out += bgColor + ';';
@ -319,6 +320,7 @@ Screen.prototype.draw = function(start, end) {
if (fgColor < 8) {
fgColor += 30;
} else if (fgColor < 16) {
fgColor -= 8;
fgColor += 90;
}
out += fgColor + ';';
@ -681,8 +683,7 @@ Box.prototype.render = function(stop) {
for (xxi = xi + (this.border ? 1 : 0); xxi < xxl; xxi++) {
if (this.content[ci] === '\n' || this.content[ci] === '\r') {
ci++;
if (!cb) break;
cb--;
break;
} else if (this.content[ci] === '\x1b') {
for (; ci < this.content.length; ci++) {
if (this.content[ci] === 'm') break;
@ -702,15 +703,21 @@ Box.prototype.render = function(stop) {
if (stop) return ret;
var lastEscape, hasEscapes, c;
var 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;
attr = dattr;
for (; yi < yl; yi++) {
if (!lines[yi]) break;
for (xi = this.left; xi < xl; xi++) {
cell = lines[yi][xi];
if (!cell) break;
if (this.border && (yi === this.top || xi === this.left || yi === yl - 1 || xi === xl - 1)) {
attr = ((this.border.bold << 18) + (this.border.underline << 18)) | (this.border.fg << 9) | this.border.bg;
if (this.border.type === 'ascii') {
if (yi === this.top) {
if (xi === this.left) ch = '┌';
@ -726,44 +733,32 @@ Box.prototype.render = function(stop) {
} else if (this.border.type === 'bg') {
ch = this.border.ch;
}
} else {
attr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg;
ch = this.content[ci++] || ' ';
if (battr !== cell[0] || ch !== cell[1]) {
lines[yi][xi][0] = battr;
lines[yi][xi][1] = ch;
lines[yi].dirty = true;
}
continue;
}
// 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';
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 = ' ';
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]) {
@ -1246,6 +1241,70 @@ ProgressBar.prototype.progress = function(filled) {
else if (this.filled > 100) this.filled = 100;
};
/**
* Helpers
*/
function attrCode(code, cur) {
var flags = (cur >> 18) & 0x1ff;
var fg = (cur >> 9) & 0x1ff;
var bg = cur & 0x1ff;
var c, i;
code = /^\x1b\[([^m]*)m$/.exec(code)[1].split(';');
if (!code[0]) code[0] = '0';
for (i = 0; i < code.length; i++) {
c = +code[i] || 0;
switch (c) {
case 0:
bg = 0x1ff;
fg = 0x1ff;
flags = 0;
break;
case 1:
flags |= 1;
break;
case 4:
flags |= 2;
break;
case 5:
flags |= 4;
break;
case 7:
flags |= 8;
break;
case 8:
flags |= 16;
break;
default:
if (c === 48 && code[i+1] === '5') {
i += 2;
bg = +code[i];
break;
} else if (c === 38 && code[i+1] === '5') {
i += 2;
fg = +code[i];
break;
}
if (c >= 40 && c <= 47) {
bg = c - 40;
} else if (c >= 100 && c <= 107) {
bg = c - 100;
bg += 8;
} else if (c >= 30 && c <= 37) {
fg = c - 30;
} else if (c >= 90 && c <= 97) {
fg = c - 90;
fg += 8;
}
break;
}
}
return (flags << 18) | (fg << 9) | bg;
}
/**
* Constants
*/

View File

@ -138,6 +138,8 @@ screen.append(progress);
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 lorem = require('fs').readFileSync(__dirname + '/../t.log', 'utf8');
var stext = new blessed.ScrollableText({
screen: screen,
parent: screen,
@ -153,7 +155,8 @@ var stext = new blessed.ScrollableText({
bg: -1
},
width: '50%',
height: 4,
//height: 4,
height: 6,
left: 0,
bottom: 0
});
@ -197,8 +200,8 @@ list.focus();
screen.render();
setInterval(function() {
stext.toggle();
screen.render();
//stext.toggle();
//screen.render();
}, 1000);
(function fill() {