From 7fc728724af69142ee7e1e89edfc50ee4e76493d Mon Sep 17 00:00:00 2001 From: nicoreed Date: Thu, 14 Jul 2011 15:59:52 -0700 Subject: [PATCH 1/4] [minor] added exports for the colors --- colors.js | 14 +++++++++++--- example.js | 8 +++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/colors.js b/colors.js index cfe3a35..9dcfc44 100644 --- a/colors.js +++ b/colors.js @@ -24,13 +24,21 @@ THE SOFTWARE. */ // prototypes the string object to have additional method calls that add terminal colors + +var addProperty = function (color, func) { + exports[color] = function(str) { + return func.apply(str); + }; + String.prototype.__defineGetter__(color, func); +} + var isHeadless = (typeof module !== 'undefined'); ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) { // __defineGetter__ at the least works in more browsers // http://robertnyman.com/javascript/javascript-getters-setters.html // Object.defineProperty only works in Chrome - String.prototype.__defineGetter__(style, function () { + addProperty(style, function () { return isHeadless ? stylize(this, style) : // for those running in node (headless environments) this.replace(/( )/, '$1'); // and for those running in browsers: @@ -40,7 +48,7 @@ var isHeadless = (typeof module !== 'undefined'); // prototypes string with method "rainbow" // rainbow will apply a the color spectrum to a string, changing colors every letter -String.prototype.__defineGetter__('rainbow', function () { +addProperty('rainbow', function () { if (!isHeadless) { return this.replace(/( )/, '$1'); } @@ -82,7 +90,7 @@ function stylize(str, style) { }; // don't summon zalgo -String.prototype.__defineGetter__('zalgo', function () { +addProperty('zalgo', function () { return zalgo(this); }); diff --git a/example.js b/example.js index 13b9fae..3003581 100644 --- a/example.js +++ b/example.js @@ -4,4 +4,10 @@ var colors = require('./colors'); sys.puts('Rainbows are fun!'.rainbow); sys.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported sys.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported -// sys.puts('zalgo time!'.zalgo); \ No newline at end of file +//sys.puts('zalgo time!'.zalgo); + + +sys.puts(colors.rainbow('Rainbows are fun!')); +sys.puts(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported +sys.puts(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported +//sys.puts(colors.zalgo('zalgo time!')); From 036bd80cdb358d430da096d2a5d3eab305e2d5b1 Mon Sep 17 00:00:00 2001 From: nicoreed Date: Thu, 14 Jul 2011 17:40:30 -0700 Subject: [PATCH 2/4] [minor] implemented a browser mode for issue #4 --- colors.js | 65 +++++++++++++++++++++++++++++++++++++----------------- example.js | 2 ++ 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/colors.js b/colors.js index 9dcfc44..e3f9009 100644 --- a/colors.js +++ b/colors.js @@ -23,6 +23,8 @@ THE SOFTWARE. */ +exports.mode = "console"; + // prototypes the string object to have additional method calls that add terminal colors var addProperty = function (color, func) { @@ -67,26 +69,49 @@ addProperty('rainbow', function () { }); function stylize(str, style) { - var styles = { - //styles - 'bold' : [1, 22], - 'italic' : [3, 23], - 'underline' : [4, 24], - 'inverse' : [7, 27], - //grayscale - 'white' : [37, 39], - 'grey' : [90, 39], - 'black' : [90, 39], - //colors - 'blue' : [34, 39], - 'cyan' : [36, 39], - 'green' : [32, 39], - 'magenta' : [35, 39], - 'red' : [31, 39], - 'yellow' : [33, 39] - }; - return '\033[' + styles[style][0] + 'm' + str + - '\033[' + styles[style][1] + 'm'; + if (exports.mode == 'console') { + var styles = { + //styles + 'bold' : ['\033[1m', '\033[22m'], + 'italic' : ['\033[3m', '\033[23m'], + 'underline' : ['\033[4m', '\033[24m'], + 'inverse' : ['\033[7m', '\033[27m'], + //grayscale + 'white' : ['\033[37m', '\033[39m'], + 'grey' : ['\033[90m', '\033[39m'], + 'black' : ['\033[90m', '\033[39m'], + //colors + 'blue' : ['\033[34m', '\033[39m'], + 'cyan' : ['\033[36m', '\033[39m'], + 'green' : ['\033[32m', '\033[39m'], + 'magenta' : ['\033[35m', '\033[39m'], + 'red' : ['\033[31m', '\033[39m'], + 'yellow' : ['\033[33m', '\033[39m'] + }; + } else if (exports.mode == 'browser') { + var styles = { + //styles + 'bold' : ['', ''], + 'italic' : ['', ''], + 'underline' : ['', ''], + 'inverse' : ['', ''], + //grayscale + 'white' : ['', ''], + 'grey' : ['', ''], + 'black' : ['', ''], + //colors + 'blue' : ['', ''], + 'cyan' : ['', ''], + 'green' : ['', ''], + 'magenta' : ['', ''], + 'red' : ['', ''], + 'yellow' : ['', ''] + }; + } else { + console.log('unsupported mode, try "browser" or "console"'); + } + + return styles[style][0] + str + styles[style][1]; }; // don't summon zalgo diff --git a/example.js b/example.js index 3003581..184a741 100644 --- a/example.js +++ b/example.js @@ -1,6 +1,8 @@ var sys = require('sys'); var colors = require('./colors'); +//colors.mode = "browser"; + sys.puts('Rainbows are fun!'.rainbow); sys.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported sys.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported From 5af04df3f34bbc4519d778166cca83a9a7a50a0b Mon Sep 17 00:00:00 2001 From: nicoreed Date: Thu, 14 Jul 2011 18:02:32 -0700 Subject: [PATCH 3/4] [minor] included an ascii code string stripper; fixes #1 --- colors.js | 4 ++++ example.js | 3 +++ 2 files changed, 7 insertions(+) diff --git a/colors.js b/colors.js index e3f9009..1eb96f6 100644 --- a/colors.js +++ b/colors.js @@ -222,3 +222,7 @@ function zalgo(text, options) { }; return heComes(text); } + +addProperty('stripColors', function() { + return ("" + this).replace(/\u001b\[\d+m/g,''); +}); diff --git a/example.js b/example.js index 184a741..6c8910c 100644 --- a/example.js +++ b/example.js @@ -3,13 +3,16 @@ var colors = require('./colors'); //colors.mode = "browser"; +var test = colors.red("hopefully colorless output"); sys.puts('Rainbows are fun!'.rainbow); sys.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported sys.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported //sys.puts('zalgo time!'.zalgo); +sys.puts(test.stripColors); sys.puts(colors.rainbow('Rainbows are fun!')); sys.puts(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported sys.puts(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported //sys.puts(colors.zalgo('zalgo time!')); +sys.puts(colors.stripColors(test)); From e32b28196ecb96014a6c097dce77d0a13fda01f1 Mon Sep 17 00:00:00 2001 From: nicoreed Date: Thu, 14 Jul 2011 18:08:57 -0700 Subject: [PATCH 4/4] [fix] fixed the black color to fix issue #6 --- colors.js | 4 ++-- example.js | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/colors.js b/colors.js index 1eb96f6..6ce4a11 100644 --- a/colors.js +++ b/colors.js @@ -35,7 +35,7 @@ var addProperty = function (color, func) { } var isHeadless = (typeof module !== 'undefined'); -['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) { +['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) { // __defineGetter__ at the least works in more browsers // http://robertnyman.com/javascript/javascript-getters-setters.html @@ -79,7 +79,7 @@ function stylize(str, style) { //grayscale 'white' : ['\033[37m', '\033[39m'], 'grey' : ['\033[90m', '\033[39m'], - 'black' : ['\033[90m', '\033[39m'], + 'black' : ['\033[30m', '\033[39m'], //colors 'blue' : ['\033[34m', '\033[39m'], 'cyan' : ['\033[36m', '\033[39m'], diff --git a/example.js b/example.js index 6c8910c..6aefa81 100644 --- a/example.js +++ b/example.js @@ -9,10 +9,12 @@ sys.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); sys.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported //sys.puts('zalgo time!'.zalgo); sys.puts(test.stripColors); - +sys.puts("a".grey + " b".black); sys.puts(colors.rainbow('Rainbows are fun!')); sys.puts(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported sys.puts(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported //sys.puts(colors.zalgo('zalgo time!')); sys.puts(colors.stripColors(test)); +sys.puts(colors.grey("a") + colors.black(" b")); +