UNPKG

2.11 kBMarkdownView Raw
1# ansicolors [![build status](https://secure.travis-ci.org/thlorenz/ansicolors.png)](http://next.travis-ci.org/thlorenz/ansicolors)
2
3Functions that surround a string with ansicolor codes so it prints in color.
4
5In case you need styles, like `bold`, have a look at [ansistyles](https://github.com/thlorenz/ansistyles).
6
7## Installation
8
9 npm install ansicolors
10
11## Usage
12
13```js
14var colors = require('ansicolors');
15
16// foreground colors
17var redHerring = colors.red('herring');
18var blueMoon = colors.blue('moon');
19var brighBlueMoon = colors.brightBlue('moon');
20
21console.log(redHerring); // this will print 'herring' in red
22console.log(blueMoon); // this 'moon' in blue
23console.log(brightBlueMoon); // I think you got the idea
24
25// background colors
26console.log(colors.bgYellow('printed on yellow background'));
27console.log(colors.bgBrightBlue('printed on bright blue background'));
28
29// mixing background and foreground colors
30// below two lines have same result (order in which bg and fg are combined doesn't matter)
31console.log(colors.bgYellow(colors.blue('printed on yellow background in blue')));
32console.log(colors.blue(colors.bgYellow('printed on yellow background in blue')));
33```
34
35## Advanced API
36
37**ansicolors** allows you to access opening and closing escape sequences separately.
38
39```js
40var colors = require('ansicolors');
41
42function inspect(obj, depth) {
43 return require('util').inspect(obj, false, depth || 5, true);
44}
45
46console.log('open blue', inspect(colors.open.blue));
47console.log('close bgBlack', inspect(colors.close.bgBlack));
48
49// => open blue '\u001b[34m'
50// close bgBlack '\u001b[49m'
51```
52
53## Tests
54
55Look at the [tests](https://github.com/thlorenz/ansicolors/blob/master/test/ansicolors.js) to see more examples and/or run them via:
56
57 npm explore ansicolors && npm test
58
59## Alternatives
60
61**ansicolors** tries to meet simple use cases with a very simple API. However, if you need a more powerful ansi formatting tool,
62I'd suggest to look at the [features](https://github.com/TooTallNate/ansi.js#features) of the [ansi module](https://github.com/TooTallNate/ansi.js).