update files

This commit is contained in:
nuintun 2015-11-27 13:38:03 +08:00
parent 47814518cd
commit a6d641622b
4 changed files with 60 additions and 38 deletions

View File

@ -47,22 +47,28 @@ function Terminal(options){
if (Array.isArray(options.colors)) { if (Array.isArray(options.colors)) {
if (options.colors.length === 8) { if (options.colors.length === 8) {
options.colors = options.colors.concat(Terminal.colors.slice(8)); options.colors = options.colors.concat(Terminal.colors.slice(8));
this.vcolors = Terminal.makeVcolors(options.colors);
} else if (options.colors.length === 16) { } else if (options.colors.length === 16) {
options.colors = options.colors.concat(Terminal.colors.slice(16)); options.colors = options.colors.concat(Terminal.colors.slice(16));
this.vcolors = Terminal.makeVcolors(options.colors);
} else if (options.colors.length === 10) { } else if (options.colors.length === 10) {
options.colors = options.colors.slice(0, -2).concat(Terminal.colors.slice(8, -2), options.colors.slice(-2)); options.colors = options.colors.slice(0, -2).concat(Terminal.colors.slice(8, -2), options.colors.slice(-2));
this.vcolors = Terminal.makeVcolors(options.colors);
} else if (options.colors.length === 18) { } else if (options.colors.length === 18) {
options.colors = options.colors.slice(0, -2).concat(Terminal.colors.slice(16, -2), options.colors.slice(-2)); options.colors = options.colors.slice(0, -2).concat(Terminal.colors.slice(16, -2), options.colors.slice(-2));
this.vcolors = Terminal.makeVcolors(options.colors);
} else { } else {
options.colors = Terminal.colors; options.colors = Terminal.colors.slice();
this.vcolors = Terminal.vcolors.slice();
} }
} else { } else {
options.colors = Terminal.colors; options.colors = Terminal.colors.slice();
this.vcolors = Terminal.vcolors.slice();
} }
this.colors = options.colors; this.colors = options.colors;
this.bgColor = options.bgColor || Terminal.defaultColors.bgColor; this.background = options.background || Terminal.defaultColors.background;
this.fgColor = options.fgColor || Terminal.defaultColors.fgColor; this.foreground = options.foreground || Terminal.defaultColors.foreground;
// set screen size // set screen size
options.cols = options.cols || Terminal.geometry[0]; options.cols = options.cols || Terminal.geometry[0];

View File

@ -5,21 +5,44 @@
'use strict'; 'use strict';
module.exports = function (Terminal){ module.exports = function (Terminal){
// Colors 0-15 // Default colors
Terminal.colors = [ Terminal.defaultColors = {
// dark: // Colors 0-15
'#2e3436', '#cc0000', '#4e9a06', '#c4a000', '#3465a4', '#75507b', '#06989a', '#d3d7cf', colors: [
// bright: // dark:
'#555753', '#ef2929', '#8ae234', '#fce94f', '#729fcf', '#ad7fa8', '#34e2e2', '#eeeeec' '#000000', // black
]; '#cd0000', // red3
'#00cd00', // green3
'#cdcd00', // yellow3
'#0000ee', // blue2
'#cd00cd', // magenta3
'#00cdcd', // cyan3
'#e5e5e5', // gray90
// bright:
'#7f7f7f', // gray50
'#ff0000', // red
'#00ff00', // green
'#ffff00', // yellow
'#5c5cff', // rgb:5c/5c/ff
'#ff00ff', // magenta
'#00ffff', // cyan
'#ffffff' // white
],
// Default background color
background: '#181818',
// Default foreground color
foreground: '#ffffff'
};
// Colors 16-255 // Colors 16-255
// Much thanks to TooTallNate for writing this. // Much thanks to TooTallNate for writing this.
Terminal.colors = (function (){ Terminal.makeColors = function (colors){
var i; var i;
var colors = Terminal.colors;
var r = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]; var r = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff];
// copy colors
colors = colors.slice();
// 16-231 // 16-231
i = 0; i = 0;
@ -46,13 +69,13 @@ module.exports = function (Terminal){
} }
return colors; return colors;
})(); };
Terminal.vcolors = (function (){ // Vcolors 0-255
Terminal.makeVcolors = function (colors){
var color; var color;
var i = 0; var i = 0;
var out = []; var out = [];
var colors = Terminal.colors;
for (; i < 256; i++) { for (; i < 256; i++) {
color = parseInt(colors[i].substring(1), 16); color = parseInt(colors[i].substring(1), 16);
@ -61,14 +84,10 @@ module.exports = function (Terminal){
} }
return out; return out;
})();
// Default BG/FG
Terminal.defaultColors = {
bgColor: '#000000',
fgColor: '#f0f0f0'
}; };
Terminal.colors[256] = Terminal.defaultColors.bgColor; // Colors 0-255
Terminal.colors[257] = Terminal.defaultColors.fgColor; Terminal.colors = Terminal.makeColors(Terminal.defaultColors.colors);
// Vcolors 0-255
Terminal.vcolors = Terminal.makeVcolors(Terminal.colors);
}; };

