Comparing version 0.5.0 to 0.5.1
@@ -26,5 +26,15 @@ /* | ||
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) { | ||
@@ -34,3 +44,3 @@ // __defineGetter__ at the least works in more browsers | ||
// Object.defineProperty only works in Chrome | ||
String.prototype.__defineGetter__(style, function () { | ||
addProperty(style, function () { | ||
return isHeadless ? | ||
@@ -45,3 +55,3 @@ stylize(this, style) : // for those running in node (headless environments) | ||
// rainbow will apply a the color spectrum to a string, changing colors every letter | ||
String.prototype.__defineGetter__('rainbow', function () { | ||
addProperty('rainbow', function () { | ||
if (!isHeadless) { | ||
@@ -65,26 +75,51 @@ return this.replace(/( )/, '$1'); | ||
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' : ['<b>', '</b>'], | ||
'italic' : ['<i>', '</i>'], | ||
'underline' : ['<u>', '</u>'], | ||
'inverse' : ['<span style="background-color:black;color:white;">', '</span>'], | ||
//grayscale | ||
'white' : ['<span style="color:white;">', '</span>'], | ||
'grey' : ['<span style="color:grey;">', '</span>'], | ||
'black' : ['<span style="color:black;">', '</span>'], | ||
//colors | ||
'blue' : ['<span style="color:blue;">', '</span>'], | ||
'cyan' : ['<span style="color:cyan;">', '</span>'], | ||
'green' : ['<span style="color:green;">', '</span>'], | ||
'magenta' : ['<span style="color:magenta;">', '</span>'], | ||
'red' : ['<span style="color:red;">', '</span>'], | ||
'yellow' : ['<span style="color:yellow;">', '</span>'] | ||
}; | ||
} else if (exports.mode == 'none') { | ||
return str; | ||
} else { | ||
console.log('unsupported mode, try "browser", "console" or "none"'); | ||
} | ||
return styles[style][0] + str + styles[style][1]; | ||
}; | ||
// don't summon zalgo | ||
String.prototype.__defineGetter__('zalgo', function () { | ||
addProperty('zalgo', function () { | ||
return zalgo(this); | ||
@@ -196,1 +231,5 @@ }); | ||
} | ||
addProperty('stripColors', function() { | ||
return ("" + this).replace(/\u001b\[\d+m/g,''); | ||
}); |
@@ -1,7 +0,20 @@ | ||
var sys = require('sys'); | ||
var util = require('util'); | ||
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); | ||
//colors.mode = "browser"; | ||
var test = colors.red("hopefully colorless output"); | ||
util.puts('Rainbows are fun!'.rainbow); | ||
util.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported | ||
util.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported | ||
//util.puts('zalgo time!'.zalgo); | ||
util.puts(test.stripColors); | ||
util.puts("a".grey + " b".black); | ||
util.puts(colors.rainbow('Rainbows are fun!')); | ||
util.puts(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported | ||
util.puts(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported | ||
//util.puts(colors.zalgo('zalgo time!')); | ||
util.puts(colors.stripColors(test)); | ||
util.puts(colors.grey("a") + colors.black(" b")); | ||
{ | ||
"name": "colors", | ||
"description": "get colors in your node.js console like what", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"author": "Marak Squires", | ||
@@ -10,6 +10,6 @@ "repository": { | ||
}, | ||
"engine": [ | ||
"node >=0.1.90" | ||
], | ||
"engines": { | ||
"node": ">=0.1.90" | ||
}, | ||
"main": "colors" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11145
224