88 |
89 |
90 |
91 |
92 | chalk
98 | 99 | 100 |Terminal string styling done right. Created because the `colors` module does some really horrible things.
106 | 107 | 108 |npm install chalk
109 |
110 |
111 |
112 |
113 | Last Published By | 129 |
130 |
131 |
134 | |
135 |
---|---|
Maintainers | 140 |
141 |
142 |
143 |
146 |
147 |
148 |
151 |
152 | |
153 |
Version | 157 |158 | 159 | 0.5.1 160 | 161 | 162 | last updated 163 | 4 months ago 164 | 165 | | 166 |
License | 170 |171 | MIT 172 | | 173 |
Keywords | 178 |color, colour, colors, terminal, console, cli, string, ansi, styles, tty, formatting, rgb, 256, shell, xterm, log, logging, command-line, text | 179 |
Repository | 184 |185 | 186 | git://github.com/sindresorhus/chalk 187 | (git) 188 | | 189 |
Homepage | 194 |195 | https://github.com/sindresorhus/chalk 196 | | 197 |
Bugs | 202 |203 | https://github.com/sindresorhus/chalk/issues 204 | | 205 |
Dependencies | 210 |211 | 212 | ansi-styles, 213 | escape-string-regexp, 214 | has-ansi, 215 | strip-ansi, 216 | supports-color 217 | | 218 |
Dependents (1000) | 222 |
223 | generator-evolve, cha-cli, generator-langular, generator-hexo-theme, generator-angular-require, build-friend, generator-italystrap, generator-fabricator, generator-pioneerapp, generator-mangular, generator-meanp-modules, assemble-yaml, generator-grunt-maven, generator-rwdmail, connect-resource-pipeline, generator-noyobo, generator-config, generator-gulp-webapp, divshot-cli, conventional, and 980 more 224 | |
225 |
Starred by (82) | 231 |
232 | shidhincr, tunnckocore, tengisb, mohsen, inderdeep, jclem, oakley349, jimnox, tcauduro, kmck, ali1k, humantriangle, rickbergfalk, zlatip, jits, bcoe, ivangaravito, brianchung808, toddtreece, jbnicolai and 233 | 62 more 234 | |
235 |
-
241 |
242 |
- Read Me 243 | 244 |
250 |252 | 254 |Terminal string styling done right
251 |
colors.js is currently the most popular string styling module, but it has serious deficiencies like extending String.prototype which causes all kinds of problems. Although there are other ones, they either do too much or not enough.
255 |Chalk is a clean and focused alternative.
256 |Why
258 |-
259 |
- Highly performant 260 |
- Doesn't extend String.prototype 261 |
- Expressive API 262 |
- Ability to nest styles 263 |
- Clean and focused 264 |
- Auto-detects color support 265 |
- Actively maintained 266 |
- Used by 1000+ modules 267 |
Install
269 |$ npm install --save chalk
270 |
271 | Usage
272 |Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
273 |var chalk = require('chalk');
274 |
275 | // style a string
276 | console.log( chalk.blue('Hello world!') );
277 |
278 | // combine styled and normal strings
279 | console.log( chalk.blue('Hello'), 'World' + chalk.red('!') );
280 |
281 | // compose multiple styles using the chainable API
282 | console.log( chalk.blue.bgRed.bold('Hello world!') );
283 |
284 | // pass in multiple arguments
285 | console.log( chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz') );
286 |
287 | // nest styles
288 | console.log( chalk.red('Hello', chalk.underline.bgBlue('world') + '!') );
289 |
290 | // nest styles of the same type even (color, underline, background)
291 | console.log( chalk.green('I am a green line ' + chalk.blue('with a blue substring') + ' that becomes green again!') );
292 |
293 | Easily define your own themes.
294 |var chalk = require('chalk');
295 | var error = chalk.bold.red;
296 | console.log(error('Error!'));
297 |
298 | Take advantage of console.log string substitution.
299 |var name = 'Sindre';
300 | console.log(chalk.green('Hello %s'), name);
301 | //=> Hello Sindre
302 |
303 | API
304 |chalk.<style>[.<style>...](string, [string...])
305 | Example: chalk.red.bold.underline('Hello', 'world');
Chain styles and call the last one as a method with a string argument. Order doesn't matter.
307 |Multiple arguments will be separated by space.
308 |chalk.enabled
309 |Color support is automatically detected, but you can override it.
310 |chalk.supportsColor
311 |Detect whether the terminal supports color.
312 |Can be overridden by the user with the flags --color
and --no-color
.
Used internally and handled for you, but exposed for convenience.
314 |chalk.styles
315 |Exposes the styles as ANSI escape codes.
316 |Generally not useful, but you might need just the .open
or .close
escape code if you're mixing externally styled strings with yours.
var chalk = require('chalk');
318 |
319 | console.log(chalk.styles.red);
320 | //=> {open: '\u001b[31m', close: '\u001b[39m'}
321 |
322 | console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
323 |
324 | chalk.hasColor(string)
325 |Check whether a string has color.
326 |chalk.stripColor(string)
327 |Strip color from a string.
328 |Can be useful in combination with .supportsColor
to strip color on externally styled text when it's not supported.
Example:
330 |var chalk = require('chalk');
331 | var styledString = getText();
332 |
333 | if (!chalk.supportsColor) {
334 | styledString = chalk.stripColor(styledString);
335 | }
336 |
337 | Styles
338 |General
339 |-
340 |
reset
341 | bold
342 | dim
343 | italic
(not widely supported)
344 | underline
345 | inverse
346 | hidden
347 | strikethrough
(not widely supported)
348 |
Text colors
350 |-
351 |
black
352 | red
353 | green
354 | yellow
355 | blue
356 | magenta
357 | cyan
358 | white
359 | gray
360 |
Background colors
362 |-
363 |
bgBlack
364 | bgRed
365 | bgGreen
366 | bgYellow
367 | bgBlue
368 | bgMagenta
369 | bgCyan
370 | bgWhite
371 |
License
373 |MIT © Sindre Sorhus
374 | 375 |