This commit is contained in:
Justin Campbell 2010-07-22 18:11:17 +00:00
parent c8cd0100f9
commit 54cffd2417
4 changed files with 37 additions and 13 deletions

View File

@ -8,6 +8,7 @@
sys.puts('hello'.green); // outputs green text sys.puts('hello'.green); // outputs green text
sys.puts('i like cake and pies'.underline.red) // outputs red underlined text sys.puts('i like cake and pies'.underline.red) // outputs red underlined text
sys.puts('inverse the color'.inverse); // inverses the color sys.puts('inverse the color'.inverse); // inverses the color
sys.puts('OMG Rainbows!'.rainbow); // rainbow (ignores spaces)
<h2>colors and styles!</h2> <h2>colors and styles!</h2>
- bold - bold
@ -26,4 +27,4 @@
### Authors ### Authors
#### Alexis Sellier (cloudhead) , Marak Squires #### Alexis Sellier (cloudhead) , Marak Squires , Justin Campbell

View File

@ -23,6 +23,7 @@ THE SOFTWARE.
*/ */
var sys = require('sys');
['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) { ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
Object.defineProperty(String.prototype, style, { Object.defineProperty(String.prototype, style, {
@ -32,20 +33,40 @@ THE SOFTWARE.
}); });
}); });
Object.defineProperty(String.prototype, 'rainbow', {
get: function () {
var rainbowcolors = ['red','yellow','green','blue','magenta']; //RoY G BiV
var exploded = this.split("");
var i=0;
exploded = exploded.map(function(letter) {
if (letter==" ") {
return letter;
} else {
return stylize(letter,rainbowcolors[i++ % rainbowcolors.length]);
}
});
return exploded.join("");
}
});
function stylize(str, style) { function stylize(str, style) {
var styles = { var styles = {
//styles
'bold' : [1, 22], 'bold' : [1, 22],
'italic' : [3, 23], 'italic' : [3, 23],
'underline' : [4, 24], 'underline' : [4, 24],
'yellow' : [33, 39], 'inverse' : [7, 27],
'cyan' : [36, 39], //grayscale
'white' : [37, 39], 'white' : [37, 39],
'green' : [32, 39],
'red' : [31, 39],
'grey' : [90, 39], 'grey' : [90, 39],
'black' : [90, 39],
//colors
'blue' : [34, 39], 'blue' : [34, 39],
'cyan' : [36, 39],
'green' : [32, 39],
'magenta' : [35, 39], 'magenta' : [35, 39],
'inverse' : [7, 27] 'red' : [31, 39],
'yellow' : [33, 39]
}; };
return '\033[' + styles[style][0] + 'm' + str + return '\033[' + styles[style][0] + 'm' + str +
'\033[' + styles[style][1] + 'm'; '\033[' + styles[style][1] + 'm';

View File

@ -1,6 +1,6 @@
var sys = require('sys'); var sys = require('sys');
var colors = require('./colors'); var colors = require('./colors');
sys.puts('C'.yellow+'o'.red+'l'.grey+'o'.cyan+'r'.magenta+'s'.magenta+' are '.green + 'fun!'.yellow); sys.puts('Colors are fun!'.rainbow);
sys.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported 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('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported

View File

@ -10,5 +10,7 @@
"engine": [ "engine": [
"node >=0.1.90" "node >=0.1.90"
], ],
"main": "colors" "main": "colors" ,
"licenses": [{ "type": "MIT" }]
"contributers": ["Alexis Sellier", "Justin Campbell"]
} }