chalk 1.1.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +165 -44
- package/license +4 -16
- package/package.json +61 -66
- package/readme.md +157 -56
- package/templates.js +128 -0
package/index.js
CHANGED
|
@@ -1,99 +1,220 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
const escapeStringRegexp = require('escape-string-regexp');
|
|
3
|
+
const ansiStyles = require('ansi-styles');
|
|
4
|
+
const supportsColor = require('supports-color');
|
|
5
|
+
|
|
6
|
+
const template = require('./templates.js');
|
|
7
|
+
|
|
8
|
+
const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm');
|
|
9
|
+
|
|
10
|
+
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
|
11
|
+
const levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m'];
|
|
12
|
+
|
|
13
|
+
// `color-convert` models to exclude from the Chalk API due to conflicts and such
|
|
14
|
+
const skipModels = new Set(['gray']);
|
|
15
|
+
|
|
16
|
+
const styles = Object.create(null);
|
|
17
|
+
|
|
18
|
+
function applyOptions(obj, options) {
|
|
19
|
+
options = options || {};
|
|
20
|
+
|
|
21
|
+
// Detect level if not set manually
|
|
22
|
+
const scLevel = supportsColor ? supportsColor.level : 0;
|
|
23
|
+
obj.level = options.level === undefined ? scLevel : options.level;
|
|
24
|
+
obj.enabled = 'enabled' in options ? options.enabled : obj.level > 0;
|
|
25
|
+
}
|
|
7
26
|
|
|
8
27
|
function Chalk(options) {
|
|
9
|
-
//
|
|
10
|
-
|
|
28
|
+
// We check for this.template here since calling `chalk.constructor()`
|
|
29
|
+
// by itself will have a `this` of a previously constructed chalk object
|
|
30
|
+
if (!this || !(this instanceof Chalk) || this.template) {
|
|
31
|
+
const chalk = {};
|
|
32
|
+
applyOptions(chalk, options);
|
|
33
|
+
|
|
34
|
+
chalk.template = function () {
|
|
35
|
+
const args = [].slice.call(arguments);
|
|
36
|
+
return chalkTag.apply(null, [chalk.template].concat(args));
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
Object.setPrototypeOf(chalk, Chalk.prototype);
|
|
40
|
+
Object.setPrototypeOf(chalk.template, chalk);
|
|
41
|
+
|
|
42
|
+
chalk.template.constructor = Chalk;
|
|
43
|
+
|
|
44
|
+
return chalk.template;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
applyOptions(this, options);
|
|
11
48
|
}
|
|
12
49
|
|
|
13
|
-
//
|
|
50
|
+
// Use bright blue on Windows as the normal blue color is illegible
|
|
14
51
|
if (isSimpleWindowsTerm) {
|
|
15
|
-
ansiStyles.blue.open = '\
|
|
52
|
+
ansiStyles.blue.open = '\u001B[94m';
|
|
16
53
|
}
|
|
17
54
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
Object.keys(ansiStyles).forEach(function (key) {
|
|
55
|
+
for (const key of Object.keys(ansiStyles)) {
|
|
21
56
|
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
|
22
57
|
|
|
23
58
|
styles[key] = {
|
|
24
|
-
get
|
|
25
|
-
|
|
59
|
+
get() {
|
|
60
|
+
const codes = ansiStyles[key];
|
|
61
|
+
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], key);
|
|
26
62
|
}
|
|
27
63
|
};
|
|
28
|
-
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
|
|
67
|
+
for (const model of Object.keys(ansiStyles.color.ansi)) {
|
|
68
|
+
if (skipModels.has(model)) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
29
71
|
|
|
30
|
-
|
|
72
|
+
styles[model] = {
|
|
73
|
+
get() {
|
|
74
|
+
const level = this.level;
|
|
75
|
+
return function () {
|
|
76
|
+
const open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments);
|
|
77
|
+
const codes = {
|
|
78
|
+
open,
|
|
79
|
+
close: ansiStyles.color.close,
|
|
80
|
+
closeRe: ansiStyles.color.closeRe
|
|
81
|
+
};
|
|
82
|
+
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g');
|
|
89
|
+
for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
|
|
90
|
+
if (skipModels.has(model)) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
31
93
|
|
|
32
|
-
|
|
33
|
-
|
|
94
|
+
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
|
|
95
|
+
styles[bgModel] = {
|
|
96
|
+
get() {
|
|
97
|
+
const level = this.level;
|
|
98
|
+
return function () {
|
|
99
|
+
const open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments);
|
|
100
|
+
const codes = {
|
|
101
|
+
open,
|
|
102
|
+
close: ansiStyles.bgColor.close,
|
|
103
|
+
closeRe: ansiStyles.bgColor.closeRe
|
|
104
|
+
};
|
|
105
|
+
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const proto = Object.defineProperties(() => {}, styles);
|
|
112
|
+
|
|
113
|
+
function build(_styles, key) {
|
|
114
|
+
const builder = function () {
|
|
34
115
|
return applyStyle.apply(builder, arguments);
|
|
35
116
|
};
|
|
36
117
|
|
|
37
118
|
builder._styles = _styles;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
119
|
+
|
|
120
|
+
const self = this;
|
|
121
|
+
|
|
122
|
+
Object.defineProperty(builder, 'level', {
|
|
123
|
+
enumerable: true,
|
|
124
|
+
get() {
|
|
125
|
+
return self.level;
|
|
126
|
+
},
|
|
127
|
+
set(level) {
|
|
128
|
+
self.level = level;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
Object.defineProperty(builder, 'enabled', {
|
|
133
|
+
enumerable: true,
|
|
134
|
+
get() {
|
|
135
|
+
return self.enabled;
|
|
136
|
+
},
|
|
137
|
+
set(enabled) {
|
|
138
|
+
self.enabled = enabled;
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// See below for fix regarding invisible grey/dim combination on Windows
|
|
143
|
+
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
|
|
144
|
+
|
|
145
|
+
// `__proto__` is used because we must return a function, but there is
|
|
146
|
+
// no way to create a function with a different prototype
|
|
147
|
+
builder.__proto__ = proto; // eslint-disable-line no-proto
|
|
43
148
|
|
|
44
149
|
return builder;
|
|
45
150
|
}
|
|
46
151
|
|
|
47
152
|
function applyStyle() {
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
153
|
+
// Support varags, but simply cast to string in case there's only one arg
|
|
154
|
+
const args = arguments;
|
|
155
|
+
const argsLen = args.length;
|
|
156
|
+
let str = String(arguments[0]);
|
|
157
|
+
|
|
158
|
+
if (argsLen === 0) {
|
|
159
|
+
return '';
|
|
160
|
+
}
|
|
52
161
|
|
|
53
162
|
if (argsLen > 1) {
|
|
54
|
-
//
|
|
55
|
-
for (
|
|
163
|
+
// Don't slice `arguments`, it prevents V8 optimizations
|
|
164
|
+
for (let a = 1; a < argsLen; a++) {
|
|
56
165
|
str += ' ' + args[a];
|
|
57
166
|
}
|
|
58
167
|
}
|
|
59
168
|
|
|
60
|
-
if (!this.enabled || !str) {
|
|
169
|
+
if (!this.enabled || this.level <= 0 || !str) {
|
|
61
170
|
return str;
|
|
62
171
|
}
|
|
63
172
|
|
|
64
|
-
var nestedStyles = this._styles;
|
|
65
|
-
var i = nestedStyles.length;
|
|
66
|
-
|
|
67
173
|
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
|
|
68
174
|
// see https://github.com/chalk/chalk/issues/58
|
|
69
175
|
// If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
|
|
70
|
-
|
|
71
|
-
if (isSimpleWindowsTerm &&
|
|
176
|
+
const originalDim = ansiStyles.dim.open;
|
|
177
|
+
if (isSimpleWindowsTerm && this.hasGrey) {
|
|
72
178
|
ansiStyles.dim.open = '';
|
|
73
179
|
}
|
|
74
180
|
|
|
75
|
-
|
|
76
|
-
var code = ansiStyles[nestedStyles[i]];
|
|
77
|
-
|
|
181
|
+
for (const code of this._styles.slice().reverse()) {
|
|
78
182
|
// Replace any instances already present with a re-opening code
|
|
79
183
|
// otherwise only the part of the string until said closing code
|
|
80
184
|
// will be colored, and the rest will simply be 'plain'.
|
|
81
185
|
str = code.open + str.replace(code.closeRe, code.open) + code.close;
|
|
82
186
|
|
|
83
187
|
// Close the styling before a linebreak and reopen
|
|
84
|
-
// after next line to fix a bleed issue on
|
|
188
|
+
// after next line to fix a bleed issue on macOS
|
|
85
189
|
// https://github.com/chalk/chalk/pull/92
|
|
86
|
-
str = str.replace(/\r?\n/g, code.close
|
|
190
|
+
str = str.replace(/\r?\n/g, `${code.close}$&${code.open}`);
|
|
87
191
|
}
|
|
88
192
|
|
|
89
|
-
// Reset the original
|
|
193
|
+
// Reset the original `dim` if we changed it to work around the Windows dimmed gray issue
|
|
90
194
|
ansiStyles.dim.open = originalDim;
|
|
91
195
|
|
|
92
196
|
return str;
|
|
93
197
|
}
|
|
94
198
|
|
|
95
|
-
|
|
199
|
+
function chalkTag(chalk, strings) {
|
|
200
|
+
if (!Array.isArray(strings)) {
|
|
201
|
+
// If chalk() was called by itself or with a string,
|
|
202
|
+
// return the string itself as a string.
|
|
203
|
+
return [].slice.call(arguments, 1).join(' ');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const args = [].slice.call(arguments, 2);
|
|
207
|
+
const parts = [strings.raw[0]];
|
|
208
|
+
|
|
209
|
+
for (let i = 1; i < strings.length; i++) {
|
|
210
|
+
parts.push(String(args[i - 1]).replace(/[{}\\]/g, '\\$&'));
|
|
211
|
+
parts.push(String(strings.raw[i]));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return template(chalk, parts.join(''));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
Object.defineProperties(Chalk.prototype, styles);
|
|
96
218
|
|
|
97
|
-
module.exports =
|
|
98
|
-
module.exports.styles = ansiStyles;
|
|
219
|
+
module.exports = Chalk(); // eslint-disable-line new-cap
|
|
99
220
|
module.exports.supportsColor = supportsColor;
|
package/license
CHANGED
|
@@ -1,21 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
3
|
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
|
4
4
|
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
11
6
|
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
14
8
|
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,68 +1,63 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
"envs": [
|
|
64
|
-
"node",
|
|
65
|
-
"mocha"
|
|
66
|
-
]
|
|
67
|
-
}
|
|
2
|
+
"name": "chalk",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Terminal string styling done right",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "chalk/chalk",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=4"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "xo && nyc ava",
|
|
12
|
+
"bench": "matcha benchmark.js",
|
|
13
|
+
"coveralls": "nyc report --reporter=text-lcov | coveralls"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"templates.js"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"color",
|
|
21
|
+
"colour",
|
|
22
|
+
"colors",
|
|
23
|
+
"terminal",
|
|
24
|
+
"console",
|
|
25
|
+
"cli",
|
|
26
|
+
"string",
|
|
27
|
+
"str",
|
|
28
|
+
"ansi",
|
|
29
|
+
"style",
|
|
30
|
+
"styles",
|
|
31
|
+
"tty",
|
|
32
|
+
"formatting",
|
|
33
|
+
"rgb",
|
|
34
|
+
"256",
|
|
35
|
+
"shell",
|
|
36
|
+
"xterm",
|
|
37
|
+
"log",
|
|
38
|
+
"logging",
|
|
39
|
+
"command-line",
|
|
40
|
+
"text"
|
|
41
|
+
],
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"ansi-styles": "^3.1.0",
|
|
44
|
+
"escape-string-regexp": "^1.0.5",
|
|
45
|
+
"supports-color": "^4.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"ava": "*",
|
|
49
|
+
"coveralls": "^2.11.2",
|
|
50
|
+
"execa": "^0.7.0",
|
|
51
|
+
"import-fresh": "^2.0.0",
|
|
52
|
+
"matcha": "^0.7.0",
|
|
53
|
+
"nyc": "^11.0.2",
|
|
54
|
+
"resolve-from": "^3.0.0",
|
|
55
|
+
"xo": "*"
|
|
56
|
+
},
|
|
57
|
+
"xo": {
|
|
58
|
+
"envs": [
|
|
59
|
+
"node",
|
|
60
|
+
"mocha"
|
|
61
|
+
]
|
|
62
|
+
}
|
|
68
63
|
}
|
package/readme.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<h1 align="center">
|
|
2
2
|
<br>
|
|
3
3
|
<br>
|
|
4
|
-
<img width="
|
|
4
|
+
<img width="320" src="https://cdn.rawgit.com/chalk/chalk/19935d6484811c5e468817f846b7b3d417d7bf4a/logo.svg" alt="chalk">
|
|
5
5
|
<br>
|
|
6
6
|
<br>
|
|
7
7
|
<br>
|
|
@@ -9,34 +9,30 @@
|
|
|
9
9
|
|
|
10
10
|
> Terminal string styling done right
|
|
11
11
|
|
|
12
|
-
[](https://travis-ci.org/chalk/chalk)
|
|
13
|
-
[](https://coveralls.io/r/chalk/chalk?branch=master)
|
|
14
|
-
[](https://www.youtube.com/watch?v=9auOCbH5Ns4)
|
|
12
|
+
[](https://travis-ci.org/chalk/chalk) [](https://coveralls.io/github/chalk/chalk?branch=master) [](https://www.youtube.com/watch?v=9auOCbH5Ns4) [](https://github.com/sindresorhus/xo)
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
[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.
|
|
18
|
-
|
|
19
|
-
**Chalk is a clean and focused alternative.**
|
|
14
|
+
### [See what's new in Chalk 2](https://github.com/chalk/chalk/releases/tag/v2.0.0)
|
|
20
15
|
|
|
21
16
|

|
|
22
17
|
|
|
23
18
|
|
|
24
|
-
##
|
|
19
|
+
## Highlights
|
|
25
20
|
|
|
26
|
-
- Highly performant
|
|
27
|
-
- Doesn't extend `String.prototype`
|
|
28
21
|
- Expressive API
|
|
22
|
+
- Highly performant
|
|
29
23
|
- Ability to nest styles
|
|
30
|
-
-
|
|
24
|
+
- [256/Truecolor color support](#256-and-truecolor-color-support)
|
|
31
25
|
- Auto-detects color support
|
|
26
|
+
- Doesn't extend `String.prototype`
|
|
27
|
+
- Clean and focused
|
|
32
28
|
- Actively maintained
|
|
33
|
-
- [Used by ~
|
|
29
|
+
- [Used by ~17,000 packages](https://www.npmjs.com/browse/depended/chalk) as of June 20th, 2017
|
|
34
30
|
|
|
35
31
|
|
|
36
32
|
## Install
|
|
37
33
|
|
|
38
|
-
```
|
|
39
|
-
$ npm install
|
|
34
|
+
```console
|
|
35
|
+
$ npm install chalk
|
|
40
36
|
```
|
|
41
37
|
|
|
42
38
|
|
|
@@ -50,47 +46,62 @@ console.log(chalk.blue('Hello world!'));
|
|
|
50
46
|
|
|
51
47
|
Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
|
|
52
48
|
|
|
53
|
-
Here without `console.log` for purity.
|
|
54
|
-
|
|
55
49
|
```js
|
|
56
50
|
const chalk = require('chalk');
|
|
51
|
+
const log = console.log;
|
|
57
52
|
|
|
58
|
-
//
|
|
59
|
-
chalk.blue('Hello') + 'World' + chalk.red('!');
|
|
53
|
+
// Combine styled and normal strings
|
|
54
|
+
log(chalk.blue('Hello') + 'World' + chalk.red('!'));
|
|
60
55
|
|
|
61
|
-
//
|
|
62
|
-
chalk.blue.bgRed.bold('Hello world!');
|
|
56
|
+
// Compose multiple styles using the chainable API
|
|
57
|
+
log(chalk.blue.bgRed.bold('Hello world!'));
|
|
63
58
|
|
|
64
|
-
//
|
|
65
|
-
chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');
|
|
59
|
+
// Pass in multiple arguments
|
|
60
|
+
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
|
|
66
61
|
|
|
67
|
-
//
|
|
68
|
-
chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
|
|
62
|
+
// Nest styles
|
|
63
|
+
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
|
|
69
64
|
|
|
70
|
-
//
|
|
71
|
-
chalk.green(
|
|
65
|
+
// Nest styles of the same type even (color, underline, background)
|
|
66
|
+
log(chalk.green(
|
|
72
67
|
'I am a green line ' +
|
|
73
68
|
chalk.blue.underline.bold('with a blue substring') +
|
|
74
69
|
' that becomes green again!'
|
|
75
|
-
);
|
|
70
|
+
));
|
|
76
71
|
|
|
77
72
|
// ES2015 template literal
|
|
78
|
-
|
|
73
|
+
log(`
|
|
79
74
|
CPU: ${chalk.red('90%')}
|
|
80
75
|
RAM: ${chalk.green('40%')}
|
|
81
76
|
DISK: ${chalk.yellow('70%')}
|
|
82
|
-
|
|
77
|
+
`);
|
|
78
|
+
|
|
79
|
+
// ES2015 tagged template literal
|
|
80
|
+
log(chalk`
|
|
81
|
+
CPU: {red ${cpu.totalPercent}%}
|
|
82
|
+
RAM: {green ${ram.used / ram.total * 100}%}
|
|
83
|
+
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
|
|
84
|
+
`);
|
|
85
|
+
|
|
86
|
+
// Use RGB colors in terminal emulators that support it.
|
|
87
|
+
log(chalk.keyword('orange')('Yay for orange colored text!'));
|
|
88
|
+
log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
|
|
89
|
+
log(chalk.hex('#DEADED').bold('Bold gray!'));
|
|
83
90
|
```
|
|
84
91
|
|
|
85
|
-
Easily define your own themes
|
|
92
|
+
Easily define your own themes:
|
|
86
93
|
|
|
87
94
|
```js
|
|
88
95
|
const chalk = require('chalk');
|
|
96
|
+
|
|
89
97
|
const error = chalk.bold.red;
|
|
98
|
+
const warning = chalk.keyword('orange');
|
|
99
|
+
|
|
90
100
|
console.log(error('Error!'));
|
|
101
|
+
console.log(warning('Warning!'));
|
|
91
102
|
```
|
|
92
103
|
|
|
93
|
-
Take advantage of console.log [string substitution](
|
|
104
|
+
Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args):
|
|
94
105
|
|
|
95
106
|
```js
|
|
96
107
|
const name = 'Sindre';
|
|
@@ -105,40 +116,46 @@ console.log(chalk.green('Hello %s'), name);
|
|
|
105
116
|
|
|
106
117
|
Example: `chalk.red.bold.underline('Hello', 'world');`
|
|
107
118
|
|
|
108
|
-
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 `
|
|
119
|
+
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`.
|
|
109
120
|
|
|
110
121
|
Multiple arguments will be separated by space.
|
|
111
122
|
|
|
112
123
|
### chalk.enabled
|
|
113
124
|
|
|
114
|
-
Color support is automatically detected,
|
|
125
|
+
Color support is automatically detected, as is the level (see `chalk.level`). However, if you'd like to simply enable/disable Chalk, you can do so via the `.enabled` property.
|
|
126
|
+
|
|
127
|
+
Chalk is enabled by default unless expicitly disabled via the constructor or `chalk.level` is `0`.
|
|
115
128
|
|
|
116
|
-
If you need to change this in a reusable module create a new instance:
|
|
129
|
+
If you need to change this in a reusable module, create a new instance:
|
|
117
130
|
|
|
118
131
|
```js
|
|
119
132
|
const ctx = new chalk.constructor({enabled: false});
|
|
120
133
|
```
|
|
121
134
|
|
|
122
|
-
### chalk.
|
|
135
|
+
### chalk.level
|
|
123
136
|
|
|
124
|
-
|
|
137
|
+
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.
|
|
125
138
|
|
|
126
|
-
|
|
139
|
+
If you need to change this in a reusable module, create a new instance:
|
|
127
140
|
|
|
128
|
-
|
|
141
|
+
```js
|
|
142
|
+
const ctx = new chalk.constructor({level: 0});
|
|
143
|
+
```
|
|
129
144
|
|
|
130
|
-
|
|
145
|
+
Levels are as follows:
|
|
131
146
|
|
|
132
|
-
|
|
147
|
+
0. All colors disabled
|
|
148
|
+
1. Basic color support (16 colors)
|
|
149
|
+
2. 256 color support
|
|
150
|
+
3. Truecolor support (16 million colors)
|
|
133
151
|
|
|
134
|
-
|
|
135
|
-
const chalk = require('chalk');
|
|
152
|
+
### chalk.supportsColor
|
|
136
153
|
|
|
137
|
-
|
|
138
|
-
//=> {open: '\u001b[31m', close: '\u001b[39m'}
|
|
154
|
+
Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.
|
|
139
155
|
|
|
140
|
-
|
|
141
|
-
|
|
156
|
+
Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, add the environment variable `FORCE_COLOR=1` to forcefully enable color or `FORCE_COLOR=0` to forcefully disable. The use of `FORCE_COLOR` overrides all other color support checks.
|
|
157
|
+
|
|
158
|
+
Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.
|
|
142
159
|
|
|
143
160
|
|
|
144
161
|
## Styles
|
|
@@ -148,11 +165,11 @@ console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
|
|
|
148
165
|
- `reset`
|
|
149
166
|
- `bold`
|
|
150
167
|
- `dim`
|
|
151
|
-
- `italic` *(
|
|
168
|
+
- `italic` *(Not widely supported)*
|
|
152
169
|
- `underline`
|
|
153
170
|
- `inverse`
|
|
154
171
|
- `hidden`
|
|
155
|
-
- `strikethrough` *(
|
|
172
|
+
- `strikethrough` *(Not widely supported)*
|
|
156
173
|
|
|
157
174
|
### Colors
|
|
158
175
|
|
|
@@ -160,11 +177,18 @@ console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
|
|
|
160
177
|
- `red`
|
|
161
178
|
- `green`
|
|
162
179
|
- `yellow`
|
|
163
|
-
- `blue` *(
|
|
180
|
+
- `blue` *(On Windows the bright version is used since normal blue is illegible)*
|
|
164
181
|
- `magenta`
|
|
165
182
|
- `cyan`
|
|
166
183
|
- `white`
|
|
167
|
-
- `gray`
|
|
184
|
+
- `gray` ("bright black")
|
|
185
|
+
- `redBright`
|
|
186
|
+
- `greenBright`
|
|
187
|
+
- `yellowBright`
|
|
188
|
+
- `blueBright`
|
|
189
|
+
- `magentaBright`
|
|
190
|
+
- `cyanBright`
|
|
191
|
+
- `whiteBright`
|
|
168
192
|
|
|
169
193
|
### Background colors
|
|
170
194
|
|
|
@@ -176,11 +200,74 @@ console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
|
|
|
176
200
|
- `bgMagenta`
|
|
177
201
|
- `bgCyan`
|
|
178
202
|
- `bgWhite`
|
|
203
|
+
- `bgBlackBright`
|
|
204
|
+
- `bgRedBright`
|
|
205
|
+
- `bgGreenBright`
|
|
206
|
+
- `bgYellowBright`
|
|
207
|
+
- `bgBlueBright`
|
|
208
|
+
- `bgMagentaBright`
|
|
209
|
+
- `bgCyanBright`
|
|
210
|
+
- `bgWhiteBright`
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
## Tagged template literal
|
|
214
|
+
|
|
215
|
+
Chalk can be used as a [tagged template literal](http://exploringjs.com/es6/ch_template-literals.html#_tagged-template-literals).
|
|
216
|
+
|
|
217
|
+
```js
|
|
218
|
+
const chalk = require('chalk');
|
|
219
|
+
|
|
220
|
+
const miles = 18;
|
|
221
|
+
const calculateFeet = miles => miles * 5280;
|
|
222
|
+
|
|
223
|
+
console.log(chalk`
|
|
224
|
+
There are {bold 5280 feet} in a mile.
|
|
225
|
+
In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
|
|
226
|
+
`);
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`).
|
|
230
|
+
|
|
231
|
+
Template styles are chained exactly like normal Chalk styles. The following two statements are equivalent:
|
|
232
|
+
|
|
233
|
+
```js
|
|
234
|
+
console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
|
|
235
|
+
console.log(chalk`{bold.rgb(10,100,200) Hello!}`);
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Note that function styles (`rgb()`, `hsl()`, `keyword()`, etc.) may not contain spaces between parameters.
|
|
239
|
+
|
|
240
|
+
All interpolated values (`` chalk`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped.
|
|
179
241
|
|
|
180
242
|
|
|
181
|
-
## 256
|
|
243
|
+
## 256 and Truecolor color support
|
|
182
244
|
|
|
183
|
-
Chalk
|
|
245
|
+
Chalk supports 256 colors and [Truecolor](https://gist.github.com/XVilka/8346728) (16 million colors) on supported terminal apps.
|
|
246
|
+
|
|
247
|
+
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).
|
|
248
|
+
|
|
249
|
+
Examples:
|
|
250
|
+
|
|
251
|
+
- `chalk.hex('#DEADED').underline('Hello, world!')`
|
|
252
|
+
- `chalk.keyword('orange')('Some orange text')`
|
|
253
|
+
- `chalk.rgb(15, 100, 204).inverse('Hello!')`
|
|
254
|
+
|
|
255
|
+
Background versions of these models are prefixed with `bg` and the first level of the module capitalized (e.g. `keyword` for foreground colors and `bgKeyword` for background colors).
|
|
256
|
+
|
|
257
|
+
- `chalk.bgHex('#DEADED').underline('Hello, world!')`
|
|
258
|
+
- `chalk.bgKeyword('orange')('Some orange text')`
|
|
259
|
+
- `chalk.bgRgb(15, 100, 204).inverse('Hello!')`
|
|
260
|
+
|
|
261
|
+
The following color models can be used:
|
|
262
|
+
|
|
263
|
+
- [`rgb`](https://en.wikipedia.org/wiki/RGB_color_model) - Example: `chalk.rgb(255, 136, 0).bold('Orange!')`
|
|
264
|
+
- [`hex`](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet) - Example: `chalk.hex('#FF8800').bold('Orange!')`
|
|
265
|
+
- [`keyword`](https://www.w3.org/wiki/CSS/Properties/color/keywords) (CSS keywords) - Example: `chalk.keyword('orange').bold('Orange!')`
|
|
266
|
+
- [`hsl`](https://en.wikipedia.org/wiki/HSL_and_HSV) - Example: `chalk.hsl(32, 100, 50).bold('Orange!')`
|
|
267
|
+
- [`hsv`](https://en.wikipedia.org/wiki/HSL_and_HSV) - Example: `chalk.hsl(32, 1, 1).bold('Orange!')`
|
|
268
|
+
- [`hwb`](https://en.wikipedia.org/wiki/HWB_color_model) - Example: `chalk.hsl(32, 0, 50).bold('Orange!')`
|
|
269
|
+
- `ansi16`
|
|
270
|
+
- `ansi256`
|
|
184
271
|
|
|
185
272
|
|
|
186
273
|
## Windows
|
|
@@ -188,18 +275,32 @@ Chalk does not support anything other than the base eight colors, which guarante
|
|
|
188
275
|
If you're on Windows, do yourself a favor and use [`cmder`](http://cmder.net/) instead of `cmd.exe`.
|
|
189
276
|
|
|
190
277
|
|
|
278
|
+
## Origin story
|
|
279
|
+
|
|
280
|
+
[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.
|
|
281
|
+
|
|
282
|
+
|
|
191
283
|
## Related
|
|
192
284
|
|
|
193
285
|
- [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
|
|
194
|
-
- [ansi-styles](https://github.com/chalk/ansi-styles
|
|
195
|
-
- [supports-color](https://github.com/chalk/supports-color
|
|
286
|
+
- [ansi-styles](https://github.com/chalk/ansi-styles) - ANSI escape codes for styling strings in the terminal
|
|
287
|
+
- [supports-color](https://github.com/chalk/supports-color) - Detect whether a terminal supports color
|
|
196
288
|
- [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes
|
|
197
289
|
- [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
|
|
198
290
|
- [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
|
|
199
291
|
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
|
|
200
292
|
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
|
|
293
|
+
- [color-convert](https://github.com/qix-/color-convert) - Converts colors between different models
|
|
294
|
+
- [chalk-animation](https://github.com/bokub/chalk-animation) - Animate strings in the terminal
|
|
295
|
+
- [gradient-string](https://github.com/bokub/gradient-string) - Apply color gradients to strings
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
## Maintainers
|
|
299
|
+
|
|
300
|
+
- [Sindre Sorhus](https://github.com/sindresorhus)
|
|
301
|
+
- [Josh Junon](https://github.com/qix-)
|
|
201
302
|
|
|
202
303
|
|
|
203
304
|
## License
|
|
204
305
|
|
|
205
|
-
MIT
|
|
306
|
+
MIT
|
package/templates.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const TEMPLATE_REGEX = /(?:\\(u[a-f0-9]{4}|x[a-f0-9]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi;
|
|
3
|
+
const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
|
|
4
|
+
const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
|
|
5
|
+
const ESCAPE_REGEX = /\\(u[0-9a-f]{4}|x[0-9a-f]{2}|.)|([^\\])/gi;
|
|
6
|
+
|
|
7
|
+
const ESCAPES = {
|
|
8
|
+
n: '\n',
|
|
9
|
+
r: '\r',
|
|
10
|
+
t: '\t',
|
|
11
|
+
b: '\b',
|
|
12
|
+
f: '\f',
|
|
13
|
+
v: '\v',
|
|
14
|
+
0: '\0',
|
|
15
|
+
'\\': '\\',
|
|
16
|
+
e: '\u001b',
|
|
17
|
+
a: '\u0007'
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function unescape(c) {
|
|
21
|
+
if ((c[0] === 'u' && c.length === 5) || (c[0] === 'x' && c.length === 3)) {
|
|
22
|
+
return String.fromCharCode(parseInt(c.slice(1), 16));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return ESCAPES[c] || c;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function parseArguments(name, args) {
|
|
29
|
+
const results = [];
|
|
30
|
+
const chunks = args.trim().split(/\s*,\s*/g);
|
|
31
|
+
let matches;
|
|
32
|
+
|
|
33
|
+
for (const chunk of chunks) {
|
|
34
|
+
if (!isNaN(chunk)) {
|
|
35
|
+
results.push(Number(chunk));
|
|
36
|
+
} else if ((matches = chunk.match(STRING_REGEX))) {
|
|
37
|
+
results.push(matches[2].replace(ESCAPE_REGEX, (m, escape, chr) => escape ? unescape(escape) : chr));
|
|
38
|
+
} else {
|
|
39
|
+
throw new Error(`Invalid Chalk template style argument: ${chunk} (in style '${name}')`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return results;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function parseStyle(style) {
|
|
47
|
+
STYLE_REGEX.lastIndex = 0;
|
|
48
|
+
|
|
49
|
+
const results = [];
|
|
50
|
+
let matches;
|
|
51
|
+
|
|
52
|
+
while ((matches = STYLE_REGEX.exec(style)) !== null) {
|
|
53
|
+
const name = matches[1];
|
|
54
|
+
|
|
55
|
+
if (matches[2]) {
|
|
56
|
+
const args = parseArguments(name, matches[2]);
|
|
57
|
+
results.push([name].concat(args));
|
|
58
|
+
} else {
|
|
59
|
+
results.push([name]);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return results;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function buildStyle(chalk, styles) {
|
|
67
|
+
const enabled = {};
|
|
68
|
+
|
|
69
|
+
for (const layer of styles) {
|
|
70
|
+
for (const style of layer.styles) {
|
|
71
|
+
enabled[style[0]] = layer.inverse ? null : style.slice(1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let current = chalk;
|
|
76
|
+
for (const styleName of Object.keys(enabled)) {
|
|
77
|
+
if (Array.isArray(enabled[styleName])) {
|
|
78
|
+
if (!(styleName in current)) {
|
|
79
|
+
throw new Error(`Unknown Chalk style: ${styleName}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (enabled[styleName].length > 0) {
|
|
83
|
+
current = current[styleName].apply(current, enabled[styleName]);
|
|
84
|
+
} else {
|
|
85
|
+
current = current[styleName];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return current;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = (chalk, tmp) => {
|
|
94
|
+
const styles = [];
|
|
95
|
+
const chunks = [];
|
|
96
|
+
let chunk = [];
|
|
97
|
+
|
|
98
|
+
// eslint-disable-next-line max-params
|
|
99
|
+
tmp.replace(TEMPLATE_REGEX, (m, escapeChar, inverse, style, close, chr) => {
|
|
100
|
+
if (escapeChar) {
|
|
101
|
+
chunk.push(unescape(escapeChar));
|
|
102
|
+
} else if (style) {
|
|
103
|
+
const str = chunk.join('');
|
|
104
|
+
chunk = [];
|
|
105
|
+
chunks.push(styles.length === 0 ? str : buildStyle(chalk, styles)(str));
|
|
106
|
+
styles.push({inverse, styles: parseStyle(style)});
|
|
107
|
+
} else if (close) {
|
|
108
|
+
if (styles.length === 0) {
|
|
109
|
+
throw new Error('Found extraneous } in Chalk template literal');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
chunks.push(buildStyle(chalk, styles)(chunk.join('')));
|
|
113
|
+
chunk = [];
|
|
114
|
+
styles.pop();
|
|
115
|
+
} else {
|
|
116
|
+
chunk.push(chr);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
chunks.push(chunk.join(''));
|
|
121
|
+
|
|
122
|
+
if (styles.length > 0) {
|
|
123
|
+
const errMsg = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
|
|
124
|
+
throw new Error(errMsg);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return chunks.join('');
|
|
128
|
+
};
|