UNPKG

5.97 kBMarkdownView Raw
1<h1 align="center">
2 <br>
3 <br>
4 <img width="360" 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)
13[![Coverage Status](https://coveralls.io/repos/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/r/chalk/chalk?branch=master)
14[![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4)
15
16
17[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). Although there are other ones, they either do too much or not enough.
18
19**Chalk is a clean and focused alternative.**
20
21![](https://github.com/chalk/ansi-styles/raw/master/screenshot.png)
22
23
24## Why
25
26- Highly performant
27- Doesn't extend `String.prototype`
28- Expressive API
29- Ability to nest styles
30- Clean and focused
31- Auto-detects color support
32- Actively maintained
33- [Used by ~7700 modules](https://www.npmjs.com/browse/depended/chalk) as of March 15, 2016
34
35
36## Install
37
38```
39$ npm install --save chalk
40```
41
42
43## Usage
44
45```js
46const chalk = require('chalk');
47
48console.log(chalk.blue('Hello world!'));
49```
50
51Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
52
53Here without `console.log` for purity.
54
55```js
56const chalk = require('chalk');
57
58// combine styled and normal strings
59chalk.blue('Hello') + 'World' + chalk.red('!');
60
61// compose multiple styles using the chainable API
62chalk.blue.bgRed.bold('Hello world!');
63
64// pass in multiple arguments
65chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');
66
67// nest styles
68chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
69
70// nest styles of the same type even (color, underline, background)
71chalk.green(
72 'I am a green line ' +
73 chalk.blue.underline.bold('with a blue substring') +
74 ' that becomes green again!'
75);
76
77// ES2015 template literal
78const systemStats = `
79CPU: ${chalk.red('90%')}
80RAM: ${chalk.green('40%')}
81DISK: ${chalk.yellow('70%')}
82`;
83```
84
85Easily define your own themes.
86
87```js
88const chalk = require('chalk');
89const error = chalk.bold.red;
90console.log(error('Error!'));
91```
92
93Take advantage of console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data).
94
95```js
96const name = 'Sindre';
97console.log(chalk.green('Hello %s'), name);
98//=> 'Hello Sindre'
99```
100
101
102## API
103
104### chalk.`<style>[.<style>...](string, [string...])`
105
106Example: `chalk.red.bold.underline('Hello', 'world');`
107
108Chain [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`.
109
110Multiple arguments will be separated by space.
111
112### chalk.enabled
113
114Color support is automatically detected, but you can override it by setting the `enabled` property. You should however only do this in your own code as it applies globally to all chalk consumers.
115
116If you need to change this in a reusable module create a new instance:
117
118```js
119const ctx = new chalk.constructor({enabled: false});
120```
121
122### chalk.supportsColor
123
124Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.
125
126Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, add an environment variable `FORCE_COLOR` with any value to force color. Trumps `--no-color`.
127
128### chalk.styles
129
130Exposes the styles as [ANSI escape codes](https://github.com/chalk/ansi-styles).
131
132Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with your own.
133
134```js
135const chalk = require('chalk');
136
137console.log(chalk.styles.red);
138//=> {open: '\u001b[31m', close: '\u001b[39m'}
139
140console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
141```
142
143
144## Styles
145
146### Modifiers
147
148- `reset`
149- `bold`
150- `dim`
151- `italic` *(not widely supported)*
152- `underline`
153- `inverse`
154- `hidden`
155- `strikethrough` *(not widely supported)*
156
157### Colors
158
159- `black`
160- `red`
161- `green`
162- `yellow`
163- `blue` *(on Windows the bright version is used as normal blue is illegible)*
164- `magenta`
165- `cyan`
166- `white`
167- `gray`
168
169### Background colors
170
171- `bgBlack`
172- `bgRed`
173- `bgGreen`
174- `bgYellow`
175- `bgBlue`
176- `bgMagenta`
177- `bgCyan`
178- `bgWhite`
179
180
181## 256-colors
182
183Chalk does not support anything other than the base eight colors, which guarantees it will work on all terminals and systems. Some terminals, specifically `xterm` compliant ones, will support the full range of 8-bit colors. For this the lower level [ansi-256-colors](https://github.com/jbnicolai/ansi-256-colors) package can be used.
184
185
186## Windows
187
188If you're on Windows, do yourself a favor and use [`cmder`](http://cmder.net/) instead of `cmd.exe`.
189
190
191## Related
192
193- [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
194- [ansi-styles](https://github.com/chalk/ansi-styles/) - ANSI escape codes for styling strings in the terminal
195- [supports-color](https://github.com/chalk/supports-color/) - Detect whether a terminal supports color
196- [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes
197- [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
198- [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
199- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
200- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
201
202
203## License
204
205MIT © [Sindre Sorhus](http://sindresorhus.com)