diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5247ac9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +*~ +*.bak +.DS_Store +*.swo +*.swp +.*.swp +.*.swo +.*.bak +lib-cov +*.seed +*.log +*.csv +*.dat +*.out +*.pid +*.gz +pids +logs +result +node_modules +!.gitignore diff --git a/MIT-LICENSE b/MIT-LICENSE new file mode 100644 index 0000000..5d12661 --- /dev/null +++ b/MIT-LICENSE @@ -0,0 +1,22 @@ +(The MIT License) + +Copyright (c) 2012 Mario Gutierrez + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d2be2f2 --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +# mgutz-colors + +ANSI colors string library. + + +## Install + +npm install mgutz-colors + +## Using + +General usage + + var color = require("mgutz-colors").color; + + console.log(color("red on black", "red:black")); + +Style format is `color+attributes:bgColor+attributes` + + "red" // red + "red+b" // red bold + "red+u" // red underline + "red+bh" // red bold high-intensity + "red:white" // red on white + "red+b:white+h" // red bold on white high-intensity + +More flexible way to require it + + var colors = require("mgutz-colors"), + color = colors.color; + +Be lazy + + var bc = colors.fn("black:cyan"); + console.log(bc("this is black text on cyan")); + +Turn off colors easily + + colors.plain = true; + console.log(bc("this is plain now")); + +See all color combinations in your terminal + + npm test + +## License + +(The MIT License) + +Copyright (c) 2012 Mario Gutierrez + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/index.js b/index.js new file mode 100644 index 0000000..a06f7ee --- /dev/null +++ b/index.js @@ -0,0 +1,6 @@ +/*============================================================================ + * Copyright(c) 2010 Mario L Gutierrez + * MIT Licensed + *==========================================================================*/ + +module.exports = require('./lib/colors'); diff --git a/lib/colors.js b/lib/colors.js new file mode 100644 index 0000000..f6ad8a6 --- /dev/null +++ b/lib/colors.js @@ -0,0 +1,118 @@ +/*============================================================================ + * Copyright(c) 2010 Mario L Gutierrez + * MIT Licensed + *==========================================================================*/ + +/* Whether to colorize */ +exports.plain = false; + + +/** + * Colorizes string. To disable colors, set `colors.plain = true` + * + * @param {String} style Style format. + * + * format: + * color+attributes:bgColor+attributes + * + * color: + * black + * red + * green + * yellow + * blue + * magenta + * cyan + * white + * + * attributes: + * b = bold + * h = high intensity + * u = underline + * + * @example + * color("...", "red") -> red + * color("...", "red+b") -> red bold + * color("...", "red+u") -> red underline + * color("...", "red+bh") -> red bold high-intensity + * color("...", "red:white") -> red on white + * color("...", "red+b:white+h") -> red bold on white high-intensity + * + */ +var color = exports.color = function(s, style) { + if (exports.plain) { + return s; + } + + var colors = { + black: 0, + red: 1, + green: 2, + yellow: 3, + blue: 4, + magenta: 5, + cyan: 6, + white: 7 + }; + + var fgIntensity = { + normal: 30, + high: 90 + } + + var bgIntensity = { + normal: 40, + high: 100 + }; + + var decoration = { + bold: '1;', + underline: '4;' + }; + + var foreground_background = style.split(':'); + var foreground = foreground_background[0].split('+'); + var fg = foreground[0]; + var fgStyle = foreground[1]; + + var background = 0, bg = 0, bgStyle = ''; + if (foreground_background[1]) { + background = foreground_background[1].split('+'); + bg = background[0]; + bgStyle = background[1]; + } + + var colored = '\033['; + var base = fgIntensity.normal; + + if (fgStyle) { + if (fgStyle.indexOf('b') >= 0) colored += decoration.bold; + if (fgStyle.indexOf('u') >= 0) colored += decoration.underline; + base = fgStyle.indexOf('h') >= 0 ? fgIntensity.high : fgIntensity.normal; + } + colored += (base + colors[fg]).toString() + ';'; + + var base = 0; + if (bg) { + base = (bgStyle && bgStyle === 'h') ? bgIntensity.high : bgIntensity.normal; + colored += (base + colors[bg]).toString() + ';'; + } + + colored = colored.slice(0, -1); // remove last ';' + colored += 'm' + s + colored += "\033[0m"; // reset colors + + return colored; +} + + +/** + * Define a color function. + * + * @param {String} style The style format. + */ +exports.fn = function(style) { + return function(s) { + return color(s, style); + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..2f36e23 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "author": "Mario Gutierrez (http://mgutz.com)", + "name": "mgutz-colors", + "description": "ANSI colors string library.", + "version": "0.1.0", + "homepage": "https://github.com/mgutz/mgutz-colors", + "repository": { + "type": "git", + "url": "git://github.com/mgutz/mgutz-colors.git" + }, + "scripts": { + "test": "node test/colorsTest.js" + }, + "engines": { + "node": "~0.6" + }, + "dependencies": {}, + "devDependencies": {}, + "optionalDependencies": {} +} diff --git a/test/colorsTest.js b/test/colorsTest.js new file mode 100644 index 0000000..87b9508 --- /dev/null +++ b/test/colorsTest.js @@ -0,0 +1,56 @@ +/*============================================================================ + * Copyright(c) 2010 Mario L Gutierrez + * MIT Licensed + *==========================================================================*/ + +var Colors = require('../lib/colors'); +var color = Colors.color; + + +var colors = + ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']; +var bgColors = + ['', ':black', ':red', ':green', ':yellow', ':blue', ':magenta', ':cyan', ':white']; + + +function pad(s, length) { + while (s.length < length) { + s += ' '; + } + return s; +} + + +function padColor(s, styles) { + var buffer = ""; + for (var i = 0; i < styles.length; i++) { + buffer += color(pad(s + styles[i], 20), s + styles[i]); + } + return buffer; +} + + +var attr, s, s2, bg; +for (var b = 0; b < bgColors.length; b++) { + for (var c = 0; c < colors.length; c++) { + fg = colors[c]; + bg = bgColors[b]; + console.log(padColor(fg, [''+bg, '+b'+bg, '+bh'+bg, '+u'+bg, '+uh'+bg])); + console.log(padColor(fg, [''+bg+'+h', '+b'+bg+'+h', '+bh'+bg+'+h', '+u'+bg+'+h', '+uh'+bg+'+h'])); + } +} + +Colors.plain = true; +for (var b = 0; b < bgColors.length; b++) { + for (var c = 0; c < colors.length; c++) { + fg = colors[c]; + bg = bgColors[b]; + console.log(padColor(fg, [''+bg, '+b'+bg, '+bh'+bg, '+u'+bg, '+uh'+bg])); + console.log(padColor(fg, [''+bg+'+h', '+b'+bg+'+h', '+bh'+bg+'+h', '+u'+bg+'+h', '+uh'+bg+'+h'])); + } +} + + +Colors.plain = false; +var redBold = Colors.fn('red+b:white'); +console.log(redBold('red bold'));