From 15f683359813a7ebfc12402180e82d7c3e857ea9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 11 Aug 2013 03:44:02 -0500 Subject: [PATCH] fix inverse. refactor charAttributes. --- example/index.js | 11 ++++++++ lib/term.js | 73 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/example/index.js b/example/index.js index 18df3e6a..2c999a40 100644 --- a/example/index.js +++ b/example/index.js @@ -15,6 +15,15 @@ var http = require('http') process.title = 'term.js'; +/** + * Dump + */ + +var stream; +if (process.argv[2] === '--dump') { + stream = require('fs').createWriteStream(__dirname + '/dump.log'); +} + /** * Open Terminal */ @@ -33,6 +42,7 @@ term = pty.fork(process.env.SHELL || 'sh', [], { }); term.on('data', function(data) { + if (stream) stream.write('OUT: ' + data + '\n-\n'); return !socket ? buff.push(data) : socket.emit('data', data); @@ -100,6 +110,7 @@ io.sockets.on('connection', function(sock) { socket = sock; socket.on('data', function(data) { + if (stream) stream.write('IN: ' + data + '\n-\n'); term.write(data); }); diff --git a/lib/term.js b/lib/term.js index 74a73911..4149db29 100644 --- a/lib/term.js +++ b/lib/term.js @@ -211,7 +211,8 @@ function Terminal(options) { this.readable = true; this.writable = true; - this.defAttr = (257 << 9) | 256; + this.defAttr = (0 << 18) | (257 << 9) | (256 << 0); + // this.defAttr = (0 << 18) | (0x1ff << 9) | (0x1ff << 0); this.curAttr = this.defAttr; this.params = []; @@ -996,6 +997,7 @@ Terminal.prototype.refresh = function(start, end) { fgColor = (data >> 9) & 0x1ff; flags = data >> 18; + // bold if (flags & 1) { if (!Terminal.brokenBold) { out += 'font-weight:bold;'; @@ -1004,16 +1006,43 @@ Terminal.prototype.refresh = function(start, end) { if (fgColor < 8) fgColor += 8; } + // underline if (flags & 2) { out += 'text-decoration:underline;'; } + // blink + if (flags & 4) { + if (flags & 2) { + out = out.slice(0, -1); + out += ' blink;'; + } else { + out += 'text-decoration:blink;'; + } + } + + // inverse + if (flags & 8) { + bgColor = (data >> 9) & 0x1ff; + fgColor = data & 0x1ff; + // Should inverse just be before the + // above boldColors effect instead? + if ((flags & 1) && fgColor < 8) fgColor += 8; + } + + // invisible + if (flags & 16) { + out += 'visibility:hidden;'; + } + + // if (bgColor !== 0x1ff) { if (bgColor !== 256) { out += 'background-color:' + Terminal.colors[bgColor] + ';'; } + // if (fgColor !== 0x1ff) { if (fgColor !== 257) { out += 'color:' + Terminal.colors[fgColor] @@ -2845,6 +2874,12 @@ Terminal.prototype.eraseInLine = function(params) { // Ps = 4 8 ; 5 ; Ps -> Set background color to the second // Ps. Terminal.prototype.charAttributes = function(params) { + // Optimize a single SGR0. + if (params.length === 1 && params[0] === 0) { + this.curAttr = this.defAttr; + return; + } + var l = params.length , i = 0 , flags = this.curAttr >> 18 @@ -2873,30 +2908,40 @@ Terminal.prototype.charAttributes = function(params) { flags = this.defAttr >> 18; fg = (this.defAttr >> 9) & 0x1ff; bg = this.defAttr & 0x1ff; + // flags = 0; + // fg = 0x1ff; + // bg = 0x1ff; } else if (p === 1) { // bold text flags |= 1; } else if (p === 4) { // underlined text flags |= 2; - } else if (p === 7 || p === 27) { + } else if (p === 5) { + // blink + flags |= 4; + } else if (p === 7) { // inverse and positive // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m' - if (p === 7) { - if (flags & 4) continue; - flags |= 4; - } else if (p === 27) { - if (~flags & 4) continue; - flags &= ~4; - } - // JAVASCRIPT, Y U NO XOR SWAP? - p = bg, bg = fg, fg = p; + flags |= 8; + } else if (p === 8) { + // invisible + flags |= 16; } else if (p === 22) { // not bold flags &= ~1; } else if (p === 24) { // not underlined flags &= ~2; + } else if (p === 25) { + // not blink + flags &= ~4; + } else if (p === 27) { + // not inverse + flags &= ~8; + } else if (p === 28) { + // not invisible + flags &= ~16; } else if (p === 39) { // reset fg fg = (this.defAttr >> 9) & 0x1ff; @@ -2933,6 +2978,12 @@ Terminal.prototype.charAttributes = function(params) { p = params[i] & 0xff; bg = p; } + } else if (p === 100) { + // reset fg/bg + fg = (this.defAttr >> 9) & 0x1ff; + bg = this.defAttr & 0x1ff; + } else { + this.error('Unknown SGR attribute: %d.', p); } }