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)
|
14 | [![Downloads](https://badgen.net/npm/dt/chalk)](https://www.npmjs.com/package/chalk)
|
15 | [![run on repl.it](https://img.shields.io/badge/Run_on_Replit-f26207?logo=replit&logoColor=white)](https://repl.it/github/chalk/chalk)
|
16 |
|
17 | ![](media/screenshot.png)
|
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>
|
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://strapi.io/?ref=sindresorhus">
|
44 | <div>
|
45 | <img src="https://sindresorhus.com/assets/thanks/strapi-logo-white-bg.png" width="220" alt="Strapi">
|
46 | </div>
|
47 | <b>Strapi is the leading open-source headless CMS.</b>
|
48 | <div>
|
49 | <sup>It’s 100% JavaScript, fully customizable, and developer-first.</sup>
|
50 | </div>
|
51 | </a>
|
52 | <br>
|
53 | <br>
|
54 | <a href="https://www.stackaid.us/?utm_campaign=sindre">
|
55 | <div>
|
56 | <img src="https://sindresorhus.com/assets/thanks/stackaid-logo.png" width="230" alt="StackAid">
|
57 | </div>
|
58 | <b>Fund your open source dependencies</b>
|
59 | </a>
|
60 | <br>
|
61 | <br>
|
62 | </p>
|
63 | </div>
|
64 |
|
65 | ---
|
66 |
|
67 | <br>
|
68 |
|
69 | ## Highlights
|
70 |
|
71 | - Expressive API
|
72 | - Highly performant
|
73 | - No dependencies
|
74 | - Ability to nest styles
|
75 | - [256/Truecolor color support](#256-and-truecolor-color-support)
|
76 | - Auto-detects color support
|
77 | - Doesn't extend `String.prototype`
|
78 | - Clean and focused
|
79 | - Actively maintained
|
80 | - [Used by ~86,000 packages](https://www.npmjs.com/browse/depended/chalk) as of October 4, 2022
|
81 |
|
82 | ## Install
|
83 |
|
84 | ```sh
|
85 | npm install chalk
|
86 | ```
|
87 |
|
88 | **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)
|
89 |
|
90 | ## Usage
|
91 |
|
92 | ```js
|
93 | import chalk from 'chalk';
|
94 |
|
95 | console.log(chalk.blue('Hello world!'));
|
96 | ```
|
97 |
|
98 | Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
|
99 |
|
100 | ```js
|
101 | import chalk from 'chalk';
|
102 |
|
103 | const log = console.log;
|
104 |
|
105 | // Combine styled and normal strings
|
106 | log(chalk.blue('Hello') + ' World' + chalk.red('!'));
|
107 |
|
108 | // Compose multiple styles using the chainable API
|
109 | log(chalk.blue.bgRed.bold('Hello world!'));
|
110 |
|
111 | // Pass in multiple arguments
|
112 | log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
|
113 |
|
114 | // Nest styles
|
115 | log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
|
116 |
|
117 | // Nest styles of the same type even (color, underline, background)
|
118 | log(chalk.green(
|
119 | 'I am a green line ' +
|
120 | chalk.blue.underline.bold('with a blue substring') +
|
121 | ' that becomes green again!'
|
122 | ));
|
123 |
|
124 | // ES2015 template literal
|
125 | log(`
|
126 | CPU: ${chalk.red('90%')}
|
127 | RAM: ${chalk.green('40%')}
|
128 | DISK: ${chalk.yellow('70%')}
|
129 | `);
|
130 |
|
131 | // Use RGB colors in terminal emulators that support it.
|
132 | log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
|
133 | log(chalk.hex('#DEADED').bold('Bold gray!'));
|
134 | ```
|
135 |
|
136 | Easily define your own themes:
|
137 |
|
138 | ```js
|
139 | import chalk from 'chalk';
|
140 |
|
141 | const error = chalk.bold.red;
|
142 | const warning = chalk.hex('#FFA500'); // Orange color
|
143 |
|
144 | console.log(error('Error!'));
|
145 | console.log(warning('Warning!'));
|
146 | ```
|
147 |
|
148 | Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args):
|
149 |
|
150 | ```js
|
151 | import chalk from 'chalk';
|
152 |
|
153 | const name = 'Sindre';
|
154 | console.log(chalk.green('Hello %s'), name);
|
155 | //=> 'Hello Sindre'
|
156 | ```
|
157 |
|
158 | ## API
|
159 |
|
160 | ### chalk.`<style>[.<style>...](string, [string...])`
|
161 |
|
162 | Example: `chalk.red.bold.underline('Hello', 'world');`
|
163 |
|
164 | Chain [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`.
|
165 |
|
166 | Multiple arguments will be separated by space.
|
167 |
|
168 | ### chalk.level
|
169 |
|
170 | Specifies the level of color support.
|
171 |
|
172 | Color 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.
|
173 |
|
174 | If you need to change this in a reusable module, create a new instance:
|
175 |
|
176 | ```js
|
177 | import {Chalk} from 'chalk';
|
178 |
|
179 | const customChalk = new Chalk({level: 0});
|
180 | ```
|
181 |
|
182 | | Level | Description |
|
183 | | :---: | :--- |
|
184 | | `0` | All colors disabled |
|
185 | | `1` | Basic color support (16 colors) |
|
186 | | `2` | 256 color support |
|
187 | | `3` | Truecolor support (16 million colors) |
|
188 |
|
189 | ### supportsColor
|
190 |
|
191 | Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.
|
192 |
|
193 | Can 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.
|
194 |
|
195 | Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.
|
196 |
|
197 | ### chalkStderr and supportsColorStderr
|
198 |
|
199 | `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.
|
200 |
|
201 | ### modifierNames, foregroundColorNames, backgroundColorNames, and colorNames
|
202 |
|
203 | All supported style strings are exposed as an array of strings for convenience. `colorNames` is the combination of `foregroundColorNames` and `backgroundColorNames`.
|
204 |
|
205 | This can be useful if you wrap Chalk and need to validate input:
|
206 |
|
207 | ```js
|
208 | import {modifierNames, foregroundColorNames} from 'chalk';
|
209 |
|
210 | console.log(modifierNames.includes('bold'));
|
211 | //=> true
|
212 |
|
213 | console.log(foregroundColorNames.includes('pink'));
|
214 | //=> false
|
215 | ```
|
216 |
|
217 | ## Styles
|
218 |
|
219 | ### Modifiers
|
220 |
|
221 | - `reset` - Reset the current style.
|
222 | - `bold` - Make the text bold.
|
223 | - `dim` - Make the text have lower opacity.
|
224 | - `italic` - Make the text italic. *(Not widely supported)*
|
225 | - `underline` - Put a horizontal line below the text. *(Not widely supported)*
|
226 | - `overline` - Put a horizontal line above the text. *(Not widely supported)*
|
227 | - `inverse`- Invert background and foreground colors.
|
228 | - `hidden` - Print the text but make it invisible.
|
229 | - `strikethrough` - Puts a horizontal line through the center of the text. *(Not widely supported)*
|
230 | - `visible`- Print the text only when Chalk has a color level above zero. Can be useful for things that are purely cosmetic.
|
231 |
|
232 | ### Colors
|
233 |
|
234 | - `black`
|
235 | - `red`
|
236 | - `green`
|
237 | - `yellow`
|
238 | - `blue`
|
239 | - `magenta`
|
240 | - `cyan`
|
241 | - `white`
|
242 | - `blackBright` (alias: `gray`, `grey`)
|
243 | - `redBright`
|
244 | - `greenBright`
|
245 | - `yellowBright`
|
246 | - `blueBright`
|
247 | - `magentaBright`
|
248 | - `cyanBright`
|
249 | - `whiteBright`
|
250 |
|
251 | ### Background colors
|
252 |
|
253 | - `bgBlack`
|
254 | - `bgRed`
|
255 | - `bgGreen`
|
256 | - `bgYellow`
|
257 | - `bgBlue`
|
258 | - `bgMagenta`
|
259 | - `bgCyan`
|
260 | - `bgWhite`
|
261 | - `bgBlackBright` (alias: `bgGray`, `bgGrey`)
|
262 | - `bgRedBright`
|
263 | - `bgGreenBright`
|
264 | - `bgYellowBright`
|
265 | - `bgBlueBright`
|
266 | - `bgMagentaBright`
|
267 | - `bgCyanBright`
|
268 | - `bgWhiteBright`
|
269 |
|
270 | ## 256 and Truecolor color support
|
271 |
|
272 | Chalk supports 256 colors and [Truecolor](https://github.com/termstandard/colors) (16 million colors) on supported terminal apps.
|
273 |
|
274 | Colors 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).
|
275 |
|
276 | Examples:
|
277 |
|
278 | - `chalk.hex('#DEADED').underline('Hello, world!')`
|
279 | - `chalk.rgb(15, 100, 204).inverse('Hello!')`
|
280 |
|
281 | Background 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).
|
282 |
|
283 | - `chalk.bgHex('#DEADED').underline('Hello, world!')`
|
284 | - `chalk.bgRgb(15, 100, 204).inverse('Hello!')`
|
285 |
|
286 | The following color models can be used:
|
287 |
|
288 | - [`rgb`](https://en.wikipedia.org/wiki/RGB_color_model) - Example: `chalk.rgb(255, 136, 0).bold('Orange!')`
|
289 | - [`hex`](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet) - Example: `chalk.hex('#FF8800').bold('Orange!')`
|
290 | - [`ansi256`](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) - Example: `chalk.bgAnsi256(194)('Honeydew, more or less')`
|
291 |
|
292 | ## Browser support
|
293 |
|
294 | Since Chrome 69, ANSI escape codes are natively supported in the developer console.
|
295 |
|
296 | ## Windows
|
297 |
|
298 | If you're on Windows, do yourself a favor and use [Windows Terminal](https://github.com/microsoft/terminal) instead of `cmd.exe`.
|
299 |
|
300 | ## Origin story
|
301 |
|
302 | [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.
|
303 |
|
304 | ## Related
|
305 |
|
306 | - [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
|
307 | - [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
|
308 | - [ansi-styles](https://github.com/chalk/ansi-styles) - ANSI escape codes for styling strings in the terminal
|
309 | - [supports-color](https://github.com/chalk/supports-color) - Detect whether a terminal supports color
|
310 | - [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes
|
311 | - [strip-ansi-stream](https://github.com/chalk/strip-ansi-stream) - Strip ANSI escape codes from a stream
|
312 | - [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
|
313 | - [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
|
314 | - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
|
315 | - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
|
316 | - [color-convert](https://github.com/qix-/color-convert) - Converts colors between different models
|
317 | - [chalk-animation](https://github.com/bokub/chalk-animation) - Animate strings in the terminal
|
318 | - [gradient-string](https://github.com/bokub/gradient-string) - Apply color gradients to strings
|
319 | - [chalk-pipe](https://github.com/LitoMore/chalk-pipe) - Create chalk style schemes with simpler style strings
|
320 | - [terminal-link](https://github.com/sindresorhus/terminal-link) - Create clickable links in the terminal
|
321 |
|
322 | ## Maintainers
|
323 |
|
324 | - [Sindre Sorhus](https://github.com/sindresorhus)
|
325 | - [Josh Junon](https://github.com/qix-)
|