From 48be2f69be43fd033e687f09341b5dfd1dedea95 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Tue, 28 Apr 2015 22:42:22 -0600 Subject: [PATCH] * Start work on new ANSI parser color/style system. Mostly notes. Will do in bulk later * Start of styleColorX concept --- core/ansi_escape_parser.js | 92 ++++++++++++++++++++++++------ core/ansi_term.js | 2 +- core/art.js | 4 +- core/mci_view_factory.js | 10 +++- core/toggle_menu_view.js | 6 +- core/view.js | 4 ++ core/view_controller.js | 14 +++++ mods/art/demo_spin_and_toggle.ans | Bin 640 -> 644 bytes mods/menu.json | 3 +- 9 files changed, 109 insertions(+), 26 deletions(-) diff --git a/core/ansi_escape_parser.js b/core/ansi_escape_parser.js index f1398820..083bedde 100644 --- a/core/ansi_escape_parser.js +++ b/core/ansi_escape_parser.js @@ -16,7 +16,8 @@ function ANSIEscapeParser(options) { this.column = 1; this.row = 1; - this.flags = 0x00; + this.style = 0x00; + //this.style = { 0 : true }; this.scrollBack = 0; options = miscUtil.valueWithDefault(options, { @@ -75,7 +76,9 @@ function ANSIEscapeParser(options) { //self.bgColor = 0; self.fgColor = 39; self.bgColor = 49; - self.flags = 0; + //self.style = { 0 : true }; + //delete self.style; + self.style = 0; }; self.rowUpdated = function() { @@ -124,7 +127,7 @@ function ANSIEscapeParser(options) { function getProcessedMCI(mci) { if(self.mciReplaceChar.length > 0) { - var eraseColor = ansi.sgr(self.eraseColor.flags, self.eraseColor.fgColor, self.eraseColor.bgColor); + var eraseColor = ansi.sgr(self.eraseColor.style, self.eraseColor.fgColor, self.eraseColor.bgColor); return eraseColor + new Array(mci.length + 1).join(self.mciReplaceChar); } else { return mci; @@ -164,7 +167,7 @@ function ANSIEscapeParser(options) { self.lastMciCode = fullMciCode; self.eraseColor = { - flags : self.flags, + flags : self.style, fgColor : self.fgColor, bgColor : self.bgColor, }; @@ -174,7 +177,7 @@ function ANSIEscapeParser(options) { self.emit('mci', mciCode, id, args); if(self.mciReplaceChar.length > 0) { - self.emit('chunk', ansi.sgr(self.eraseColor.flags, self.eraseColor.fgColor, self.eraseColor.bgColor)); + self.emit('chunk', ansi.sgr(self.eraseColor.style, self.eraseColor.fgColor, self.eraseColor.bgColor)); literal(new Array(match[0].length + 1).join(self.mciReplaceChar)); } else { literal(match[0]); @@ -278,33 +281,46 @@ function ANSIEscapeParser(options) { // set graphic rendition case 'm' : + + // :TODO: reset state here for new system for(i = 0, len = args.length; i < len; ++i) { arg = args[i]; - /*if(0x00 === arg) { - self.flags = 0x00; - self.resetColor(); - } else { - switch(Math.floor(arg / 10)) { - case 0 : self.flags |= arg; break; - case 3 : self.fgColor = arg; break; - case 4 : self.bgColor = arg; break; - //case 3 : self.fgColor = arg - 30; break; - //case 4 : self.bgColor = arg - 40; break; - } + + // :TODO: finish this system + // * style is map of styleName -> boolean + // * 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]) { + self.fgColor = arg;//ANSIEscapeParser.foregroundColors[arg]; + } else if(ANSIEscapeParser.backgroundColors[arg]) { + self.bgColor = arg;//ANSIEscapeParser.backgroundColors[arg]; + } else if(39 === arg) { + delete self.fgColor; + } else if(49 === arg) { + delete self.bgColor; + } else if(ANSIEscapeParser.styles[arg]) { + self.style = arg; } + */ + if(arg >= 30 && arg <= 37) { self.fgColor = arg; } else if(arg >= 40 && arg <= 47) { self.bgColor = arg; } else { - self.flags |= arg; + self.style |= arg; if(0 === arg) { self.resetColor(); - //self.flags = 0; + //self.style = 0; } } + } break; @@ -321,4 +337,42 @@ function ANSIEscapeParser(options) { this.resetColor(); } -util.inherits(ANSIEscapeParser, events.EventEmitter); \ No newline at end of file +util.inherits(ANSIEscapeParser, events.EventEmitter); + +ANSIEscapeParser.foregroundColors = { + 30 : 'black', + 31 : 'red', + 32 : 'green', + 33 : 'yellow', + 34 : 'blue', + 35 : 'magenta', + 36 : 'cyan', + 37 : 'white', + 90 : 'grey' +}; +Object.freeze(ANSIEscapeParser.foregroundColors); + +ANSIEscapeParser.backgroundColors = { + 40 : 'black', + 41 : 'red', + 42 : 'green', + 43 : 'yellow', + 44 : 'blue', + 45 : 'magenta', + 46 : 'cyan', + 47 : 'white' +}; +Object.freeze(ANSIEscapeParser.backgroundColors); + +ANSIEscapeParser.styles = { + 0 : 'default', + 1 : 'bright', + 2 : 'dim', + 5 : 'slow blink', + 6 : 'fast blink', + 7 : 'negative', + 8 : 'concealed', + 22 : 'normal', + 27 : 'positive', +}; +Object.freeze(ANSIEscapeParser.styles); \ No newline at end of file diff --git a/core/ansi_term.js b/core/ansi_term.js index 58bb7c26..b4f20a77 100644 --- a/core/ansi_term.js +++ b/core/ansi_term.js @@ -313,7 +313,7 @@ function sgr() { } result += SGRValues[args[i]]; } - } else if(typeof args[i] === 'number') { + } else if(typeof args[i] === 'number') { if(result.length > 0) { result += ';'; } diff --git a/core/art.js b/core/art.js index 8c343747..14e10cf1 100644 --- a/core/art.js +++ b/core/art.js @@ -459,7 +459,7 @@ function display(options, cb) { mci[mapItem].focusColor = { fg : parser.fgColor, bg : parser.bgColor, - flags : parser.flags, + flags : parser.style, }; mci[mapItem].focusArgs = args; } else { @@ -468,7 +468,7 @@ function display(options, cb) { color : { fg : parser.fgColor, bg : parser.bgColor, - flags : parser.flags, + flags : parser.style, }, code : mciCode, id : parseInt(id, 10), diff --git a/core/mci_view_factory.js b/core/mci_view_factory.js index aac8c93f..8a21edb0 100644 --- a/core/mci_view_factory.js +++ b/core/mci_view_factory.js @@ -179,9 +179,15 @@ MCIViewFactory.prototype.createFromMCI = function(mci) { break; case 'TM' : - setOption(0, 'textStyle'); + if(mci.args.length > 0) { + var color = { fg : parseInt(mci.args[0], 10), flags : 0 }; + if(mci.args.length > 1) { + color.bg = parseInt(mci.args[1], 10); + } + options.styleColor1 = color; + } - setFocusOption(0, 'focusTextStyle') + setFocusOption(0, 'focusTextStyle'); view = new ToggleMenuView(options); break; diff --git a/core/toggle_menu_view.js b/core/toggle_menu_view.js index c3e92b30..c055d5f0 100644 --- a/core/toggle_menu_view.js +++ b/core/toggle_menu_view.js @@ -46,7 +46,11 @@ ToggleMenuView.prototype.redraw = function() { item.text, i === this.focusedItemIndex && this.hasFocus ? this.focusTextStyle : this.textStyle); if(1 === i) { - this.client.term.write(this.getANSIColor(this.getColor()) + ' / '); // :TODO: We need a color for this!!! + //console.log(this.styleColor1) + var sepColor = this.getANSIColor(this.styleColor1 || this.getColor()); + console.log(sepColor.substr(1)) + //sepColor = '\u001b[0m\u001b[1;30m'; + this.client.term.write(sepColor + ' / '); } this.client.term.write(this.getANSIColor(i === this.focusedItemIndex ? this.getFocusColor() : this.getColor())); diff --git a/core/view.js b/core/view.js index 0c53a5bf..b73b26c6 100644 --- a/core/view.js +++ b/core/view.js @@ -67,6 +67,10 @@ function View(options) { this.color = options.color || { flags : 0, fg : 7, bg : 0 }; this.focusColor = options.focusColor || this.color; + if(options.styleColor1) { + this.styleColor1 = options.styleColor1; + } + if(this.acceptsInput) { this.specialKeyMap = options.specialKeyMap || VIEW_SPECIAL_KEY_MAP_DEFAULT; } diff --git a/core/view_controller.js b/core/view_controller.js index 819adf64..202daae2 100644 --- a/core/view_controller.js +++ b/core/view_controller.js @@ -183,6 +183,20 @@ function ViewController(options) { setViewProp('focusTextStyle'); setViewProp('maxLength'); setViewProp('width', function(v) { view.dimens.width = parseInt(v, 10); }); + + setViewProp('styleColor1', function(v) { + if(!_.has(v, 'fg')) { + return; + } + + var color = { + fg : v.fg, + bg : v.bg || 0, + flags : v.flags || 0 + }; + + view.styleColor1 = color; + }); setViewProp('fillChar', function(v) { if(_.isNumber(v)) { diff --git a/mods/art/demo_spin_and_toggle.ans b/mods/art/demo_spin_and_toggle.ans index 52d1be29cad76930d595c444053cdc9d26623c5f..470495b8aa75ebc93ad66265245f21f81183e8d0 100644 GIT binary patch delta 23 ecmZo*ZDHMTn2|-p*j#h+VaDl9%uJJGm{b5#j|NNt delta 18 acmZo+ZD8GSm~rxP#_3G|87C(&sQ>^%1_qD- diff --git a/mods/menu.json b/mods/menu.json index 7c12d13d..16235591 100644 --- a/mods/menu.json +++ b/mods/menu.json @@ -266,7 +266,8 @@ "items" : [ "Razor 1911", "DrinkOrDie", "TRSI" ] }, "TM3" : { - "items" : [ "Yarly", "Nowaii" ] + "items" : [ "Yarly", "Nowaii" ], + "styleColor1" : { "fg" : 30, "flags" : 1 } }, "BT8" : { "text" : "< Back",