update files

This commit is contained in:
nuintun 2015-11-25 17:51:14 +08:00
parent 092961802c
commit b13364fd67
3 changed files with 209 additions and 105 deletions

View File

@ -48,6 +48,21 @@ module.exports = function (Terminal){
return colors; return colors;
})(); })();
Terminal.vcolors = (function (){
var color;
var i = 0;
var out = [];
var colors = Terminal.colors;
for (; i < 256; i++) {
color = parseInt(colors[i].substring(1), 16);
out.push([(color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff]);
}
return out;
})();
// Default BG/FG // Default BG/FG
Terminal.defaultColors = { Terminal.defaultColors = {
bgColor: '#000000', bgColor: '#000000',

View File

@ -5,6 +5,49 @@
'use strict'; 'use strict';
module.exports = function (Terminal){ module.exports = function (Terminal){
function matchColor(r1, g1, b1){
var hash = (r1 << 16) | (g1 << 8) | b1;
if (matchColor._cache.hasOwnProperty(hash + '')) {
return matchColor._cache[hash];
}
var i = 0;
var li = -1;
var ldiff = Infinity;
var c, r2, g2, b2, diff;
for (; i < Terminal.vcolors.length; i++) {
c = Terminal.vcolors[i];
r2 = c[0];
g2 = c[1];
b2 = c[2];
diff = matchColor.distance(r1, g1, b1, r2, g2, b2);
if (diff === 0) {
li = i;
break;
}
if (diff < ldiff) {
ldiff = diff;
li = i;
}
}
return matchColor._cache[hash] = li;
}
matchColor._cache = {};
// http://stackoverflow.com/questions/1633828
matchColor.distance = function (r1, g1, b1, r2, g2, b2){
return Math.pow(30 * (r1 - r2), 2)
+ Math.pow(59 * (g1 - g2), 2)
+ Math.pow(11 * (b1 - b2), 2);
};
// CSI Pm m Character Attributes (SGR). // CSI Pm m Character Attributes (SGR).
// Ps = 0 -> Normal (default). // Ps = 0 -> Normal (default).
// Ps = 1 -> Bold. // Ps = 1 -> Bold.
@ -68,81 +111,126 @@ module.exports = function (Terminal){
// Ps = 4 8 ; 5 ; Ps -> Set background color to the second // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
// Ps. // Ps.
Terminal.prototype.charAttributes = function (params){ Terminal.prototype.charAttributes = function (params){
var l = params.length, // Optimize a single SGR0.
i = 0, if (params.length === 1 && params[0] === 0) {
bg, fg, p; this.curAttr = this.defAttr;
return;
}
var p;
var i = 0;
var l = params.length;
var flags = this.curAttr >> 18;
var fg = (this.curAttr >> 9) & 0x1ff;
var bg = this.curAttr & 0x1ff;
for (; i < l; i++) { for (; i < l; i++) {
p = params[i]; p = params[i];
if (p >= 30 && p <= 37) { if (p >= 30 && p <= 37) {
// fg color 8 // fg color 8
this.curAttr = (this.curAttr & ~(0x1ff << 9)) | ((p - 30) << 9); fg = p - 30;
} else if (p >= 40 && p <= 47) { } else if (p >= 40 && p <= 47) {
// bg color 8 // bg color 8
this.curAttr = (this.curAttr & ~0x1ff) | (p - 40); bg = p - 40;
} else if (p >= 90 && p <= 97) { } else if (p >= 90 && p <= 97) {
// fg color 16 // fg color 16
p += 8; p += 8;
this.curAttr = (this.curAttr & ~(0x1ff << 9)) | ((p - 90) << 9); fg = p - 90;
} else if (p >= 100 && p <= 107) { } else if (p >= 100 && p <= 107) {
// bg color 16 // bg color 16
p += 8; p += 8;
this.curAttr = (this.curAttr & ~0x1ff) | (p - 100); bg = p - 100;
} else if (p === 0) { } else if (p === 0) {
// default // default
this.curAttr = this.defAttr; flags = this.defAttr >> 18;
fg = (this.defAttr >> 9) & 0x1ff;
bg = this.defAttr & 0x1ff;
// flags = 0;
// fg = 0x1ff;
// bg = 0x1ff;
} else if (p === 1) { } else if (p === 1) {
// bold text // bold text
this.curAttr = this.curAttr | (1 << 18); flags |= 1;
} else if (p === 4) { } else if (p === 4) {
// underlined text // underlined text
this.curAttr = this.curAttr | (2 << 18); flags |= 2;
} else if (p === 7 || p === 27) { } else if (p === 5) {
// blink
flags |= 4;
} else if (p === 7) {
// inverse and positive // inverse and positive
// test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m' // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
if (p === 7) { flags |= 8;
if ((this.curAttr >> 18) & 4) continue; } else if (p === 8) {
this.curAttr = this.curAttr | (4 << 18); // invisible
} else if (p === 27) { flags |= 16;
if (~(this.curAttr >> 18) & 4) continue;
this.curAttr = this.curAttr & ~(4 << 18);
}
bg = this.curAttr & 0x1ff;
fg = (this.curAttr >> 9) & 0x1ff;
this.curAttr = (this.curAttr & ~0x3ffff) | ((bg << 9) | fg);
} else if (p === 22) { } else if (p === 22) {
// not bold // not bold
this.curAttr = this.curAttr & ~(1 << 18); flags &= ~1;
} else if (p === 24) { } else if (p === 24) {
// not underlined // not underlined
this.curAttr = this.curAttr & ~(2 << 18); 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) { } else if (p === 39) {
// reset fg // reset fg
this.curAttr = this.curAttr & ~(0x1ff << 9); fg = (this.defAttr >> 9) & 0x1ff;
this.curAttr = this.curAttr | (((this.defAttr >> 9) & 0x1ff) << 9);
} else if (p === 49) { } else if (p === 49) {
// reset bg // reset bg
this.curAttr = this.curAttr & ~0x1ff; bg = this.defAttr & 0x1ff;
this.curAttr = this.curAttr | (this.defAttr & 0x1ff);
} else if (p === 38) { } else if (p === 38) {
// fg color 256 // fg color 256
if (params[i + 1] !== 5) continue; if (params[i + 1] === 2) {
i += 2;
fg = matchColor(
params[i] & 0xff,
params[i + 1] & 0xff,
params[i + 2] & 0xff);
if (fg === -1) fg = 0x1ff;
i += 2;
} else if (params[i + 1] === 5) {
i += 2; i += 2;
p = params[i] & 0xff; p = params[i] & 0xff;
// convert 88 colors to 256 fg = p;
// if (this.is('rxvt-unicode') && p < 88) p = p * 2.9090 | 0; }
this.curAttr = (this.curAttr & ~(0x1ff << 9)) | (p << 9);
} else if (p === 48) { } else if (p === 48) {
// bg color 256 // bg color 256
if (params[i + 1] !== 5) continue; if (params[i + 1] === 2) {
i += 2;
bg = matchColor(
params[i] & 0xff,
params[i + 1] & 0xff,
params[i + 2] & 0xff);
if (bg === -1) bg = 0x1ff;
i += 2;
} else if (params[i + 1] === 5) {
i += 2; i += 2;
p = params[i] & 0xff; p = params[i] & 0xff;
// convert 88 colors to 256 bg = p;
// if (this.is('rxvt-unicode') && p < 88) p = p * 2.9090 | 0; }
this.curAttr = (this.curAttr & ~0x1ff) | 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);
} }
} }
this.curAttr = (flags << 18) | (fg << 9) | bg;
}; };
}; };

View File

@ -11,14 +11,15 @@ module.exports = function (Terminal){
this.cursorHidden = false; this.cursorHidden = false;
this.insertMode = false; this.insertMode = false;
this.originMode = false; this.originMode = false;
this.wraparoundMode = false; // autowrap // autowrap
this.applicationKeypad = false; // ? this.wraparoundMode = false;
this.applicationKeypad = false;
this.scrollTop = 0; this.scrollTop = 0;
this.scrollBottom = this.rows - 1; this.scrollBottom = this.rows - 1;
this.curAttr = this.defAttr; this.curAttr = this.defAttr;
this.x = this.y = 0; // ? this.x = this.y = 0;
this.charset = null; this.charset = null;
this.glevel = 0; // ?? this.glevel = 0;
this.charsets = [null]; // ?? this.charsets = [null];
}; };
}; };