2014-07-04 23:23:45 +02:00
|
|
|
# <img width="300" src="https://cdn.rawgit.com/sindresorhus/chalk/77ae94f63ab1ac61389b190e5a59866569d1a376/logo.svg" alt="chalk">
|
2013-08-03 02:16:26 +02:00
|
|
|
|
2013-12-13 20:21:51 +01:00
|
|
|
> Terminal string styling done right
|
2013-08-03 02:16:26 +02:00
|
|
|
|
2014-04-03 22:57:06 +02:00
|
|
|
[](https://travis-ci.org/sindresorhus/chalk)
|
2014-06-26 00:18:23 +02:00
|
|
|

|
2013-11-10 13:25:35 +01:00
|
|
|
|
2014-08-30 23:06:34 +02:00
|
|
|
[colors.js](https://github.com/Marak/colors.js) used to be the most popular
|
2014-08-30 22:53:00 +02:00
|
|
|
string styling module, but it has serious deficiencies like extending
|
2014-08-30 23:08:28 +02:00
|
|
|
`String.prototype` which causes all kinds of
|
2014-08-30 22:53:00 +02:00
|
|
|
[problems](https://github.com/yeoman/yo/issues/68). Although there are other
|
|
|
|
|
ones, they either do too much or not enough.
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
**Chalk is a clean and focused alternative.**
|
|
|
|
|
|
2014-06-04 01:38:03 +02:00
|
|
|

|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
## Why
|
|
|
|
|
|
2014-07-04 21:29:01 +02:00
|
|
|
- Highly performant
|
2014-08-30 23:08:28 +02:00
|
|
|
- Doesn't extend `String.prototype`
|
2013-08-03 02:16:26 +02:00
|
|
|
- Expressive API
|
2014-06-24 17:23:43 +02:00
|
|
|
- Ability to nest styles
|
2013-08-03 18:47:47 +02:00
|
|
|
- Clean and focused
|
2013-08-03 02:16:26 +02:00
|
|
|
- Auto-detects color support
|
|
|
|
|
- Actively maintained
|
2014-08-30 23:05:34 +02:00
|
|
|
- [Used by 1700+ modules](https://npmjs.org/browse/depended/chalk)
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
## Install
|
|
|
|
|
|
2014-06-04 01:43:07 +02:00
|
|
|
```sh
|
2014-08-30 23:10:03 +02:00
|
|
|
npm install --save chalk
|
2014-02-11 20:33:11 +01:00
|
|
|
```
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
|
2014-04-03 22:57:06 +02:00
|
|
|
## Usage
|
2013-08-03 02:16:26 +02:00
|
|
|
|
2014-08-30 22:53:00 +02:00
|
|
|
Chalk comes with an easy to use composable API where you just chain and nest
|
|
|
|
|
the styles you want.
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var chalk = require('chalk');
|
|
|
|
|
|
|
|
|
|
// style a string
|
2013-12-13 20:21:51 +01:00
|
|
|
console.log( chalk.blue('Hello world!') );
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
// combine styled and normal strings
|
2013-12-13 20:21:51 +01:00
|
|
|
console.log( chalk.blue('Hello'), 'World' + chalk.red('!') );
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
// compose multiple styles using the chainable API
|
2013-12-13 20:21:51 +01:00
|
|
|
console.log( chalk.blue.bgRed.bold('Hello world!') );
|
2013-08-03 18:47:47 +02:00
|
|
|
|
2014-06-24 17:23:43 +02:00
|
|
|
// pass in multiple arguments
|
|
|
|
|
console.log( chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz') );
|
|
|
|
|
|
2013-08-03 18:47:47 +02:00
|
|
|
// nest styles
|
2013-12-13 20:21:51 +01:00
|
|
|
console.log( chalk.red('Hello', chalk.underline.bgBlue('world') + '!') );
|
2013-10-07 00:34:32 -04:00
|
|
|
|
2014-06-24 17:23:43 +02:00
|
|
|
// nest styles of the same type even (color, underline, background)
|
|
|
|
|
console.log( chalk.green('I am a green line ' + chalk.blue('with a blue substring') + ' that becomes green again!') );
|
2013-08-03 02:16:26 +02:00
|
|
|
```
|
|
|
|
|
|
2013-12-16 21:44:25 +01:00
|
|
|
Easily define your own themes.
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var chalk = require('chalk');
|
|
|
|
|
var error = chalk.bold.red;
|
|
|
|
|
console.log(error('Error!'));
|
|
|
|
|
```
|
|
|
|
|
|
2014-08-30 22:53:00 +02:00
|
|
|
Take advantage of console.log [string
|
|
|
|
|
substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data).
|
2013-12-16 21:44:25 +01:00
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var name = 'Sindre';
|
|
|
|
|
console.log(chalk.green('Hello %s'), name);
|
|
|
|
|
//=> Hello Sindre
|
|
|
|
|
```
|
|
|
|
|
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
|
2013-10-19 18:04:30 +02:00
|
|
|
### chalk.`<style>[.<style>...](string, [string...])`
|
2013-08-03 02:16:26 +02:00
|
|
|
|
2013-12-13 20:21:51 +01:00
|
|
|
Example: `chalk.red.bold.underline('Hello', 'world');`
|
2013-08-03 02:16:26 +02:00
|
|
|
|
2014-08-30 22:53:00 +02: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`.
|
2013-12-13 20:21:51 +01:00
|
|
|
|
|
|
|
|
Multiple arguments will be separated by space.
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
### chalk.enabled
|
|
|
|
|
|
|
|
|
|
Color support is automatically detected, but you can override it.
|
|
|
|
|
|
|
|
|
|
### chalk.supportsColor
|
|
|
|
|
|
2014-08-30 22:53:00 +02:00
|
|
|
Detect whether the terminal [supports
|
|
|
|
|
color](https://github.com/sindresorhus/supports-color).
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
Can be overridden by the user with the flags `--color` and `--no-color`.
|
|
|
|
|
|
|
|
|
|
Used internally and handled for you, but exposed for convenience.
|
|
|
|
|
|
|
|
|
|
### chalk.styles
|
|
|
|
|
|
2014-08-30 22:53:00 +02:00
|
|
|
Exposes the styles as [ANSI escape
|
|
|
|
|
codes](https://github.com/sindresorhus/ansi-styles).
|
2013-08-03 02:16:26 +02:00
|
|
|
|
2014-08-30 22:53:00 +02:00
|
|
|
Generally not useful, but you might need just the `.open` or `.close` escape
|
|
|
|
|
code if you're mixing externally styled strings with yours.
|
2013-12-13 20:21:51 +01:00
|
|
|
|
2013-08-03 02:16:26 +02:00
|
|
|
```js
|
|
|
|
|
var chalk = require('chalk');
|
2013-08-03 18:47:47 +02:00
|
|
|
|
2013-08-03 02:16:26 +02:00
|
|
|
console.log(chalk.styles.red);
|
2014-06-04 01:43:07 +02:00
|
|
|
//=> {open: '\u001b[31m', close: '\u001b[39m'}
|
2013-08-03 18:47:47 +02:00
|
|
|
|
2013-12-08 01:02:35 +01:00
|
|
|
console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
|
2013-08-03 02:16:26 +02:00
|
|
|
```
|
|
|
|
|
|
2014-06-24 21:34:11 +02:00
|
|
|
### chalk.hasColor(string)
|
|
|
|
|
|
|
|
|
|
Check whether a string [has color](https://github.com/sindresorhus/has-ansi).
|
|
|
|
|
|
2013-08-03 03:38:42 +02:00
|
|
|
### chalk.stripColor(string)
|
2013-08-03 02:16:26 +02:00
|
|
|
|
2013-12-08 01:02:35 +01:00
|
|
|
[Strip color](https://github.com/sindresorhus/strip-ansi) from a string.
|
2013-08-03 02:16:26 +02:00
|
|
|
|
2014-08-30 22:53:00 +02:00
|
|
|
Can be useful in combination with `.supportsColor` to strip color on externally
|
|
|
|
|
styled text when it's not supported.
|
2013-12-13 20:21:51 +01:00
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var chalk = require('chalk');
|
2014-02-11 20:33:11 +01:00
|
|
|
var styledString = getText();
|
2013-12-13 20:21:51 +01:00
|
|
|
|
|
|
|
|
if (!chalk.supportsColor) {
|
2014-02-11 20:33:11 +01:00
|
|
|
styledString = chalk.stripColor(styledString);
|
2013-12-13 20:21:51 +01:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
## Styles
|
|
|
|
|
|
|
|
|
|
### General
|
|
|
|
|
|
2014-06-04 01:38:03 +02:00
|
|
|
- `reset`
|
|
|
|
|
- `bold`
|
|
|
|
|
- `dim`
|
|
|
|
|
- `italic` *(not widely supported)*
|
|
|
|
|
- `underline`
|
|
|
|
|
- `inverse`
|
|
|
|
|
- `hidden`
|
|
|
|
|
- `strikethrough` *(not widely supported)*
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
### Text colors
|
|
|
|
|
|
2014-06-04 01:38:03 +02:00
|
|
|
- `black`
|
|
|
|
|
- `red`
|
|
|
|
|
- `green`
|
|
|
|
|
- `yellow`
|
|
|
|
|
- `blue`
|
|
|
|
|
- `magenta`
|
|
|
|
|
- `cyan`
|
|
|
|
|
- `white`
|
|
|
|
|
- `gray`
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
### Background colors
|
|
|
|
|
|
2014-06-04 01:38:03 +02:00
|
|
|
- `bgBlack`
|
|
|
|
|
- `bgRed`
|
|
|
|
|
- `bgGreen`
|
|
|
|
|
- `bgYellow`
|
|
|
|
|
- `bgBlue`
|
|
|
|
|
- `bgMagenta`
|
|
|
|
|
- `bgCyan`
|
|
|
|
|
- `bgWhite`
|
2013-08-03 02:16:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
2014-06-04 01:43:07 +02:00
|
|
|
MIT © [Sindre Sorhus](http://sindresorhus.com)
|