View File

@ -7,12 +7,13 @@
module.exports = function (Terminal){ module.exports = function (Terminal){
/** /**
* matchColor * matchColor
* @param vcolors
* @param r1 * @param r1
* @param g1 * @param g1
* @param b1 * @param b1
* @returns {*} * @returns {*}
*/ */
function matchColor(r1, g1, b1){ function matchColor(vcolors, r1, g1, b1){
var hash = (r1 << 16) | (g1 << 8) | b1; var hash = (r1 << 16) | (g1 << 8) | b1;
if (matchColor._cache.hasOwnProperty(hash + '')) { if (matchColor._cache.hasOwnProperty(hash + '')) {
@ -24,8 +25,8 @@ module.exports = function (Terminal){
var ldiff = Infinity; var ldiff = Infinity;
var c, r2, g2, b2, diff; var c, r2, g2, b2, diff;
for (; i < Terminal.vcolors.length; i++) { for (; i < vcolors.length; i++) {
c = Terminal.vcolors[i]; c = vcolors[i];
r2 = c[0]; r2 = c[0];
g2 = c[1]; g2 = c[1];
b2 = c[2]; b2 = c[2];
@ -121,6 +122,7 @@ module.exports = function (Terminal){
// Optimize a single SGR0. // Optimize a single SGR0.
if (params.length === 1 && params[0] === 0) { if (params.length === 1 && params[0] === 0) {
this.curAttr = this.defAttr; this.curAttr = this.defAttr;
return; return;
} }
@ -198,10 +200,7 @@ module.exports = function (Terminal){
if (params[i + 1] === 2) { if (params[i + 1] === 2) {
i += 2; i += 2;
fg = matchColor( fg = matchColor(this.vcolors, params[i] & 0xff, params[i + 1] & 0xff, params[i + 2] & 0xff);
params[i] & 0xff,
params[i + 1] & 0xff,
params[i + 2] & 0xff);
if (fg === -1) fg = 0x1ff; if (fg === -1) fg = 0x1ff;
@ -216,10 +215,7 @@ module.exports = function (Terminal){
if (params[i + 1] === 2) { if (params[i + 1] === 2) {
i += 2; i += 2;
bg = matchColor( bg = matchColor(this.vcolors, params[i] & 0xff, params[i + 1] & 0xff, params[i + 2] & 0xff);
params[i] & 0xff,
params[i + 1] & 0xff,
params[i + 2] & 0xff);
if (bg === -1) bg = 0x1ff; if (bg === -1) bg = 0x1ff;

View File

@ -40,17 +40,18 @@ module.exports = function (Terminal){
this.screen.setAttribute('spellcheck', 'false'); this.screen.setAttribute('spellcheck', 'false');
// sync default bg/fg colors // sync default bg/fg colors
this.screen.style.backgroundColor = this.bgColor; this.screen.style.backgroundColor = this.background;
this.screen.style.color = this.fgColor; this.screen.style.color = this.foreground;
// Create the lines for our terminal. // Create the lines for our terminal.
this.children = []; this.children = [];
for (; i < this.rows; i++) { for (; i < this.rows; i++) {
div = document.createElement('div'); div = document.createElement('div');
div.className = 'ui-terminal-row';
this.screen.appendChild(div);
this.children.push(div); this.children.push(div);
this.screen.appendChild(div);
} }
// XXX - hack, move this somewhere else. // XXX - hack, move this somewhere else.