[api] Added custom themes using colors.setTheme. Cleaned up browser detection logic.

This commit is contained in:
Marak Squires 2011-12-09 03:32:41 -08:00
parent 2f03cd933b
commit e4decd0fb2
5 changed files with 200 additions and 42 deletions

View File

@ -1,4 +1,7 @@
Copyright (c) 2010 Alexis Sellier (cloudhead) , Marak Squires Copyright (c) 2010
Marak Squires
Alexis Sellier (cloudhead)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -1,16 +1,14 @@
<h1>colors.js - get color and style in your node.js console like what</h1> # colors.js - get color and style in your node.js console ( and browser ) like what
<img src="http://i.imgur.com/goJdO.png" border = "0"/> <img src="http://i.imgur.com/goJdO.png" border = "0"/>
var sys = require('sys');
var colors = require('./colors');
sys.puts('hello'.green); // outputs green text ## Installation
sys.puts('i like cake and pies'.underline.red) // outputs red underlined text
sys.puts('inverse the color'.inverse); // inverses the color npm install colors
sys.puts('OMG Rainbows!'.rainbow); // rainbow (ignores spaces)
## colors and styles!
<h2>colors and styles!</h2>
- bold - bold
- italic - italic
- underline - underline
@ -23,8 +21,57 @@
- red - red
- grey - grey
- blue - blue
- rainbow
- zebra
- random
## Usage
``` js
var colors = require('./colors');
console.log('hello'.green); // outputs green text
console.log('i like cake and pies'.underline.red) // outputs red underlined text
console.log('inverse the color'.inverse); // inverses the color
console.log('OMG Rainbows!'.rainbow); // rainbow (ignores spaces)
```
# Creating Custom themes
```js
var require('colors');
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
// outputs red text
console.log("this is an error".error);
// outputs yellow text
console.log("this is a warning".warn);
```
### Authors ### Contributors
#### Alexis Sellier (cloudhead) , Marak Squires , Justin Campbell, Dustin Diaz (@ded) Marak (Marak Squires)
Alexis Sellier (cloudhead)
mmalecki (Maciej Małecki)
nicoreed (Nico Reed)
morganrallen (Morgan Allen)
JustinCampbell (Justin Campbell)
ded (Dustin Diaz)
#### , Marak Squires , Justin Campbell, Dustin Diaz (@ded)

View File

