diff --git a/colors.js b/colors.js
index cfe3a35..6ce4a11 100644
--- a/colors.js
+++ b/colors.js
@@ -23,14 +23,24 @@ THE SOFTWARE.
*/
+exports.mode = "console";
+
// 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) {
+['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
// 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 +50,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');
}
@@ -59,30 +69,53 @@ String.prototype.__defineGetter__('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[30m', '\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
-String.prototype.__defineGetter__('zalgo', function () {
+addProperty('zalgo', function () {
return zalgo(this);
});
@@ -189,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 13b9fae..6aefa81 100644
--- a/example.js
+++ b/example.js
@@ -1,7 +1,20 @@
var sys = require('sys');
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);
\ No newline at end of file
+//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"));
+