diff --git a/MIT-LICENSE.txt b/MIT-LICENSE.txt index 7df0d5eb..7dca1070 100644 --- a/MIT-LICENSE.txt +++ b/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 diff --git a/ReadMe.md b/ReadMe.md index 74aceadd..1c6b0d07 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,16 +1,14 @@ -

colors.js - get color and style in your node.js console like what

+# colors.js - get color and style in your node.js console ( and browser ) like what - 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) - -

colors and styles!

+## 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) diff --git a/colors.js b/colors.js index c50cc583..467c4a88 100644 --- a/colors.js +++ b/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.') + } 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]; }; diff --git a/example.html b/example.html index c9cd68c5..ab956494 100644 --- a/example.html +++ b/example.html @@ -4,17 +4,71 @@ Colors Example - \ No newline at end of file diff --git a/example.js b/example.js index a4e3628b..3da29864 100644 --- a/example.js +++ b/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); + +