From 461af61a598ff76f3cf4edfab02809e7d7eff233 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 13 Jun 2013 02:16:32 -0500 Subject: [PATCH] refactor colors/attr. --- lib/widget.js | 80 ++++++++++++++++++++++++++++++---------------- test/widget-pos.js | 4 +-- test/widget.js | 46 +++++++++++++------------- 3 files changed, 77 insertions(+), 53 deletions(-) diff --git a/lib/widget.js b/lib/widget.js index e2c1a8a..bc9c7e3 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -563,25 +563,20 @@ function Element(options) { // this.position.margin = options.margin || 0; this.hidden = options.hidden || false; - this.fg = options.fg || 0x1ff; - this.bg = options.bg || 0x1ff; + this.fg = convert(options.fg); + this.bg = convert(options.bg); this.bold = options.bold ? 1 : 0; this.underline = options.underline ? 2 : 0; - if (this.fg === -1) this.fg = exports.NORMAL; - if (this.bg === -1) this.bg = exports.NORMAL; - this.fixed = options.fixed || false; this.border = options.border; if (this.border) { this.border.type = this.border.type || 'bg'; - this.border.fg = this.border.fg || -1; - this.border.bg = this.border.bg || -1; + this.border.fg = convert(this.border.fg); + this.border.bg = convert(this.border.bg); this.border.ch = this.border.ch || ' '; this.border.bold = this.border.bold ? 1 : 0; this.border.underline = this.border.underline ? 2 : 0; - if (this.border.fg === -1) this.border.fg = exports.NORMAL; - if (this.border.bg === -1) this.border.bg = exports.NORMAL; } if (options.clickable) { @@ -1028,11 +1023,11 @@ Box.prototype.render = function(stop) { if (stop) return ret; battr = this.border - ? ((this.border.bold << 18) + (this.border.underline << 18)) | (this.border.fg << 9) | this.border.bg + ? sattr(this.border, this.border.fg, this.border.bg) : 0; //if (this.escapes) dattr = this.screen.dattr; - dattr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg; + dattr = sattr(this, this.fg, this.bg); attr = dattr; // Check previous line for escape codes. @@ -1193,7 +1188,7 @@ Text.prototype.render = function(stop) { if (stop) return ret; - dattr = ((this.bold << 18) + (this.underline << 18)) | (this.fg << 9) | this.bg; + dattr = sattr(this, this.fg, this.bg); attr = dattr; for (; yi < yl; yi++) { @@ -1271,8 +1266,8 @@ function Line(options) { options.border = { type: 'bg', - bg: options.bg || -1, - fg: options.fg || -1, + bg: convert(options.bg), + fg: convert(options.fg), ch: !options.type || options.type === 'ascii' ? orientation === 'horizontal' ? '─' : '│' : options.ch || ' ' @@ -1338,14 +1333,11 @@ function List(options) { this.items = []; this.selected = 0; - this.selectedBg = options.selectedBg || -1; - this.selectedFg = options.selectedFg || -1; + this.selectedBg = convert(options.selectedBg); + this.selectedFg = convert(options.selectedFg); this.selectedBold = options.selectedBold ? 1 : 0; this.selectedUnderline = options.selectedUnderline ? 2 : 0; - if (this.selectedBg === -1) this.selectedBg = exports.NORMAL; - if (this.selectedFg === -1) this.selectedFg = exports.NORMAL; - this.mouse = options.mouse || false; if (options.items) { @@ -1711,10 +1703,8 @@ function ProgressBar(options) { this.filled = +this.filled.slice(0, -1); } this.ch = options.ch || ' '; - this.barFg = options.barFg || -1; - this.barBg = options.barBg || -1; - if (this.barFg === -1) this.barFg = exports.NORMAL; - if (this.barBg === -1) this.barBg = exports.NORMAL; + this.barFg = convert(options.barFg); + this.barBg = convert(options.barBg); this.orientation = options.orientation || 'horizontal'; } @@ -1743,7 +1733,7 @@ ProgressBar.prototype.render = function(stop) { yi = yi + ((yl - yi) - (((yl - yi) * (this.filled / 100)) | 0)); } - dattr = ((this.bold << 18) + (this.underline << 18)) | (this.barFg << 9) | this.barBg; + dattr = sattr(this, this.barFg, this.barBg); this.screen.fillRegion(dattr, this.ch, xi, xl, yi, yl); @@ -1920,11 +1910,45 @@ function wrapContent(content, width) { return out; } -/** - * Constants - */ +var colors = { + default: -1, + bg: -1, + fg: -1, + black: 0, + red: 1, + green: 2, + yellow: 3, + blue: 4, + magenta: 5, + cyan: 6, + white: 7, + lightblack: 8, + lightred: 9, + lightgreen: 10, + lightyellow: 11, + lightblue: 12, + lightmagenta: 13, + lightcyan: 14, + lightwhite: 15 +}; -exports.NORMAL = 0x1ff; +function convert(color) { + var val = colors[color]; + if (val == null) val = color; + if (val == null) val = -1; + if (val === -1) return 0x1ff; + return val; +} + +function sattr(obj, fg, bg) { + return (((obj.invisible << 18) + + (obj.inverse << 18) + + (obj.blink << 18) + + (obj.bold << 18) + + (obj.underline << 18)) + | (fg << 9)) + | bg; +} /** * Expose diff --git a/test/widget-pos.js b/test/widget-pos.js index a521acd..3112906 100644 --- a/test/widget-pos.js +++ b/test/widget-pos.js @@ -15,7 +15,7 @@ var main = new blessed.Box({ //height: '75%', width: 115, height: 14, - bg: 3, + bg: 'yellow', top: 2, left: 2, content: 'Welcome to my program' @@ -28,7 +28,7 @@ var inner = new blessed.Box({ height: '50%', //width: 57, //height: 7, - bg: 4, + bg: 'blue', top: 2, left: 2, content: 'Hello' diff --git a/test/widget.js b/test/widget.js index 622f6cb..6cfb195 100644 --- a/test/widget.js +++ b/test/widget.js @@ -57,18 +57,18 @@ screen.children[0].append(new blessed.Box({ var list = new blessed.List({ mouse: true, - fg: 4, - bg: -1, + fg: 'blue', + bg: 'default', border: { type: 'ascii', - fg: -1, - bg: -1 + fg: 'default', + bg: 'default' }, width: '50%', height: '50%', top: 'center', left: 'center', - selectedBg: 2, + selectedBg: 'green', items: [ 'one', 'two', @@ -104,14 +104,14 @@ list.on('keypress', function(ch, key) { }); var progress = new blessed.ProgressBar({ - fg: 4, - bg: -1, - barBg: -1, - barFg: 4, + fg: 'blue', + bg: 'default', + barBg: 'default', + barFg: 'blue', border: { type: 'ascii', - fg: -1, - bg: -1 + fg: 'default', + bg: 'default' }, ch: ':', //orientation: 'vertical', @@ -135,14 +135,14 @@ var lorem = require('fs').readFileSync(__dirname + '/git.diff', 'utf8'); var stext = new blessed.ScrollableText({ mouse: true, content: lorem, - fg: 4, - bg: -1, - barBg: -1, - barFg: 4, + fg: 'blue', + bg: 'default', + barBg: 'default', + barFg: 'blue', border: { type: 'ascii', - fg: -1, - bg: -1 + fg: 'default', + bg: 'default' }, width: '50%', //height: 4, @@ -174,14 +174,14 @@ var input = new blessed.Textbox({ mouse: true, label: ' My Input ', content: '', - fg: 4, - bg: -1, - barBg: -1, - barFg: 4, + fg: 'blue', + bg: 'default', + barBg: 'default', + barFg: 'blue', border: { type: 'ascii', - fg: -1, - bg: -1 + fg: 'default', + bg: 'default' }, width: '30%', height: 3,