chalk @ 1.0.0 - readme.md
1
<h1 align="center">
2
<br>
3
<img width="360" src="https://cdn.rawgit.com/sindresorhus/chalk/19935d6484811c5e468817f846b7b3d417d7bf4a/logo.svg" alt="chalk">
4
<br>
5
<br>
6
</h1>
7
8
> Terminal string styling done right
9
10
[![Build Status](https://travis-ci.org/sindresorhus/chalk.svg?branch=master)](https://travis-ci.org/sindresorhus/chalk) [![](http://img.shields.io/badge/unicorn-approved-ff69b4.svg?style=flat)](https://www.youtube.com/watch?v=Sm368W0OsHo)
11
12
[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.
13
14
**Chalk is a clean and focused alternative.**
15
16
![screenshot](https://github.com/sindresorhus/ansi-styles/raw/master/screenshot.png)
17
18
19
## Why
20
21
- Highly performant
22
- Doesn't extend `String.prototype`
23
- Expressive API
24
- Ability to nest styles
25
- Clean and focused
26
- Auto-detects color support
27
- Actively maintained
28
- [Used by ~3000 modules](https://www.npmjs.com/browse/depended/chalk)
29
30
31
## Install
32
33
```
34
$ npm install --save chalk
35
```
36
37
38
## Usage
39
40
Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
41
42
```js
43
var chalk = require('chalk');
44
45
// style a string
46
chalk.blue('Hello world!');
47
48
// combine styled and normal strings
49
chalk.blue('Hello') + 'World' + chalk.red('!');
50
51
// compose multiple styles using the chainable API
52
chalk.blue.bgRed.bold('Hello world!');
53
54
// pass in multiple arguments
55
chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');
56
57
// nest styles
58
chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
59
60
// nest styles of the same type even (color, underline, background)
61
chalk.green(
62
'I am a green line ' +
63
chalk.blue.underline.bold('with a blue substring') +
64
' that becomes green again!'
65
);
66
```
67
68
Easily define your own themes.
69
70
```js
71
var chalk = require('chalk');
72
var error = chalk.bold.red;
73
console.log(error('Error!'));
74
```
75
76
Take advantage of console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data).
77
78
```js
79
var name = 'Sindre';
80
console.log(chalk.green('Hello %s'), name);
81
//=> Hello Sindre
82
```
83
84
85
## API
86
87
### chalk.`<style>[.<style>...](string, [string...])`
88
89
Example: `chalk.red.bold.underline('Hello', 'world');`
90
91
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`.
92
93
Multiple arguments will be separated by space.
94
95
### chalk.enabled
96
97
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.
98
99
If you need to change this in a reusable module create a new instance:
100
101
```js
102
var ctx = new chalk.constructor({enabled: false});
103
```
104
105
### chalk.supportsColor
106
107
Detect whether the terminal [supports color](https://github.com/sindresorhus/supports-color). Used internally and handled for you, but exposed for convenience.
108
109
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`.
110
111
### chalk.styles
112
113
Exposes the styles as [ANSI escape codes](https://github.com/sindresorhus/ansi-styles).
114
115
Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with your own.
116
117
```js
118
var chalk = require('chalk');
119
120
console.log(chalk.styles.red);
121
//=> {open: '\u001b[31m', close: '\u001b[39m'}
122
123
console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
124
```
125
126
### chalk.hasColor(string)
127
128
Check whether a string [has color](https://github.com/sindresorhus/has-ansi).
129
130
### chalk.stripColor(string)
131
132
[Strip color](https://github.com/sindresorhus/strip-ansi) from a string.
133
134
Can be useful in combination with `.supportsColor` to strip color on externally styled text when it's not supported.
135
136
Example:
137
138
```js
139
var chalk = require('chalk');
140
var styledString = getText();
141
142
if (!chalk.supportsColor) {
143
styledString = chalk.stripColor(styledString);
144
}
145
```
146
147
148
## Styles
149
150
### Modifiers
151
152
- `reset`
153
- `bold`
154
- `dim`
155
- `italic` *(not widely supported)*
156
- `underline`
157
- `inverse`
158
- `hidden`
159
- `strikethrough` *(not widely supported)*
160
161
### Colors
162
163
- `black`
164
- `red`
165
- `green`
166
- `yellow`
167
- `blue` *(on Windows the bright version is used as normal blue is illegible)*
168
- `magenta`
169
- `cyan`
170
- `white`
171
- `gray`
172
173
### Background colors
174
175
- `bgBlack`
176
- `bgRed`
177
- `bgGreen`
178
- `bgYellow`
179
- `bgBlue`
180
- `bgMagenta`
181
- `bgCyan`
182
- `bgWhite`
183
184
185
## 256-colors
186
187
Chalk does not support 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.
188
189
190
## Windows
191
192
If you're on Windows, do yourself a favor and use [`cmder`](http://bliker.github.io/cmder/) instead of `cmd.exe`.
193
194
195
## License
196
197
MIT © [Sindre Sorhus](http://sindresorhus.com)
198