Giter Club home page Giter Club logo

bash-color's Introduction

bash-color

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 = 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.

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.

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:

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:

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.

bash-color's People

Contributors

christianbundy avatar mbilokonsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

bash-color's Issues

Can you add a License

Hi Mykola!

Thank you for your OSS contributions! We use your library at my company, but they need for us to know what License you are releasing this software as. Would you be willing to add a license to your package.json or in your README.md? I have personally always released my software under MIT, but obviously use whatever license you prefer, we just need to know what you have chosen so we can continue to use your software.

If you can provide information about if the license you choose applied to your previously released versions as well (and not just going forward) that would be awesome.

Thank you for your consideration!

Cheers,
Nowell

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.