Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mgutz committed Jan 28, 2012
0 parents commit be08bc4
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .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
22 changes: 22 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2012 Mario Gutierrez <mario@mgutz.com>

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.
69 changes: 69 additions & 0 deletions 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 <mario@mgutz.com>

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.
6 changes: 6 additions & 0 deletions index.js
@@ -0,0 +1,6 @@
/*============================================================================
* Copyright(c) 2010 Mario L Gutierrez <mario@mgutz.com>
* MIT Licensed
*==========================================================================*/

module.exports = require('./lib/colors');
118 changes: 118 additions & 0 deletions lib/colors.js
@@ -0,0 +1,118 @@
/*============================================================================
* Copyright(c) 2010 Mario L Gutierrez <mario@mgutz.com>
* 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);
}
}
20 changes: 20 additions & 0 deletions package.json
@@ -0,0 +1,20 @@
{
"author": "Mario Gutierrez <mario@mgutz.com> (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": {}
}
56 changes: 56 additions & 0 deletions test/colorsTest.js
@@ -0,0 +1,56 @@
/*============================================================================
* Copyright(c) 2010 Mario L Gutierrez <mario@mgutz.com>
* 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'));

0 comments on commit be08bc4

Please sign in to comment.