2022-01-21 08:28:41 +00:00
< h1 align = "center" >
< br >
< br >
2022-06-08 10:36:39 +00:00
< img width = "360" src = "https://cdn.rawgit.com/chalk/chalk/19935d6484811c5e468817f846b7b3d417d7bf4a/logo.svg" alt = "chalk" >
2022-01-21 08:28:41 +00:00
< br >
< br >
< br >
< / h1 >
> Terminal string styling done right
2022-06-08 10:36:39 +00:00
[![Build Status ](https://travis-ci.org/chalk/chalk.svg?branch=master )](https://travis-ci.org/chalk/chalk)
[![Coverage Status ](https://coveralls.io/repos/chalk/chalk/badge.svg?branch=master )](https://coveralls.io/r/chalk/chalk?branch=master)
[![ ](http://img.shields.io/badge/unicorn-approved-ff69b4.svg )](https://www.youtube.com/watch?v=9auOCbH5Ns4)
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
[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.
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
**Chalk is a clean and focused alternative.**
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
![](https://github.com/chalk/ansi-styles/raw/master/screenshot.png)
## Why
2022-01-21 08:28:41 +00:00
2022-05-29 09:24:36 +00:00
- Highly performant
- Doesn't extend `String.prototype`
2022-06-08 10:36:39 +00:00
- Expressive API
- Ability to nest styles
2022-05-29 09:24:36 +00:00
- Clean and focused
2022-06-08 10:36:39 +00:00
- Auto-detects color support
2022-01-21 08:28:41 +00:00
- Actively maintained
2022-06-08 10:36:39 +00:00
- [Used by ~4500 modules ](https://www.npmjs.com/browse/depended/chalk ) as of July 15, 2015
2022-01-21 08:28:41 +00:00
## Install
```
2022-06-08 10:36:39 +00:00
$ npm install --save chalk
```
2022-01-21 08:28:41 +00:00
## Usage
Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
```js
2022-06-08 10:36:39 +00:00
var chalk = require('chalk');
// style a string
chalk.blue('Hello world!');
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
// combine styled and normal strings
chalk.blue('Hello') + 'World' + chalk.red('!');
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
// compose multiple styles using the chainable API
chalk.blue.bgRed.bold('Hello world!');
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
// pass in multiple arguments
chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
// nest styles
chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
// nest styles of the same type even (color, underline, background)
chalk.green(
2022-01-21 08:28:41 +00:00
'I am a green line ' +
chalk.blue.underline.bold('with a blue substring') +
' that becomes green again!'
2022-06-08 10:36:39 +00:00
);
2022-01-21 08:28:41 +00:00
```
2022-06-08 10:36:39 +00:00
Easily define your own themes.
2022-01-21 08:28:41 +00:00
```js
2022-06-08 10:36:39 +00:00
var chalk = require('chalk');
var error = chalk.bold.red;
2022-01-21 08:28:41 +00:00
console.log(error('Error!'));
```
2022-06-08 10:36:39 +00:00
Take advantage of console.log [string substitution ](http://nodejs.org/docs/latest/api/console.html#console_console_log_data ).
2022-01-21 08:28:41 +00:00
```js
2022-06-08 10:36:39 +00:00
var name = 'Sindre';
2022-01-21 08:28:41 +00:00
console.log(chalk.green('Hello %s'), name);
2022-06-08 10:36:39 +00:00
//=> Hello Sindre
2022-01-21 08:28:41 +00:00
```
## API
### chalk.`<style>[.<style>...](string, [string...])`
Example: `chalk.red.bold.underline('Hello', 'world');`
2022-06-08 10:36:39 +00:00
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` .
2022-01-21 08:28:41 +00:00
Multiple arguments will be separated by space.
### chalk.enabled
2022-06-08 10:36:39 +00:00
Color 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.
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
If you need to change this in a reusable module create a new instance:
2022-01-21 08:28:41 +00:00
```js
2022-06-08 10:36:39 +00:00
var ctx = new chalk.constructor({enabled: false});
2022-01-21 08:28:41 +00:00
```
2022-06-08 10:36:39 +00:00
### chalk.supportsColor
Detect whether the terminal [supports color ](https://github.com/chalk/supports-color ). Used internally and handled for you, but exposed for convenience.
Can 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` .
### chalk.styles
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
Exposes the styles as [ANSI escape codes ](https://github.com/chalk/ansi-styles ).
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with your own.
2022-01-21 08:28:41 +00:00
```js
2022-06-08 10:36:39 +00:00
var chalk = require('chalk');
console.log(chalk.styles.red);
//=> {open: '\u001b[31m', close: '\u001b[39m'}
console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
2022-01-21 08:28:41 +00:00
```
2022-06-08 10:36:39 +00:00
### chalk.hasColor(string)
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
Check whether a string [has color ](https://github.com/chalk/has-ansi ).
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
### chalk.stripColor(string)
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
[Strip color ](https://github.com/chalk/strip-ansi ) from a string.
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
Can be useful in combination with `.supportsColor` to strip color on externally styled text when it's not supported.
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
Example:
```js
var chalk = require('chalk');
var styledString = getText();
if (!chalk.supportsColor) {
styledString = chalk.stripColor(styledString);
}
```
2022-01-21 08:28:41 +00:00
## Styles
### Modifiers
- `reset`
- `bold`
- `dim`
2022-06-08 10:36:39 +00:00
- `italic` *(not widely supported)*
2022-01-21 08:28:41 +00:00
- `underline`
- `inverse`
- `hidden`
2022-06-08 10:36:39 +00:00
- `strikethrough` *(not widely supported)*
2022-01-21 08:28:41 +00:00
### Colors
- `black`
- `red`
- `green`
- `yellow`
2022-06-08 10:36:39 +00:00
- `blue` *(on Windows the bright version is used as normal blue is illegible)*
2022-01-21 08:28:41 +00:00
- `magenta`
- `cyan`
- `white`
2022-06-08 10:36:39 +00:00
- `gray`
2022-01-21 08:28:41 +00:00
### Background colors
- `bgBlack`
- `bgRed`
- `bgGreen`
- `bgYellow`
- `bgBlue`
- `bgMagenta`
- `bgCyan`
- `bgWhite`
2022-06-08 10:36:39 +00:00
## 256-colors
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
Chalk 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.
2022-01-21 08:28:41 +00:00
## Windows
2022-06-08 10:36:39 +00:00
If you're on Windows, do yourself a favor and use [`cmder` ](http://bliker.github.io/cmder/ ) instead of `cmd.exe` .
2022-01-21 08:28:41 +00:00
## Related
- [chalk-cli ](https://github.com/chalk/chalk-cli ) - CLI for this module
2022-06-08 10:36:39 +00:00
- [ansi-styles ](https://github.com/chalk/ansi-styles/ ) - ANSI escape codes for styling strings in the terminal
- [supports-color ](https://github.com/chalk/supports-color/ ) - Detect whether a terminal supports color
2022-01-21 08:28:41 +00:00
- [strip-ansi ](https://github.com/chalk/strip-ansi ) - Strip ANSI escape codes
- [has-ansi ](https://github.com/chalk/has-ansi ) - Check if a string has ANSI escape codes
- [ansi-regex ](https://github.com/chalk/ansi-regex ) - Regular expression for matching ANSI escape codes
- [wrap-ansi ](https://github.com/chalk/wrap-ansi ) - Wordwrap a string with ANSI escape codes
## License
2022-06-08 10:36:39 +00:00
MIT © [Sindre Sorhus ](http://sindresorhus.com )