node-colors-sync
get color and style in your node.js console
Installation
npm install node-colors-sync
colors and styles!
text colors
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
- gray
- grey
bright text colors
- brightRed
- brightGreen
- brightYellow
- brightBlue
- brightMagenta
- brightCyan
- brightWhite
background colors
- bgBlack
- bgRed
- bgGreen
- bgYellow
- bgBlue
- bgMagenta
- bgCyan
- bgWhite
- bgGray
- bgGrey
bright background colors
- bgBrightRed
- bgBrightGreen
- bgBrightYellow
- bgBrightBlue
- bgBrightMagenta
- bgBrightCyan
- bgBrightWhite
styles
- reset
- bold
- dim
- italic
- underline
- inverse
- hidden
- strikethrough
- rainbow
- zebra
- america
- trap
- random
Usage
By popular demand, node-colors-sync
now ships with two types of usages!
The super nifty way
var colors = require('node-colors-sync');
console.log('hello'.green);
console.log('i like cake and pies'.underline.red);
console.log('inverse the color'.inverse);
console.log('OMG Rainbows!'.rainbow);
console.log('Run the trap'.trap);
or a slightly less nifty way which doesn't extend String.prototype
var colors = require('node-colors-sync/safe');
console.log(colors.green('hello'));
console.log(colors.red.underline('i like cake and pies'));
console.log(colors.inverse('inverse the color'));
console.log(colors.rainbow('OMG Rainbows!'));
console.log(colors.trap('Run the trap'));
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.
Enabling/Disabling Colors
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:
node myapp.js --no-color
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:
var colors = require('node-colors-sync');
colors.enable();
colors.disable();
var name = 'fuhrer';
console.log(colors.green('Hello %s'), name);
Custom themes
Using standard API
var colors = require('node-colors-sync');
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
console.log("this is an error".error);
console.log("this is a warning".warn);
Using string safe API
var colors = require('node-colors-sync/safe');
var error = colors.red;
error('this is red');
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
console.log(colors.error("this is an error"));
console.log(colors.warn("this is a warning"));
Combining Colors
var colors = require('node-colors-sync');
colors.setTheme({
custom: ['red', 'underline']
});
console.log('test'.custom);
Protip: There is a secret undocumented style in node-colors-sync
. If you find the style you can summon him.