UNPKG

11.9 kBMarkdownView Raw
1<h1 align="center">
2 <br>
3 <br>
4 <img width="320" src="media/logo.svg" alt="Chalk">
5 <br>
6 <br>
7 <br>
8</h1>
9
10> Terminal string styling done right
11
12[![Coverage Status](https://codecov.io/gh/chalk/chalk/branch/main/graph/badge.svg)](https://codecov.io/gh/chalk/chalk)
13[![npm dependents](https://badgen.net/npm/dependents/chalk)](https://www.npmjs.com/package/chalk?activeTab=dependents) [![Downloads](https://badgen.net/npm/dt/chalk)](https://www.npmjs.com/package/chalk)
14[![run on repl.it](https://repl.it/badge/github/chalk/chalk)](https://repl.it/github/chalk/chalk)
15[![Support Chalk on DEV](https://badge.devprotocol.xyz/0x44d871aebF0126Bf646753E2C976Aa7e68A66c15/descriptive)](https://stakes.social/0x44d871aebF0126Bf646753E2C976Aa7e68A66c15)
16
17<img src="https://cdn.jsdelivr.net/gh/chalk/ansi-styles@8261697c95bf34b6c7767e2cbe9941a851d59385/screenshot.svg" width="900">
18
19<br>
20
21---
22
23<div align="center">
24 <p>
25 <p>
26 <sup>
27 Sindre Sorhus' open source work is supported by the community on <a href="https://github.com/sponsors/sindresorhus">GitHub Sponsors</a> and <a href="https://stakes.social/0x44d871aebF0126Bf646753E2C976Aa7e68A66c15">Dev</a>
28 </sup>
29 </p>
30 <sup>Special thanks to:</sup>
31 <br>
32 <br>
33 <a href="https://standardresume.co/tech">
34 <img src="https://sindresorhus.com/assets/thanks/standard-resume-logo.svg" width="160"/>
35 </a>
36 <br>
37 <br>
38 <a href="https://retool.com/?utm_campaign=sindresorhus">
39 <img src="https://sindresorhus.com/assets/thanks/retool-logo.svg" width="230"/>
40 </a>
41 <br>
42 <br>
43 <a href="https://doppler.com/?utm_campaign=github_repo&utm_medium=referral&utm_content=chalk&utm_source=github">
44 <div>
45 <img src="https://dashboard.doppler.com/imgs/logo-long.svg" width="240" alt="Doppler">
46 </div>
47 <b>All your environment variables, in one place</b>
48 <div>
49 <span>Stop struggling with scattered API keys, hacking together home-brewed tools,</span>
50 <br>
51 <span>and avoiding access controls. Keep your team and servers in sync with Doppler.</span>
52 </div>
53 </a>
54 <br>
55 <a href="https://strapi.io/?ref=sindresorhus">
56 <div>
57 <img src="https://sindresorhus.com/assets/thanks/strapi-logo-white-bg.png" width="220" alt="Strapi">
58 </div>
59 <b>Strapi is the leading open-source headless CMS.</b>
60 <div>
61 <sup>It’s 100% JavaScript, fully customizable, and developer-first.</sup>
62 </div>
63 </a>
64 </p>
65</div>
66
67---
68
69<br>
70
71## Highlights
72
73- Expressive API
74- Highly performant
75- No dependencies
76- Ability to nest styles
77- [256/Truecolor color support](#256-and-truecolor-color-support)
78- Auto-detects color support
79- Doesn't extend `String.prototype`
80- Clean and focused
81- Actively maintained
82- [Used by ~76,000 packages](https://www.npmjs.com/browse/depended/chalk) as of October 26, 2021
83
84## Install
85
86```sh
87npm install chalk
88```
89
90**IMPORTANT:** Chalk 5 is ESM. If you want to use Chalk with TypeScript or a build tool, you will probably want to use Chalk 4 for now. [Read more.](https://github.com/chalk/chalk/releases/tag/v5.0.0)
91
92## Usage
93
94```js
95import chalk from 'chalk';
96
97console.log(chalk.blue('Hello world!'));
98```
99
100Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
101
102```js
103import chalk from 'chalk';
104
105const log = console.log;
106
107// Combine styled and normal strings
108log(chalk.blue('Hello') + ' World' + chalk.red('!'));
109
110// Compose multiple styles using the chainable API
111log(chalk.blue.bgRed.bold('Hello world!'));
112
113// Pass in multiple arguments
114log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
115
116// Nest styles
117log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
118
119// Nest styles of the same type even (color, underline, background)
120log(chalk.green(
121 'I am a green line ' +
122 chalk.blue.underline.bold('with a blue substring') +
123 ' that becomes green again!'
124));
125
126// ES2015 template literal
127log(`
128CPU: ${chalk.red('90%')}
129RAM: ${chalk.green('40%')}
130DISK: ${chalk.yellow('70%')}
131`);
132
133// Use RGB colors in terminal emulators that support it.
134log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
135log(chalk.hex('#DEADED').bold('Bold gray!'));
136```
137
138Easily define your own themes:
139
140```js
141import chalk from 'chalk';
142
143const error = chalk.bold.red;
144const warning = chalk.hex('#FFA500'); // Orange color
145
146console.log(error('Error!'));
147console.log(warning('Warning!'));
148```
149
150Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args):
151
152```js
153import chalk from 'chalk';
154
155const name = 'Sindre';
156console.log(chalk.green('Hello %s'), name);
157//=> 'Hello Sindre'
158```
159
160## API
161
162### chalk.`<style>[.<style>...](string, [string...])`
163
164Example: `chalk.red.bold.underline('Hello', 'world');`
165
166Chain [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`.
167
168Multiple arguments will be separated by space.
169
170### chalk.level
171
172Specifies the level of color support.
173
174Color 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.
175
176If you need to change this in a reusable module, create a new instance:
177
178```js
179import {Chalk} from 'chalk';
180
181const customChalk = new Chalk({level: 0});
182```
183
184| Level | Description |
185| :---: | :--- |
186| `0` | All colors disabled |
187| `1` | Basic color support (16 colors) |
188| `2` | 256 color support |
189| `3` | Truecolor support (16 million colors) |
190
191### supportsColor
192
193Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.
194
195Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, use the environment variable `FORCE_COLOR=1` (level 1), `FORCE_COLOR=2` (level 2), or `FORCE_COLOR=3` (level 3) to forcefully enable color, or `FORCE_COLOR=0` to forcefully disable. The use of `FORCE_COLOR` overrides all other color support checks.
196
197Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.
198
199### chalkStderr and supportsColorStderr
200
201`chalkStderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `supportsColor` apply to this too. `supportsColorStderr` is exposed for convenience.
202
203## Styles
204
205### Modifiers
206
207- `reset` - Reset the current style.
208- `bold` - Make the text bold.
209- `dim` - Make the text have lower opacity.
210- `italic` - Make the text italic. *(Not widely supported)*
211- `underline` - Put a horizontal line below the text. *(Not widely supported)*
212- `overline` - Put a horizontal line above the text. *(Not widely supported)*
213- `inverse`- Invert background and foreground colors.
214- `hidden` - Print the text but make it invisible.
215- `strikethrough` - Puts a horizontal line through the center of the text. *(Not widely supported)*
216- `visible`- Print the text only when Chalk has a color level above zero. Can be useful for things that are purely cosmetic.
217
218### Colors
219
220- `black`
221- `red`
222- `green`
223- `yellow`
224- `blue`
225- `magenta`
226- `cyan`
227- `white`
228- `blackBright` (alias: `gray`, `grey`)
229- `redBright`
230- `greenBright`
231- `yellowBright`
232- `blueBright`
233- `magentaBright`
234- `cyanBright`
235- `whiteBright`
236
237### Background colors
238
239- `bgBlack`
240- `bgRed`
241- `bgGreen`
242- `bgYellow`
243- `bgBlue`
244- `bgMagenta`
245- `bgCyan`
246- `bgWhite`
247- `bgBlackBright` (alias: `bgGray`, `bgGrey`)
248- `bgRedBright`
249- `bgGreenBright`
250- `bgYellowBright`
251- `bgBlueBright`
252- `bgMagentaBright`
253- `bgCyanBright`
254- `bgWhiteBright`
255
256## 256 and Truecolor color support
257
258Chalk supports 256 colors and [Truecolor](https://gist.github.com/XVilka/8346728) (16 million colors) on supported terminal apps.
259
260Colors 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).
261
262Examples:
263
264- `chalk.hex('#DEADED').underline('Hello, world!')`
265- `chalk.rgb(15, 100, 204).inverse('Hello!')`
266
267Background versions of these models are prefixed with `bg` and the first level of the module capitalized (e.g. `hex` for foreground colors and `bgHex` for background colors).
268
269- `chalk.bgHex('#DEADED').underline('Hello, world!')`
270- `chalk.bgRgb(15, 100, 204).inverse('Hello!')`
271
272The following color models can be used:
273
274- [`rgb`](https://en.wikipedia.org/wiki/RGB_color_model) - Example: `chalk.rgb(255, 136, 0).bold('Orange!')`
275- [`hex`](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet) - Example: `chalk.hex('#FF8800').bold('Orange!')`
276- [`ansi256`](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) - Example: `chalk.bgAnsi256(194)('Honeydew, more or less')`
277
278## Browser support
279
280Since Chrome 69, ANSI escape codes are natively supported in the developer console.
281
282## Windows
283
284If you're on Windows, do yourself a favor and use [Windows Terminal](https://github.com/microsoft/terminal) instead of `cmd.exe`.
285
286## Origin story
287
288[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.
289
290## chalk for enterprise
291
292Available as part of the Tidelift Subscription.
293
294The maintainers of chalk and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-chalk?utm_source=npm-chalk&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
295
296## Related
297
298- [chalk-template](https://github.com/chalk/chalk-template) - [Tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) support for this module
299- [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
300- [ansi-styles](https://github.com/chalk/ansi-styles) - ANSI escape codes for styling strings in the terminal
301- [supports-color](https://github.com/chalk/supports-color) - Detect whether a terminal supports color
302- [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes
303- [strip-ansi-stream](https://github.com/chalk/strip-ansi-stream) - Strip ANSI escape codes from a stream
304- [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
305- [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
306- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
307- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
308- [color-convert](https://github.com/qix-/color-convert) - Converts colors between different models
309- [chalk-animation](https://github.com/bokub/chalk-animation) - Animate strings in the terminal
310- [gradient-string](https://github.com/bokub/gradient-string) - Apply color gradients to strings
311- [chalk-pipe](https://github.com/LitoMore/chalk-pipe) - Create chalk style schemes with simpler style strings
312- [terminal-link](https://github.com/sindresorhus/terminal-link) - Create clickable links in the terminal
313
314## Maintainers
315
316- [Sindre Sorhus](https://github.com/sindresorhus)
317- [Josh Junon](https://github.com/qix-)