Skip to content
Browse files

[api] Added custom themes using colors.setTheme. Cleaned up browser d…

…etection logic.
  • Loading branch information...
1 parent 2f03cd9 commit e4decd0fb2aa379c76f39754b9ed4513b7a541d4 @Marak committed Dec 9, 2011
Showing with 200 additions and 42 deletions.
  1. +4 −1 MIT-LICENSE.txt
  2. +58 −11 ReadMe.md
  3. +41 −12 colors.js
  4. +62 −8 example.html
  5. +35 −10 example.js
View
5 MIT-LICENSE.txt
@@ -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
of this software and associated documentation files (the "Software"), to deal
View
69 ReadMe.md
@@ -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"/>
- var sys = require('sys');
- var colors = require('./colors');
- sys.puts('hello'.green); // outputs green 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('OMG Rainbows!'.rainbow); // rainbow (ignores spaces)
-
-<h2>colors and styles!</h2>
+## Installation
+
+ npm install colors
+
+## colors and styles!
+
- bold
- italic
- underline
@@ -23,8 +21,57 @@
- red
- grey
- 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);
+```
+
+
+### Contributors
+Marak (Marak Squires)
+Alexis Sellier (cloudhead)
+mmalecki (Maciej Małecki)
+nicoreed (Nico Reed)
+morganrallen (Morgan Allen)
+JustinCampbell (Justin Campbell)
+ded (Dustin Diaz)
-### Authors
-#### Alexis Sellier (cloudhead) , Marak Squires , Justin Campbell, Dustin Diaz (@ded)
+#### , Marak Squires , Justin Campbell, Dustin Diaz (@ded)
View
53 colors.js
@@ -1,7 +1,10 @@
/*
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
of this software and associated documentation files (the "Software"), to deal
@@ -23,28 +26,47 @@ THE SOFTWARE.
*/
-exports.mode = "console";
+var isHeadless = false;
+
+if (typeof module !== 'undefined') {
+ isHeadless = true;
+}
-// prototypes the string object to have additional method calls that add terminal colors
+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 allowOverride = ['bold'];
+ if (""[color] && allowOverride.indexOf(color) === -1) {
+ throw new Error(color + ' already exists on String.prototype, cannot override.')
@indexzero
indexzero added a note Dec 9, 2011

This explodes pretty hard all over the place. If two versions of colors exist on disk then this will always throw. This is extremely common in development scenarios with npm link but also if two packages depended on by a third both depend on colors but the parent does not.

  parent-app
  `--- pkg-a
  |   `---colors
  `--- pkg-b
     `---colors

See this gist for a good repo: https://gist.github.com/1451296

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
exports[color] = function(str) {
return func.apply(str);
};
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
// http://robertnyman.com/javascript/javascript-getters-setters.html
// Object.defineProperty only works in Chrome
addProperty(style, function () {
- return isHeadless ?
- 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
+ return stylize(this, style);
});
});
@@ -58,8 +80,8 @@ function sequencer(map) {
exploded = exploded.map(map);
return exploded.join("");
}
-}
-
+}
+
var rainbowMap = (function () {
var rainbowColors = ['red','yellow','green','blue','magenta']; //RoY G BiV
return function (letter, i, exploded) {
@@ -80,8 +102,16 @@ exports.addSequencer('zebra', function (letter, i, exploded) {
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) {
+
if (exports.mode == 'console') {
var styles = {
//styles
@@ -125,7 +155,6 @@ function stylize(str, style) {
} else {
console.log('unsupported mode, try "browser", "console" or "none"');
}
-
return styles[style][0] + str + styles[style][1];
};
View
70 example.html
@@ -4,17 +4,71 @@
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Colors Example</title>
<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>
<body>
<script>
- document.write('Rainbows are fun!'.rainbow + '<br>');
- document.write('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse + '<br>');
- document.write('Chains are also cool.'.bold.italic.underline.red);
+
+ var test = colors.red("hopefully colorless output");
+
+ 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>
</body>
</html>
View
45 example.js
@@ -20,21 +20,46 @@ console.log(colors.stripColors(test));
console.log(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;
- }
+ 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'];
+ 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))]];
- };
+ return function(letter, i, exploded) {
+ return letter === " " ? letter : letter[available[Math.round(Math.random() * (available.length - 1))]];
+ };
})());
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);
+
+//
+// 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);
+
+

0 comments on commit e4decd0

Please sign in to comment.
Something went wrong with that request. Please try again.