UNPKG

4.65 kBMarkdownView Raw
1# colors.js
2[![Build Status](https://travis-ci.org/Marak/colors.js.svg?branch=master)](https://travis-ci.org/Marak/colors.js)
3[![version](https://img.shields.io/npm/v/colors.svg)](https://www.npmjs.org/package/colors)
4[![dependencies](https://david-dm.org/Marak/colors.js.svg)](https://david-dm.org/Marak/colors.js)
5[![devDependencies](https://david-dm.org/Marak/colors.js/dev-status.svg)](https://david-dm.org/Marak/colors.js#info=devDependencies)
6
7Please 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.
8
9## get color and style in your node.js console
10
11![Demo](https://raw.githubusercontent.com/Marak/colors.js/master/screenshots/colors.png)
12
13## Installation
14
15 npm install colors
16
17## colors and styles!
18
19### text colors
20
21 - black
22 - red
23 - green
24 - yellow
25 - blue
26 - magenta
27 - cyan
28 - white
29 - gray
30 - grey
31
32### bright text colors
33
34 - brightRed
35 - brightGreen
36 - brightYellow
37 - brightBlue
38 - brightMagenta
39 - brightCyan
40 - brightWhite
41
42### background colors
43
44 - bgBlack
45 - bgRed
46 - bgGreen
47 - bgYellow
48 - bgBlue
49 - bgMagenta
50 - bgCyan
51 - bgWhite
52 - bgGray
53 - bgGrey
54
55### bright background colors
56
57 - bgBrightRed
58 - bgBrightGreen
59 - bgBrightYellow
60 - bgBrightBlue
61 - bgBrightMagenta
62 - bgBrightCyan
63 - bgBrightWhite
64
65### styles
66
67 - reset
68 - bold
69 - dim
70 - italic
71 - underline
72 - inverse
73 - hidden
74 - strikethrough
75
76### extras
77
78 - rainbow
79 - zebra
80 - america
81 - trap
82 - random
83
84
85## Usage
86
87By popular demand, `colors` now ships with two types of usages!
88
89The super nifty way
90
91```js
92var colors = require('colors');
93
94console.log('hello'.green); // outputs green text
95console.log('i like cake and pies'.underline.red) // outputs red underlined text
96console.log('inverse the color'.inverse); // inverses the color
97console.log('OMG Rainbows!'.rainbow); // rainbow
98console.log('Run the trap'.trap); // Drops the bass
99
100```
101
102or a slightly less nifty way which doesn't extend `String.prototype`
103
104```js
105var colors = require('colors/safe');
106
107console.log(colors.green('hello')); // outputs green text
108console.log(colors.red.underline('i like cake and pies')) // outputs red underlined text
109console.log(colors.inverse('inverse the color')); // inverses the color
110console.log(colors.rainbow('OMG Rainbows!')); // rainbow
111console.log(colors.trap('Run the trap')); // Drops the bass
112
113```
114
115I prefer the first way. Some people seem to be afraid of extending `String.prototype` and prefer the second way.
116
117If 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.
118
119## Enabling/Disabling Colors
120
121The 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:
122
123```bash
124node myapp.js --no-color
125node myapp.js --color=false
126
127node myapp.js --color
128node myapp.js --color=true
129node myapp.js --color=always
130
131FORCE_COLOR=1 node myapp.js
132```
133
134Or in code:
135
136```javascript
137var colors = require('colors');
138colors.enable();
139colors.disable();
140```
141
142## Console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data)
143
144```js
145var name = 'Marak';
146console.log(colors.green('Hello %s'), name);
147// outputs -> 'Hello Marak'
148```
149
150## Custom themes
151
152### Using standard API
153
154```js
155
156var colors = require('colors');
157
158colors.setTheme({
159 silly: 'rainbow',
160 input: 'grey',
161 verbose: 'cyan',
162 prompt: 'grey',
163 info: 'green',
164 data: 'grey',
165 help: 'cyan',
166 warn: 'yellow',
167 debug: 'blue',
168 error: 'red'
169});
170
171// outputs red text
172console.log("this is an error".error);
173
174// outputs yellow text
175console.log("this is a warning".warn);
176```
177
178### Using string safe API
179
180```js
181var colors = require('colors/safe');
182
183// set single property
184var error = colors.red;
185error('this is red');
186
187// set theme
188colors.setTheme({
189 silly: 'rainbow',
190 input: 'grey',
191 verbose: 'cyan',
192 prompt: 'grey',
193 info: 'green',
194 data: 'grey',
195 help: 'cyan',
196 warn: 'yellow',
197 debug: 'blue',
198 error: 'red'
199});
200
201// outputs red text
202console.log(colors.error("this is an error"));
203
204// outputs yellow text
205console.log(colors.warn("this is a warning"));
206
207```
208
209### Combining Colors
210
211```javascript
212var colors = require('colors');
213
214colors.setTheme({
215 custom: ['red', 'underline']
216});
217
218console.log('test'.custom);
219```
220
221*Protip: There is a secret undocumented style in `colors`. If you find the style you can summon him.*