@ -1,7 +1,10 @@
/* /*
colors.js colors.js
Copyright (c) 2010 Alexis Sellier (cloudhead) , Marak Squires Copyright (c) 2010
Marak Squires
Alexis Sellier (cloudhead)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -23,28 +26,47 @@ THE SOFTWARE.
*/ */
exports.mode = "console"; var isHeadless = false;
// prototypes the string object to have additional method calls that add terminal colors if (typeof module !== 'undefined') {
isHeadless = true;
}
if (!isHeadless) {
var exports = {};
var module = {};
var colors = exports;
exports.mode = "browser";
} else {
exports.mode = "console";
}
//
// Prototypes the string object to have additional method calls that add terminal colors
//
var addProperty = function (color, func) { var addProperty = function (color, func) {
var allowOverride = ['bold'];
if (""[color] && allowOverride.indexOf(color) === -1) {
throw new Error(color + ' already exists on String.prototype, cannot override.')
}
exports[color] = function(str) { exports[color] = function(str) {
return func.apply(str); return func.apply(str);
}; };
String.prototype.__defineGetter__(color, func); String.prototype.__defineGetter__(color, func);
} }
var isHeadless = (typeof module !== 'undefined'); //
['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) { // Iterate through all default styles and colors
//
var x = ['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'];
x.forEach(function (style) {
// __defineGetter__ at the least works in more browsers // __defineGetter__ at the least works in more browsers
// http://robertnyman.com/javascript/javascript-getters-setters.html // http://robertnyman.com/javascript/javascript-getters-setters.html
// Object.defineProperty only works in Chrome // Object.defineProperty only works in Chrome
addProperty(style, function () { addProperty(style, function () {
return isHeadless ? return stylize(this, style);
stylize(this, style) : // for those running in node (headless environments)
this.replace(/( )/, '$1'); // and for those running in browsers:
// re: ^ you'd think 'return this' works (but doesn't) so replace coerces the string to be a real string
}); });
}); });
@ -80,8 +102,16 @@ exports.addSequencer('zebra', function (letter, i, exploded) {
return i % 2 === 0 ? letter : letter.inverse; return i % 2 === 0 ? letter : letter.inverse;
}); });
exports.setTheme = function (theme) {
Object.keys(theme).forEach(function(prop){
addProperty(prop, function(){
return exports[theme[prop]](this);
});
});
}
function stylize(str, style) { function stylize(str, style) {
if (exports.mode == 'console') { if (exports.mode == 'console') {
var styles = { var styles = {
//styles //styles
@ -125,7 +155,6 @@ function stylize(str, style) {
} else { } else {
console.log('unsupported mode, try "browser", "console" or "none"'); console.log('unsupported mode, try "browser", "console" or "none"');
} }
return styles[style][0] + str + styles[style][1]; return styles[style][0] + str + styles[style][1];
}; };

View File

@ -4,17 +4,71 @@
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Colors Example</title> <title>Colors Example</title>
<script src="colors.js"></script> <script src="colors.js"></script>
<script type="text/javascript">
console.log('Rainbows are fun!'.rainbow);
console.log('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse);
console.log('Chains are also cool.'.bold.italic.underline.red);
</script>
</head> </head>
<body> <body>
<script> <script>
document.write('Rainbows are fun!'.rainbow + '<br>');
document.write('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse + '<br>'); var test = colors.red("hopefully colorless output");
document.write('Chains are also cool.'.bold.italic.underline.red);
document.write('Rainbows are fun!'.rainbow + '<br/>');
document.write('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
document.write('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
//document.write('zalgo time!'.zalgo);
document.write(test.stripColors);
document.write("a".grey + " b".black);
document.write("Zebras are so fun!".zebra);
document.write(colors.rainbow('Rainbows are fun!'));
document.write(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
document.write(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
//document.write(colors.zalgo('zalgo time!'));
document.write(colors.stripColors(test));
document.write(colors.grey("a") + colors.black(" b"));
colors.addSequencer("america", function(letter, i, exploded) {
if(letter === " ") return letter;
switch(i%3) {
case 0: return letter.red;
case 1: return letter.white;
case 2: return letter.blue;
}
});
colors.addSequencer("random", (function() {
var available = ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'];
return function(letter, i, exploded) {
return letter === " " ? letter : letter[available[Math.round(Math.random() * (available.length - 1))]];
};
})());
document.write("AMERICA! F--K YEAH!".america);
document.write("So apparently I've been to Mars, with all the little green men. But you know, I don't recall.".random);
//
// Custom themes
//
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
// outputs red text
document.write("this is an error".error);
// outputs yellow text
document.write("this is a warning".warn);
</script> </script>
</body> </body>
</html> </html>

View File

@ -20,21 +20,46 @@ console.log(colors.stripColors(test));
console.log(colors.grey("a") + colors.black(" b")); console.log(colors.grey("a") + colors.black(" b"));
colors.addSequencer("america", function(letter, i, exploded) { colors.addSequencer("america", function(letter, i, exploded) {
if(letter === " ") return letter; if(letter === " ") return letter;
switch(i%3) { switch(i%3) {
case 0: return letter.red; case 0: return letter.red;
case 1: return letter.white; case 1: return letter.white;
case 2: return letter.blue; case 2: return letter.blue;
} }
}); });
colors.addSequencer("random", (function() { colors.addSequencer("random", (function() {
var available = ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta']; var available = ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'];
return function(letter, i, exploded) { return function(letter, i, exploded) {
return letter === " " ? letter : letter[available[Math.round(Math.random() * (available.length - 1))]]; return letter === " " ? letter : letter[available[Math.round(Math.random() * (available.length - 1))]];
}; };
})()); })());
console.log("AMERICA! F--K YEAH!".america); console.log("AMERICA! F--K YEAH!".america);
console.log("So apparently I've been to Mars, with all the little green men. But you know, I don't recall.".random); console.log("So apparently I've been to Mars, with all the little green men. But you know, I don't recall.".random);
//
// Custom themes
//
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
// outputs red text
console.log("this is an error".error);
// outputs yellow text
console.log("this is a warning".warn);