package/package.json000644 000765 000024 0000000705 12712677671013037 0ustar00000000 000000 {
"name": "bash-color",
"version" : "0.0.4",
"author" : "mykola bilokonsky ",
"description" : "a simple tool for wrapping strings in bash color codes. Used to color console output.",
"repository": {
"type": "git",
"url": "https://github.com/mbilokonsky/bash-color.git"
},
"keywords": [
"cli",
"bash",
"color",
"design",
"print",
"pretty print",
"color codes"
],
"license": "MIT",
"main": "./index.js"
}package/README.md000644 000765 000024 0000005714 12712677556012037 0ustar00000000 000000 bash-color
============
A node.js module for wrapping strings in color codes for pretty printing in bash. Usage is very simple. Install via NPM:
```bash
$ npm install bash-color
```
Then import it into your project:
```js
var color = require('bash-color');
console.log('Guess which word is ' + color.red('red') + ' when this is run?');
```
The 8 standard colors are explicitly supported as method names, and each takes a second argument which is a boolean (defaults to false) toggling hi-intensity.
```js
color.black('this text is black');
color.red('this text is high-intensity red', true);
color.green('this text is green');
color.yellow('this text is high-intensity yellow', true);
color.blue('this text is blue');
color.purple('this text is purple');
color.cyan('this text is cyan');
color.white('this text is white');
```
Additionally, the wrap() method allows you to pass in three arguments: your string, the color you want to use and a style value. Colors are enumerated as `color.colors`. Styles are enumerated as `color.styles`, and include bold, underline, background, high intensity text, high intensity bold text and high intensity background.
```js
color.wrap('this string will have a high-intensity blue background.', color.colors.BLUE, color.styles.hi_background);
color.wrap('this string will be red and underlined.', color.colors.RED, color.styles.underline);
```
Nesting things does NOT work. Bash codes can't nest this way - each color overwrites the previous, so you can't do one color over another. So this will fail:
```js
color.wrap(color.wrap("You might expect this text to be green on a high-intensity yellow background, but you'd be wrong.", color.colors.GREEN), color.colors.YELLOW, color.styles.hi_background);
```
Finally, all codes are exposed using the color.bash_codes property so if you wanted to wrap your own strings you could. For instance, this will work:
```js
var string = color.bash_codes.GREEN.text + "This text is green." + color.REMOVE_COLOR;
```
Just pay attention to that `color.REMOVE_COLOR` append - if you don't add that you may accidentally have all the rest of your console output colored until you do. The convenience methods all append that for you.
When to use bash-color and when not to use bash-color
===
Please understand the way bash color codes work: these methods actually prepend and append characters to your strings. These characters are only meaningful in a bash environment - they tell bash how to color the following text. If you try to add this stuff to strings that will be later rendered in an HTML page then you're going to see some weird characters show up - don't do that.
The whole point of this is that you can quickly and easily wrap pieces of text just before logging them to the console. This is useful in things like logging tools (`console.log(color.red('[error]') + ' - ' + err)`), or if you're building some sort of CLI interface and expect the user to interact directly with various color-coded pieces of text in the console.package/LICENSE000644 000765 000024 0000002072 12712677603011550 0ustar00000000 000000 The MIT License (MIT)
Copyright (c) 2014 Mykola Bilokonsky
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.package/color.js000644 000765 000024 0000007043 12712677556012231 0ustar00000000 000000 var bash_codes = exports.bash_codes = {
"BLACK" : {
"text" : "\033[0;30m",
"underline": "\033[4;30m",
"background": "\033[40m",
"bold":"\033[1;30m",
"hi_text":"\033[0;90m",
"hi_bold" : "\033[1;90m",
"hi_background" : "\033[0;100m"
},
"RED" : {
"text" : "\033[0;31m",
"bold":"\033[1;31m",
"underline": "\033[4;31m",
"background": "\033[41m",
"hi_text":"\033[0;91m",
"hi_bold" : "\033[1;91m",
"hi_background" : "\033[0;101m"
},
"GREEN" : {
"text" : "\033[0;32m",
"bold":"\033[1;32m",
"underline": "\033[4;32m",
"background": "\033[42m",
"hi_text":"\033[0;92m",
"hi_bold" : "\033[1;92m",
"hi_background" : "\033[0;102m"
},
"YELLOW" : {
"text" : "\033[0;33m",
"bold":"\033[1;33m",
"underline": "\033[4;33m",
"background": "\033[43m",
"hi_text":"\033[0;93m",
"hi_bold" : "\033[1;93m",
"hi_background" : "\033[0;103m"
},
"BLUE" : {
"text" : "\033[0;34m",
"bold":"\033[1;34m",
"underline": "\033[4;34m",
"background": "\033[44m",
"hi_text":"\033[0;94m",
"hi_bold" : "\033[1;94m",
"hi_background" : "\033[0;104m"
},
"PURPLE" : {
"text" : "\033[0;35m",
"bold":"\033[1;35m",
"underline": "\033[4;35m",
"background": "\033[45m",
"hi_text":"\033[0;95m",
"hi_bold" : "\033[1;95m",
"hi_background" : "\033[0;105m"
},
"CYAN" : {
"text" : "\033[0;36m",
"bold":"\033[1;36m",
"underline": "\033[4;36m",
"background": "\033[46m",
"hi_text":"\033[0;96m",
"hi_bold" : "\033[1;96m",
"hi_background" : "\033[0;106m"
},
"WHITE" : {
"text" : "\033[0;37m",
"bold":"\033[1;37m",
"underline": "\033[4;37m",
"background": "\033[47m",
"hi_text":"\033[0;97m",
"hi_bold" : "\033[1;97m",
"hi_background" : "\033[0;107m"
}
};
exports.colors = {
BLACK: "BLACK",
RED: "RED",
GREEN: "GREEN",
YELLOW: "YELLOW",
BLUE: "BLUE",
PURPLE: "PURPLE",
CYAN: "CYAN",
WHITE: "WHITE"
};
var styles = exports.styles = {
bold: "bold",
underline: "underline",
background: "background",
hi_text: "hi_text",
hi_bold: "hi_bold",
hi_background: "hi_background"
};
var REMOVE_COLOR = exports.REMOVE_COLOR = "\033[0m";
// various logical inconsistencies in the code below - renderColor and wrap seem like they should be combined, but I'm letting wrap basically stand on its own
// in case anyone wants access to explicitly handle background or underline stuff. I feel like those are a bit more special-casey, and generally speakign
// users are going to want to quickly turn a word or phrase into a single color without worrying about background or underline. So the .colorName methods
// are just syntactic sugar.
exports.wrap = function(str, color, style) {
var c = bash_codes[color.toUpperCase()];
var s = styles[style] || "text";
return render(c[s], str);
};
exports.black = function(str, hi) {
return renderColor(str, bash_codes.BLACK, hi);
};
exports.red = function(str, hi) {
return renderColor(str, bash_codes.RED, hi);
};
exports.green = function(str, hi) {
return renderColor(str, bash_codes.GREEN, hi);
};
exports.yellow = function(str, hi) {
return renderColor(str, bash_codes.YELLOW, hi);
};
exports.blue = function(str, hi) {
return renderColor(str, bash_codes.BLUE, hi);
};
exports.purple = function(str, hi) {
return renderColor(str, bash_codes.PURPLE, hi);
};
exports.cyan = function(str, hi) {
return renderColor(str, bash_codes.CYAN, hi);
};
exports.white = function(str, hi) {
return renderColor(str, bash_codes.WHITE, hi);
};
function renderColor(str, color, hi) {
return render(hi ? color.hi_text : color.text, str);
}
function render(code, str) {
return code + str + REMOVE_COLOR;
}package/index.js000644 000765 000024 0000007043 12712677556012222 0ustar00000000 000000 var bash_codes = exports.bash_codes = {
"BLACK" : {
"text" : "\033[0;30m",
"underline": "\033[4;30m",
"background": "\033[40m",
"bold":"\033[1;30m",
"hi_text":"\033[0;90m",
"hi_bold" : "\033[1;90m",
"hi_background" : "\033[0;100m"
},
"RED" : {
"text" : "\033[0;31m",
"bold":"\033[1;31m",
"underline": "\033[4;31m",
"background": "\033[41m",
"hi_text":"\033[0;91m",
"hi_bold" : "\033[1;91m",
"hi_background" : "\033[0;101m"
},
"GREEN" : {
"text" : "\033[0;32m",
"bold":"\033[1;32m",
"underline": "\033[4;32m",
"background": "\033[42m",
"hi_text":"\033[0;92m",
"hi_bold" : "\033[1;92m",
"hi_background" : "\033[0;102m"
},
"YELLOW" : {
"text" : "\033[0;33m",
"bold":"\033[1;33m",
"underline": "\033[4;33m",
"background": "\033[43m",
"hi_text":"\033[0;93m",
"hi_bold" : "\033[1;93m",
"hi_background" : "\033[0;103m"
},
"BLUE" : {
"text" : "\033[0;34m",
"bold":"\033[1;34m",
"underline": "\033[4;34m",
"background": "\033[44m",
"hi_text":"\033[0;94m",
"hi_bold" : "\033[1;94m",
"hi_background" : "\033[0;104m"
},
"PURPLE" : {
"text" : "\033[0;35m",
"bold":"\033[1;35m",
"underline": "\033[4;35m",
"background": "\033[45m",
"hi_text":"\033[0;95m",
"hi_bold" : "\033[1;95m",
"hi_background" : "\033[0;105m"
},
"CYAN" : {
"text" : "\033[0;36m",
"bold":"\033[1;36m",
"underline": "\033[4;36m",
"background": "\033[46m",
"hi_text":"\033[0;96m",
"hi_bold" : "\033[1;96m",
"hi_background" : "\033[0;106m"
},
"WHITE" : {
"text" : "\033[0;37m",
"bold":"\033[1;37m",
"underline": "\033[4;37m",
"background": "\033[47m",
"hi_text":"\033[0;97m",
"hi_bold" : "\033[1;97m",
"hi_background" : "\033[0;107m"
}
};
exports.colors = {
BLACK: "BLACK",
RED: "RED",
GREEN: "GREEN",
YELLOW: "YELLOW",
BLUE: "BLUE",
PURPLE: "PURPLE",
CYAN: "CYAN",
WHITE: "WHITE"
};
var styles = exports.styles = {
bold: "bold",
underline: "underline",
background: "background",
hi_text: "hi_text",
hi_bold: "hi_bold",
hi_background: "hi_background"
};
var REMOVE_COLOR = exports.REMOVE_COLOR = "\033[0m";
// various logical inconsistencies in the code below - renderColor and wrap seem like they should be combined, but I'm letting wrap basically stand on its own
// in case anyone wants access to explicitly handle background or underline stuff. I feel like those are a bit more special-casey, and generally speakign
// users are going to want to quickly turn a word or phrase into a single color without worrying about background or underline. So the .colorName methods
// are just syntactic sugar.
exports.wrap = function(str, color, style) {
var c = bash_codes[color.toUpperCase()];
var s = styles[style] || "text";
return render(c[s], str);
};
exports.black = function(str, hi) {
return renderColor(str, bash_codes.BLACK, hi);
};
exports.red = function(str, hi) {
return renderColor(str, bash_codes.RED, hi);
};
exports.green = function(str, hi) {
return renderColor(str, bash_codes.GREEN, hi);
};
exports.yellow = function(str, hi) {
return renderColor(str, bash_codes.YELLOW, hi);
};
exports.blue = function(str, hi) {
return renderColor(str, bash_codes.BLUE, hi);
};
exports.purple = function(str, hi) {
return renderColor(str, bash_codes.PURPLE, hi);
};
exports.cyan = function(str, hi) {
return renderColor(str, bash_codes.CYAN, hi);
};
exports.white = function(str, hi) {
return renderColor(str, bash_codes.WHITE, hi);
};
function renderColor(str, color, hi) {
return render(hi ? color.hi_text : color.text, str);
}
function render(code, str) {
return code + str + REMOVE_COLOR;
}