A node.js module for wrapping strings in color codes for pretty printing in bash. Usage is very simple. Install via NPM:
$ npm install bash-color
Then import it into your project:
var color = ;console;
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.
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.
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:
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:
var string = colorbash_codesGREENtext + "This text is green." + colorREMOVE_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.
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.