This commit is contained in:
Bryan Ashby 2015-04-29 21:57:26 -06:00
commit 23a4344a4b
10 changed files with 106 additions and 110 deletions

View File

@ -1,24 +1,27 @@
/* jslint node: true */ /* jslint node: true */
'use strict'; 'use strict';
var events = require('events');
var util = require('util');
var miscUtil = require('./misc_util.js'); var miscUtil = require('./misc_util.js');
var ansi = require('./ansi_term.js'); var ansi = require('./ansi_term.js');
var events = require('events');
var util = require('util');
var _ = require('lodash');
exports.ANSIEscapeParser = ANSIEscapeParser; exports.ANSIEscapeParser = ANSIEscapeParser;
var CR = 0x0d;
var LF = 0x0a;
function ANSIEscapeParser(options) { function ANSIEscapeParser(options) {
var self = this; var self = this;
events.EventEmitter.call(this); events.EventEmitter.call(this);
this.column = 1; this.column = 1;
this.row = 1; this.row = 1;
this.style = 0x00; this.scrollBack = 0;
//this.style = { 0 : true }; this.graphicRendition = { styles : [] };
this.scrollBack = 0;
options = miscUtil.valueWithDefault(options, { options = miscUtil.valueWithDefault(options, {
mciReplaceChar : '', mciReplaceChar : '',
@ -71,23 +74,11 @@ function ANSIEscapeParser(options) {
self.emit('clear screen'); self.emit('clear screen');
}; };
self.resetColor = function() {
//self.fgColor = 7;
//self.bgColor = 0;
self.fgColor = 39;
self.bgColor = 49;
//self.style = { 0 : true };
//delete self.style;
self.style = 0;
};
self.rowUpdated = function() { self.rowUpdated = function() {
self.emit('row update', self.row + self.scrollBack); self.emit('row update', self.row + self.scrollBack);
}; };
function literal(text) { function literal(text) {
var CR = 0x0d;
var LF = 0x0a;
var charCode; var charCode;
var len = text.length; var len = text.length;
@ -127,14 +118,14 @@ function ANSIEscapeParser(options) {
function getProcessedMCI(mci) { function getProcessedMCI(mci) {
if(self.mciReplaceChar.length > 0) { if(self.mciReplaceChar.length > 0) {
var eraseColor = ansi.sgr(self.eraseColor.style, self.eraseColor.fgColor, self.eraseColor.bgColor); return ansi.getSGRFromGraphicRendition(self.graphicRendition) + new Array(mci.length + 1).join(self.mciReplaceChar);
return eraseColor + new Array(mci.length + 1).join(self.mciReplaceChar);
} else { } else {
return mci; return mci;
} }
} }
function parseMCI(buffer) { function parseMCI(buffer) {
// :TODO: move this to "constants" seciton @ top
var mciRe = /\%([A-Z]{2})([0-9]{1,2})?(?:\(([0-9A-Z,]+)\))*/g; var mciRe = /\%([A-Z]{2})([0-9]{1,2})?(?:\(([0-9A-Z,]+)\))*/g;
var pos = 0; var pos = 0;
var match; var match;
@ -166,18 +157,15 @@ function ANSIEscapeParser(options) {
self.lastMciCode = fullMciCode; self.lastMciCode = fullMciCode;
self.eraseColor = { self.graphicRenditionForErase = _.clone(self.graphicRendition, true);
flags : self.style,
fgColor : self.fgColor,
bgColor : self.bgColor,
};
} }
self.emit('mci', mciCode, id, args); self.emit('mci', mciCode, id, args);
if(self.mciReplaceChar.length > 0) { if(self.mciReplaceChar.length > 0) {
self.emit('chunk', ansi.sgr(self.eraseColor.style, self.eraseColor.fgColor, self.eraseColor.bgColor)); //self.emit('chunk', ansi.sgr(self.eraseColor.style, self.eraseColor.fgColor, self.eraseColor.bgColor));
self.emit('chunk', ansi.getSGRFromGraphicRendition(self.graphicRenditionForErase));
literal(new Array(match[0].length + 1).join(self.mciReplaceChar)); literal(new Array(match[0].length + 1).join(self.mciReplaceChar));
} else { } else {
literal(match[0]); literal(match[0]);
@ -197,6 +185,7 @@ function ANSIEscapeParser(options) {
self.parse = function(buffer, savedRe) { self.parse = function(buffer, savedRe) {
// :TODO: ensure this conforms to ANSI-BBS / CTerm / bansi.txt for movement/etc. // :TODO: ensure this conforms to ANSI-BBS / CTerm / bansi.txt for movement/etc.
// :TODO: move this to "constants" section @ top
var re = /(?:\x1b\x5b)([\?=;0-9]*?)([ABCDHJKfhlmnpsu])/g; var re = /(?:\x1b\x5b)([\?=;0-9]*?)([ABCDHJKfhlmnpsu])/g;
var pos = 0; var pos = 0;
var match; var match;
@ -282,47 +271,28 @@ function ANSIEscapeParser(options) {
// set graphic rendition // set graphic rendition
case 'm' : case 'm' :
// :TODO: reset state here for new system self.graphicRendition = { styles : [ 0 ] }; // reset
//self.graphicRendition.styles = [ 0 ];
for(i = 0, len = args.length; i < len; ++i) { for(i = 0, len = args.length; i < len; ++i) {
arg = args[i]; arg = args[i];
// :TODO: finish this system
// * style is map of styleName -> boolean
// * change self.style -> self.styles
// * Change all fg/bg/etc -> self.state.color { fg, bg, style{} }
// * Change all refs to use this new system
// * When passing color -> sgr, iterate enabled styles -> additional params
// * view.getANSIColor() will need updated
// * art.js will need updated
/*
if(ANSIEscapeParser.foregroundColors[arg]) { if(ANSIEscapeParser.foregroundColors[arg]) {
self.fgColor = arg;//ANSIEscapeParser.foregroundColors[arg]; self.graphicRendition.fg = arg;
} else if(ANSIEscapeParser.backgroundColors[arg]) { } else if(ANSIEscapeParser.backgroundColors[arg]) {
self.bgColor = arg;//ANSIEscapeParser.backgroundColors[arg]; self.graphicRendition.bg = arg;
} else if(39 === arg) { } else if(39 === arg) {
delete self.fgColor; delete self.graphicRendition.fg;
} else if(49 === arg) { } else if(49 === arg) {
delete self.bgColor; delete self.graphicRendition.bg;
} else if(ANSIEscapeParser.styles[arg]) { } else if(ANSIEscapeParser.styles[arg]) {
self.style = arg; self.graphicRendition.styles.push(arg);
} else if(22 === arg) {
// :TODO: remove bold.
} }
*/
if(arg >= 30 && arg <= 37) {
self.fgColor = arg;
} else if(arg >= 40 && arg <= 47) {
self.bgColor = arg;
} else {
self.style |= arg;
if(0 === arg) {
self.resetColor();
//self.style = 0;
}
}
} }
console.log(self.graphicRendition)
break; break;
// erase display/screen // erase display/screen
@ -334,8 +304,6 @@ function ANSIEscapeParser(options) {
break; break;
} }
} }
this.resetColor();
} }
util.inherits(ANSIEscapeParser, events.EventEmitter); util.inherits(ANSIEscapeParser, events.EventEmitter);
@ -365,12 +333,13 @@ ANSIEscapeParser.backgroundColors = {
}; };
Object.freeze(ANSIEscapeParser.backgroundColors); Object.freeze(ANSIEscapeParser.backgroundColors);
// :TODO: ensure these all align with that of ansi_term.js
ANSIEscapeParser.styles = { ANSIEscapeParser.styles = {
0 : 'default', 0 : 'default',
1 : 'bright', 1 : 'bright',
2 : 'dim', 2 : 'dim',
5 : 'slow blink', 5 : 'slowBlink',
6 : 'fast blink', 6 : 'fastBlink',
7 : 'negative', 7 : 'negative',
8 : 'concealed', 8 : 'concealed',
22 : 'normal', 22 : 'normal',

View File

@ -19,6 +19,7 @@ var _ = require('lodash');
exports.getFGColorValue = getFGColorValue; exports.getFGColorValue = getFGColorValue;
exports.getBGColorValue = getBGColorValue; exports.getBGColorValue = getBGColorValue;
exports.sgr = sgr; exports.sgr = sgr;
exports.getSGRFromGraphicRendition = getSGRFromGraphicRendition;
exports.clearScreen = clearScreen; exports.clearScreen = clearScreen;
exports.resetScreen = resetScreen; exports.resetScreen = resetScreen;
exports.normal = normal; exports.normal = normal;
@ -323,23 +324,19 @@ function sgr() {
return ESC_CSI + result + 'm'; return ESC_CSI + result + 'm';
} }
function ansiFromColor(color) { //
var sgrParams = []; // Converts a Graphic Rendition object used elsewhere
for(var k in color.styles) { // to a ANSI SGR sequence
if(true === color.styles[k]) { //
sgrParams.push(k); function getSGRFromGraphicRendition(graphicRendition) {
} var sgrSeq = graphicRendition.styles.slice(0); // start out with styles
if(graphicRendition.fg) {
sgrSeq.push(graphicRendition.fg);
} }
if(graphicRendition.bg) {
if(color.fg) { sgrSeq.push(graphicRendition.bg);
sgrParams.push(color.fg);
} }
return sgr(sgrSeq);
if(color.bg) {
sgrParams.push(color.bg);
}
return sgr(sgrParams);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -456,22 +456,29 @@ function display(options, cb) {
var mapItem = mciCode + id; var mapItem = mciCode + id;
// :TODO: Avoid mutiple [] lookups here // :TODO: Avoid mutiple [] lookups here
if(mci[mapItem]) { if(mci[mapItem]) {
mci[mapItem].focusGraphicRendition = parser.graphicRendition;
/*
mci[mapItem].focusColor = { mci[mapItem].focusColor = {
fg : parser.fgColor, fg : parser.fgColor,
bg : parser.bgColor, bg : parser.bgColor,
flags : parser.style, flags : parser.style,
}; };
*/
mci[mapItem].focusArgs = args; mci[mapItem].focusArgs = args;
} else { } else {
mci[mapItem] = { mci[mapItem] = {
args : args, args : args,
/*
color : { color : {
fg : parser.fgColor, fg : parser.fgColor,
bg : parser.bgColor, bg : parser.bgColor,
flags : parser.style, flags : parser.style,
}, },
code : mciCode, */
id : parseInt(id, 10), graphicRendition : parser.graphicRendition,
code : mciCode,
id : parseInt(id, 10),
}; };
mciPosQueue.push(mapItem); mciPosQueue.push(mapItem);

View File

@ -23,8 +23,7 @@ function EditTextView(options) {
this.cursorPos = { x : 0 }; this.cursorPos = { x : 0 };
this.clientBackspace = function() { this.clientBackspace = function() {
this.client.term.write( this.client.term.write('\b' + this.getSGR() + this.fillChar + '\b' + this.getFocusSGR());
'\b' + this.getANSIColor(this.getColor()) + this.fillChar + '\b' + this.getANSIColor(this.getFocusColor()));
}; };
} }

View File

@ -56,11 +56,15 @@ MCIViewFactory.prototype.createFromMCI = function(mci) {
var view; var view;
var options = { var options = {
client : this.client, client : this.client,
id : mci.id, id : mci.id,
color : mci.color, color : mci.color,
focusColor : mci.focusColor, focusColor : mci.focusColor,
position : { x : mci.position[0], y : mci.position[1] },
graphicRendition : mci.graphicRendition,
focusGraphicRendition : mci.focusGraphicRendition,
position : { x : mci.position[0], y : mci.position[1] },
}; };
function setOption(pos, name) { function setOption(pos, name) {

View File

@ -38,7 +38,7 @@ function SpinnerMenuView(options) {
} }
this.client.term.write(ansi.goto(this.position.x, this.position.y)); this.client.term.write(ansi.goto(this.position.x, this.position.y));
this.client.term.write(self.getANSIColor(this.hasFocus ? self.getFocusColor() : self.getColor())); this.client.term.write(self.hasFocus ? self.getFocusSGR() : self.getSGR());
var text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle); var text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);

View File

@ -51,14 +51,18 @@ function TextView(options) {
// this is the text but too long // this is the text but too long
// text but too long // text but too long
if(this.horizScroll) { if(this.horizScroll) {
textToDraw = textToDraw.substr(textToDraw.length - this.dimens.width, textToDraw.length);//0, this.dimens.width);//textToDraw.length - (this.dimens.width + 1)); textToDraw = textToDraw.substr(textToDraw.length - this.dimens.width, textToDraw.length);
} }
} }
var textAnsiColor = this.getANSIColor(this.hasFocus ? this.getFocusColor() : this.getColor()); this.client.term.write(strUtil.pad(
var fillAnsiColor = this.getANSIColor(this.getColor()); textToDraw,
this.dimens.width + 1,
this.client.term.write(strUtil.pad(textToDraw, this.dimens.width + 1, this.fillChar, this.justify, textAnsiColor, fillAnsiColor)); this.fillChar,
this.justify,
this.hasFocus ? this.getFocusSGR() : this.getSGR(),
this.getSGR() // :TODO: use extended style color if avail
));
}; };
this.setText(options.text || ''); this.setText(options.text || '');
@ -76,8 +80,15 @@ TextView.prototype.setFocus = function(focused) {
TextView.super_.prototype.setFocus.call(this, focused); TextView.super_.prototype.setFocus.call(this, focused);
this.redraw(); this.redraw();
console.log('---')
console.log(this.graphicRendition)
console.log(this.focusGraphicRendition)
console.log('---')
// position & SGR for cursor
this.client.term.write(ansi.goto(this.position.x, this.position.y + this.text.length)); this.client.term.write(ansi.goto(this.position.x, this.position.y + this.text.length));
this.client.term.write(this.getANSIColor(this.getFocusColor())); this.client.term.write(this.getFocusSGR());
}; };
TextView.prototype.getData = function() { TextView.prototype.getData = function() {

View File

@ -37,7 +37,7 @@ ToggleMenuView.prototype.redraw = function() {
this.cachePositions(); this.cachePositions();
this.client.term.write(this.getANSIColor(this.hasFocus ? this.getFocusColor() : this.getColor())); this.client.term.write(this.hasFocus ? this.getFocusSGR() : this.getSGR());
assert(this.items.length === 2); assert(this.items.length === 2);
for(var i = 0; i < 2; i++) { for(var i = 0; i < 2; i++) {
@ -47,13 +47,13 @@ ToggleMenuView.prototype.redraw = function() {
if(1 === i) { if(1 === i) {
//console.log(this.styleColor1) //console.log(this.styleColor1)
var sepColor = this.getANSIColor(this.styleColor1 || this.getColor()); //var sepColor = this.getANSIColor(this.styleColor1 || this.getColor());
console.log(sepColor.substr(1)) //console.log(sepColor.substr(1))
//sepColor = '\u001b[0m\u001b[1;30m'; var sepColor = '\u001b[0m\u001b[1;30m'; // :TODO: FIX ME!!!
this.client.term.write(sepColor + ' / '); this.client.term.write(sepColor + ' / ');
} }
this.client.term.write(this.getANSIColor(i === this.focusedItemIndex ? this.getFocusColor() : this.getColor())); this.client.term.write(i === this.focusedItemIndex ? this.getFocusSGR() : this.getSGR());
this.client.term.write(text); this.client.term.write(text);
} }
}; };

View File

@ -76,8 +76,7 @@ function VerticalMenuView(options) {
} }
self.client.term.write(ansi.goto(item.xPosition, self.position.y)); self.client.term.write(ansi.goto(item.xPosition, self.position.y));
this.client.term.write(self.getANSIColor( this.client.term.write(index === self.focusedItemIndex ? this.getFocusSGR() : this.getSGR());
index === self.focusedItemIndex ? self.getFocusColor() : self.getColor()));
var text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle); var text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);

View File

@ -64,8 +64,8 @@ function View(options) {
this.dimens.width = options.dimens.width; this.dimens.width = options.dimens.width;
} }
this.color = options.color || { flags : 0, fg : 7, bg : 0 }; this.graphicRendition = options.graphicRendition || { fg : 7, bg : 0, styles : [ 0 ] };
this.focusColor = options.focusColor || this.color; this.focusGraphicRendition = options.focusGraphicRendition || this.graphicRendition;
if(options.styleColor1) { if(options.styleColor1) {
this.styleColor1 = options.styleColor1; this.styleColor1 = options.styleColor1;
@ -134,6 +134,7 @@ View.prototype.setPosition = function(pos) {
'Y position ' + this.position.y + ' out of terminal range ' + this.client.term.termWidth); 'Y position ' + this.position.y + ' out of terminal range ' + this.client.term.termWidth);
}; };
/*
View.prototype.setColor = function(color, bgColor, flags) { View.prototype.setColor = function(color, bgColor, flags) {
if(_.isObject(color)) { if(_.isObject(color)) {
assert(_.has(color, 'fg')); assert(_.has(color, 'fg'));
@ -164,14 +165,23 @@ View.prototype.setColor = function(color, bgColor, flags) {
this.color.bg = ansi.getBGColorValue(this.color.bg); this.color.bg = ansi.getBGColorValue(this.color.bg);
} }
}; };
*/
View.prototype.getColor = function() { View.prototype.getGraphicRendition = function() {
return this.color; return this.graphicRendition;
}; }
View.prototype.getFocusColor = function() { View.prototype.getFocusGraphicRendition = function() {
return this.focusColor; return this.focusGraphicRendition;
}; }
View.prototype.getSGR = function() {
return ansi.getSGRFromGraphicRendition(this.getGraphicRendition());
}
View.prototype.getFocusSGR = function() {
return ansi.getSGRFromGraphicRendition(this.getFocusGraphicRendition());
}
View.prototype.redraw = function() { View.prototype.redraw = function() {
this.client.term.write(ansi.goto(this.position.x, this.position.y)); this.client.term.write(ansi.goto(this.position.x, this.position.y));