UNPKG

10.4 kBMarkdownView Raw
1<h1 align="center">
2 <br>
3 <br>
4 <img width="320" src="https://cdn.rawgit.com/chalk/chalk/19935d6484811c5e468817f846b7b3d417d7bf4a/logo.svg" alt="chalk">
5 <br>
6 <br>
7 <br>
8</h1>
9
10> Terminal string styling done right
11
12[![Build Status](https://travis-ci.org/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk) [![Coverage Status](https://coveralls.io/repos/github/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/github/chalk/chalk?branch=master) [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
13
14### [See what's new in Chalk 2](https://github.com/chalk/chalk/releases/tag/v2.0.0)
15
16![](https://github.com/chalk/ansi-styles/raw/master/screenshot.png)
17
18
19## Highlights
20
21- Expressive API
22- Highly performant
23- Ability to nest styles
24- [256/Truecolor color support](#256-and-truecolor-color-support)
25- Auto-detects color support
26- Doesn't extend `String.prototype`
27- Clean and focused
28- Actively maintained
29- [Used by ~17,000 packages](https://www.npmjs.com/browse/depended/chalk) as of June 20th, 2017
30
31
32## Install
33
34```console
35$ npm install chalk
36```
37
38
39## Usage
40
41```js
42const chalk = require('chalk');
43
44console.log(chalk.blue('Hello world!'));
45```
46
47Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
48
49```js
50const chalk = require('chalk');
51const log = console.log;
52
53// Combine styled and normal strings
54log(chalk.blue('Hello') + 'World' + chalk.red('!'));
55
56// Compose multiple styles using the chainable API
57log(chalk.blue.bgRed.bold('Hello world!'));
58
59// Pass in multiple arguments
60log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
61
62// Nest styles
63log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
64
65// Nest styles of the same type even (color, underline, background)
66log(chalk.green(
67 'I am a green line ' +
68 chalk.blue.underline.bold('with a blue substring') +
69 ' that becomes green again!'
70));
71
72// ES2015 template literal
73log(`
74CPU: ${chalk.red('90%')}
75RAM: ${chalk.green('40%')}
76DISK: ${chalk.yellow('70%')}
77`);
78
79// ES2015 tagged template literal
80log(chalk`
81CPU: {red ${cpu.totalPercent}%}
82RAM: {green ${ram.used / ram.total * 100}%}
83DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
84`);
85
86// Use RGB colors in terminal emulators that support it.
87log(chalk.keyword('orange')('Yay for orange colored text!'));
88log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
89log(chalk.hex('#DEADED').bold('Bold gray!'));
90```
91
92Easily define your own themes:
93
94```js
95const chalk = require('chalk');
96
97const error = chalk.bold.red;
98const warning = chalk.keyword('orange');
99
100console.log(error('Error!'));
101console.log(warning('Warning!'));
102```
103
104Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args):
105
106```js
107const name = 'Sindre';
108console.log(chalk.green('Hello %s'), name);
109//=> 'Hello Sindre'
110```
111
112
113## API
114
115### chalk.`<style>[.<style>...](string, [string...])`
116
117Example: `chalk.red.bold.underline('Hello', 'world');`
118
119Chain [styles](#styles) and call the last one as a method with a string argument. Order doesn't matter, and later styles take precedent in case of a conflict. This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
120
121Multiple arguments will be separated by space.
122
123### chalk.enabled
124
125Color support is automatically detected, as is the level (see `chalk.level`). However, if you'd like to simply enable/disable Chalk, you can do so via the `.enabled` property.
126
127Chalk is enabled by default unless expicitly disabled via the constructor or `chalk.level` is `0`.
128
129If you need to change this in a reusable module, create a new instance:
130
131```js
132const ctx = new chalk.constructor({enabled: false});
133```
134
135### chalk.level
136
137Color support is automatically detected, but you can override it by setting the `level` property. You should however only do this in your own code as it applies globally to all Chalk consumers.
138
139If you need to change this in a reusable module, create a new instance:
140
141```js
142const ctx = new chalk.constructor({level: 0});
143```
144
145Levels are as follows:
146
1470. All colors disabled
1481. Basic color support (16 colors)
1492. 256 color support
1503. Truecolor support (16 million colors)
151
152### chalk.supportsColor
153
154Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.
155
156Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, add the environment variable `FORCE_COLOR=1` to forcefully enable color or `FORCE_COLOR=0` to forcefully disable. The use of `FORCE_COLOR` overrides all other color support checks.
157
158Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.
159
160
161## Styles
162
163### Modifiers
164
165- `reset`
166- `bold`
167- `dim`
168- `italic` *(Not widely supported)*
169- `underline`
170- `inverse`
171- `hidden`
172- `strikethrough` *(Not widely supported)*
173
174### Colors
175
176- `black`
177- `red`
178- `green`
179- `yellow`
180- `blue` *(On Windows the bright version is used since normal blue is illegible)*
181- `magenta`
182- `cyan`
183- `white`
184- `gray` ("bright black")
185- `redBright`
186- `greenBright`
187- `yellowBright`
188- `blueBright`
189- `magentaBright`
190- `cyanBright`
191- `whiteBright`
192
193### Background colors
194
195- `bgBlack`
196- `bgRed`
197- `bgGreen`
198- `bgYellow`
199- `bgBlue`
200- `bgMagenta`
201- `bgCyan`
202- `bgWhite`
203- `bgBlackBright`
204- `bgRedBright`
205- `bgGreenBright`
206- `bgYellowBright`
207- `bgBlueBright`
208- `bgMagentaBright`
209- `bgCyanBright`
210- `bgWhiteBright`
211
212
213## Tagged template literal
214
215Chalk can be used as a [tagged template literal](http://exploringjs.com/es6/ch_template-literals.html#_tagged-template-literals).
216
217```js
218const chalk = require('chalk');
219
220const miles = 18;
221const calculateFeet = miles => miles * 5280;
222
223console.log(chalk`
224 There are {bold 5280 feet} in a mile.
225 In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
226`);
227```
228
229Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`).
230
231Template styles are chained exactly like normal Chalk styles. The following two statements are equivalent:
232
233```js
234console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
235console.log(chalk`{bold.rgb(10,100,200) Hello!}`);
236```
237
238Note that function styles (`rgb()`, `hsl()`, `keyword()`, etc.) may not contain spaces between parameters.
239
240All interpolated values (`` chalk`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped.
241
242
243## 256 and Truecolor color support
244
245Chalk supports 256 colors and [Truecolor](https://gist.github.com/XVilka/8346728) (16 million colors) on supported terminal apps.
246
247Colors are downsampled from 16 million RGB values to an ANSI color format that is supported by the terminal emulator (or by specifying `{level: n}` as a Chalk option). For example, Chalk configured to run at level 1 (basic color support) will downsample an RGB value of #FF0000 (red) to 31 (ANSI escape for red).
248
249Examples:
250
251- `chalk.hex('#DEADED').underline('Hello, world!')`
252- `chalk.keyword('orange')('Some orange text')`
253- `chalk.rgb(15, 100, 204).inverse('Hello!')`
254
255Background versions of these models are prefixed with `bg` and the first level of the module capitalized (e.g. `keyword` for foreground colors and `bgKeyword` for background colors).
256
257- `chalk.bgHex('#DEADED').underline('Hello, world!')`
258- `chalk.bgKeyword('orange')('Some orange text')`
259- `chalk.bgRgb(15, 100, 204).inverse('Hello!')`
260
261The following color models can be used:
262
263- [`rgb`](https://en.wikipedia.org/wiki/RGB_color_model) - Example: `chalk.rgb(255, 136, 0).bold('Orange!')`
264- [`hex`](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet) - Example: `chalk.hex('#FF8800').bold('Orange!')`
265- [`keyword`](https://www.w3.org/wiki/CSS/Properties/color/keywords) (CSS keywords) - Example: `chalk.keyword('orange').bold('Orange!')`
266- [`hsl`](https://en.wikipedia.org/wiki/HSL_and_HSV) - Example: `chalk.hsl(32, 100, 50).bold('Orange!')`
267- [`hsv`](https://en.wikipedia.org/wiki/HSL_and_HSV) - Example: `chalk.hsl(32, 1, 1).bold('Orange!')`
268- [`hwb`](https://en.wikipedia.org/wiki/HWB_color_model) - Example: `chalk.hsl(32, 0, 50).bold('Orange!')`
269- `ansi16`
270- `ansi256`
271
272
273## Windows
274
275If you're on Windows, do yourself a favor and use [`cmder`](http://cmder.net/) instead of `cmd.exe`.
276
277
278## Origin story
279
280[colors.js](https://github.com/Marak/colors.js) used to be the most popular string styling module, but it has serious deficiencies like extending `String.prototype` which causes all kinds of [problems](https://github.com/yeoman/yo/issues/68) and the package is unmaintained. Although there are other packages, they either do too much or not enough. Chalk is a clean and focused alternative.
281
282
283## Related
284
285- [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
286- [ansi-styles](https://github.com/chalk/ansi-styles) - ANSI escape codes for styling strings in the terminal
287- [supports-color](https://github.com/chalk/supports-color) - Detect whether a terminal supports color
288- [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes
289- [strip-ansi-stream](https://github.com/chalk/strip-ansi-stream) - Strip ANSI escape codes from a stream
290- [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
291- [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
292- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
293- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
294- [color-convert](https://github.com/qix-/color-convert) - Converts colors between different models
295- [chalk-animation](https://github.com/bokub/chalk-animation) - Animate strings in the terminal
296- [gradient-string](https://github.com/bokub/gradient-string) - Apply color gradients to strings
297- [chalk-pipe](https://github.com/LitoMore/chalk-pipe) - Create chalk style schemes with simpler style strings
298
299
300## Maintainers
301
302- [Sindre Sorhus](https://github.com/sindresorhus)
303- [Josh Junon](https://github.com/qix-)
304
305
306## License
307
308MIT