2017-03-08 23:34:49 +01:00
# colors.js
[](https://travis-ci.org/Marak/colors.js)
[](https://www.npmjs.org/package/colors)
[](https://david-dm.org/Marak/colors.js)
[](https://david-dm.org/Marak/colors.js#info =devDependencies)
2010-06-11 02:59:01 -04:00
2018-03-09 22:28:54 -08:00
Please check out the [roadmap ](ROADMAP.md ) for upcoming features and releases. Please open Issues to provide feedback, and check the `develop` branch for the latest bleeding-edge updates.
2018-02-16 14:57:36 -08:00
2014-09-30 22:41:24 +02:00
## get color and style in your node.js console
2015-01-24 16:55:56 +01:00

2010-06-11 08:01:54 -04:00
2011-12-09 03:32:41 -08:00
## Installation
2014-10-01 13:00:33 +02:00
npm install colors
2011-12-09 03:32:41 -08:00
## colors and styles!
2014-09-30 22:41:24 +02:00
### text colors
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
- gray
- grey
2019-09-22 16:36:30 -07:00
### bright text colors
- brightRed
- brightGreen
- brightYellow
- brightBlue
- brightMagenta
- brightCyan
- brightWhite
2014-09-30 22:41:24 +02:00
### background colors
- bgBlack
- bgRed
- bgGreen
- bgYellow
- bgBlue
- bgMagenta
- bgCyan
- bgWhite
2019-09-22 16:36:30 -07:00
- bgGray
- bgGrey
### bright background colors
- bgBrightRed
- bgBrightGreen
- bgBrightYellow
- bgBrightBlue
- bgBrightMagenta
- bgBrightCyan
- bgBrightWhite
2014-09-30 22:41:24 +02:00
### styles
- reset
- bold
- dim
- italic
- underline
- inverse
- hidden
- strikethrough
### extras
- rainbow
- zebra
- america
- trap
- random
2011-12-09 03:32:41 -08:00
## Usage
2014-09-30 22:41:24 +02:00
By popular demand, `colors` now ships with two types of usages!
The super nifty way
```js
2014-10-01 13:00:33 +02:00
var colors = require('colors');
2011-12-09 03:32:41 -08:00
console.log('hello'.green); // outputs green text
console.log('i like cake and pies'.underline.red) // outputs red underlined text
console.log('inverse the color'.inverse); // inverses the color
2014-09-30 22:41:24 +02:00
console.log('OMG Rainbows!'.rainbow); // rainbow
console.log('Run the trap'.trap); // Drops the bass
```
or a slightly less nifty way which doesn't extend `String.prototype`
```js
var colors = require('colors/safe');
console.log(colors.green('hello')); // outputs green text
console.log(colors.red.underline('i like cake and pies')) // outputs red underlined text
console.log(colors.inverse('inverse the color')); // inverses the color
console.log(colors.rainbow('OMG Rainbows!')); // rainbow
console.log(colors.trap('Run the trap')); // Drops the bass
```
I prefer the first way. Some people seem to be afraid of extending `String.prototype` and prefer the second way.
If you are writing good code you will never have an issue with the first approach. If you really don't want to touch `String.prototype` , the second usage will not touch `String` native object.
2019-09-22 17:12:39 -06:00
## Enabling/Disabling Colors
2014-09-30 22:41:24 +02:00
2019-09-22 17:12:39 -06:00
The package will auto-detect whether your terminal can use colors and enable/disable accordingly. When colors are disabled, the color functions do nothing. You can override this with a command-line flag:
2014-09-30 22:41:24 +02:00
```bash
node myapp.js --no-color
2019-09-22 17:12:39 -06:00
node myapp.js --color=false
node myapp.js --color
node myapp.js --color=true
node myapp.js --color=always
FORCE_COLOR=1 node myapp.js
```
Or in code:
```javascript
var colors = require('colors');
colors.enable();
colors.disable();
2011-12-09 03:32:41 -08:00
```
2014-10-01 13:24:15 +02:00
## Console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data)
2014-10-01 18:06:48 +02:00
```js
2014-10-01 13:24:15 +02:00
var name = 'Marak';
2014-10-02 23:01:14 +08:00
console.log(colors.green('Hello %s'), name);
2014-10-01 13:24:15 +02:00
// outputs -> 'Hello Marak'
2014-10-01 13:27:41 +02:00
```
2014-10-01 13:24:15 +02:00
2014-09-30 22:41:24 +02:00
## Custom themes
### Using standard API
2011-12-09 03:32:41 -08:00
```js
2011-12-09 15:12:14 -08:00
var colors = require('colors');
2011-12-09 03:32:41 -08:00
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
// outputs red text
console.log("this is an error".error);
// outputs yellow text
console.log("this is a warning".warn);
```
2014-09-30 22:41:24 +02:00
### Using string safe API
2011-12-09 03:32:41 -08:00
2014-09-30 22:41:24 +02:00
```js
var colors = require('colors/safe');
2010-06-11 03:04:50 -04:00
2014-09-30 22:41:24 +02:00
// set single property
var error = colors.red;
error('this is red');
2010-06-11 07:08:56 -04:00
2014-09-30 22:41:24 +02:00
// set theme
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
// outputs red text
console.log(colors.error("this is an error"));
// outputs yellow text
console.log(colors.warn("this is a warning"));
2015-01-24 16:55:56 +01:00
```
2015-06-17 15:42:07 +02:00
### Combining Colors
2015-01-24 16:55:56 +01:00
```javascript
2015-06-17 14:59:08 +02:00
var colors = require('colors');
2015-01-24 16:55:56 +01:00
colors.setTheme({
2015-06-17 14:59:08 +02:00
custom: ['red', 'underline']
2015-01-24 16:55:56 +01:00
});
2015-06-17 14:59:08 +02:00
console.log('test'.custom);
2014-09-30 22:41:24 +02:00
```
2010-06-11 07:10:02 -04:00
2015-01-24 16:55:56 +01:00
*Protip: There is a secret undocumented style in `colors` . If you find the style you can summon him.*