chalk 2.0.1 → 2.3.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 +29 -15
- package/package.json +11 -7
- package/readme.md +13 -6
- package/templates.js +94 -141
- package/types/index.d.ts +97 -0
package/index.js
CHANGED
|
@@ -19,13 +19,14 @@ function applyOptions(obj, options) {
|
|
|
19
19
|
options = options || {};
|
|
20
20
|
|
|
21
21
|
// Detect level if not set manually
|
|
22
|
-
|
|
22
|
+
const scLevel = supportsColor ? supportsColor.level : 0;
|
|
23
|
+
obj.level = options.level === undefined ? scLevel : options.level;
|
|
23
24
|
obj.enabled = 'enabled' in options ? options.enabled : obj.level > 0;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
function Chalk(options) {
|
|
27
|
-
// We check for this.template here since calling chalk.constructor()
|
|
28
|
-
// by itself will have a `this` of a previously constructed chalk object
|
|
28
|
+
// We check for this.template here since calling `chalk.constructor()`
|
|
29
|
+
// by itself will have a `this` of a previously constructed chalk object
|
|
29
30
|
if (!this || !(this instanceof Chalk) || this.template) {
|
|
30
31
|
const chalk = {};
|
|
31
32
|
applyOptions(chalk, options);
|
|
@@ -57,11 +58,17 @@ for (const key of Object.keys(ansiStyles)) {
|
|
|
57
58
|
styles[key] = {
|
|
58
59
|
get() {
|
|
59
60
|
const codes = ansiStyles[key];
|
|
60
|
-
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], key);
|
|
61
|
+
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, key);
|
|
61
62
|
}
|
|
62
63
|
};
|
|
63
64
|
}
|
|
64
65
|
|
|
66
|
+
styles.visible = {
|
|
67
|
+
get() {
|
|
68
|
+
return build.call(this, this._styles || [], true, 'visible');
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
65
72
|
ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
|
|
66
73
|
for (const model of Object.keys(ansiStyles.color.ansi)) {
|
|
67
74
|
if (skipModels.has(model)) {
|
|
@@ -78,7 +85,7 @@ for (const model of Object.keys(ansiStyles.color.ansi)) {
|
|
|
78
85
|
close: ansiStyles.color.close,
|
|
79
86
|
closeRe: ansiStyles.color.closeRe
|
|
80
87
|
};
|
|
81
|
-
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
|
|
88
|
+
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, model);
|
|
82
89
|
};
|
|
83
90
|
}
|
|
84
91
|
};
|
|
@@ -101,7 +108,7 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
|
|
|
101
108
|
close: ansiStyles.bgColor.close,
|
|
102
109
|
closeRe: ansiStyles.bgColor.closeRe
|
|
103
110
|
};
|
|
104
|
-
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
|
|
111
|
+
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, model);
|
|
105
112
|
};
|
|
106
113
|
}
|
|
107
114
|
};
|
|
@@ -109,12 +116,13 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
|
|
|
109
116
|
|
|
110
117
|
const proto = Object.defineProperties(() => {}, styles);
|
|
111
118
|
|
|
112
|
-
function build(_styles, key) {
|
|
119
|
+
function build(_styles, _empty, key) {
|
|
113
120
|
const builder = function () {
|
|
114
121
|
return applyStyle.apply(builder, arguments);
|
|
115
122
|
};
|
|
116
123
|
|
|
117
124
|
builder._styles = _styles;
|
|
125
|
+
builder._empty = _empty;
|
|
118
126
|
|
|
119
127
|
const self = this;
|
|
120
128
|
|
|
@@ -142,7 +150,7 @@ function build(_styles, key) {
|
|
|
142
150
|
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
|
|
143
151
|
|
|
144
152
|
// `__proto__` is used because we must return a function, but there is
|
|
145
|
-
// no way to create a function with a different prototype
|
|
153
|
+
// no way to create a function with a different prototype
|
|
146
154
|
builder.__proto__ = proto; // eslint-disable-line no-proto
|
|
147
155
|
|
|
148
156
|
return builder;
|
|
@@ -152,7 +160,11 @@ function applyStyle() {
|
|
|
152
160
|
// Support varags, but simply cast to string in case there's only one arg
|
|
153
161
|
const args = arguments;
|
|
154
162
|
const argsLen = args.length;
|
|
155
|
-
let str =
|
|
163
|
+
let str = String(arguments[0]);
|
|
164
|
+
|
|
165
|
+
if (argsLen === 0) {
|
|
166
|
+
return '';
|
|
167
|
+
}
|
|
156
168
|
|
|
157
169
|
if (argsLen > 1) {
|
|
158
170
|
// Don't slice `arguments`, it prevents V8 optimizations
|
|
@@ -162,7 +174,7 @@ function applyStyle() {
|
|
|
162
174
|
}
|
|
163
175
|
|
|
164
176
|
if (!this.enabled || this.level <= 0 || !str) {
|
|
165
|
-
return str;
|
|
177
|
+
return this._empty ? '' : str;
|
|
166
178
|
}
|
|
167
179
|
|
|
168
180
|
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
|
|
@@ -192,17 +204,18 @@ function applyStyle() {
|
|
|
192
204
|
}
|
|
193
205
|
|
|
194
206
|
function chalkTag(chalk, strings) {
|
|
195
|
-
const args = [].slice.call(arguments, 2);
|
|
196
|
-
|
|
197
207
|
if (!Array.isArray(strings)) {
|
|
198
|
-
|
|
208
|
+
// If chalk() was called by itself or with a string,
|
|
209
|
+
// return the string itself as a string.
|
|
210
|
+
return [].slice.call(arguments, 1).join(' ');
|
|
199
211
|
}
|
|
200
212
|
|
|
213
|
+
const args = [].slice.call(arguments, 2);
|
|
201
214
|
const parts = [strings.raw[0]];
|
|
202
215
|
|
|
203
216
|
for (let i = 1; i < strings.length; i++) {
|
|
204
|
-
parts.push(args[i - 1]
|
|
205
|
-
parts.push(strings.raw[i]);
|
|
217
|
+
parts.push(String(args[i - 1]).replace(/[{}\\]/g, '\\$&'));
|
|
218
|
+
parts.push(String(strings.raw[i]));
|
|
206
219
|
}
|
|
207
220
|
|
|
208
221
|
return template(chalk, parts.join(''));
|
|
@@ -212,3 +225,4 @@ Object.defineProperties(Chalk.prototype, styles);
|
|
|
212
225
|
|
|
213
226
|
module.exports = Chalk(); // eslint-disable-line new-cap
|
|
214
227
|
module.exports.supportsColor = supportsColor;
|
|
228
|
+
module.exports.default = module.exports; // For TypeScript
|
package/package.json
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chalk",
|
|
3
|
-
"version": "2.0
|
|
4
|
-
"description": "Terminal string styling done right
|
|
3
|
+
"version": "2.3.0",
|
|
4
|
+
"description": "Terminal string styling done right",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "chalk/chalk",
|
|
7
7
|
"engines": {
|
|
8
8
|
"node": ">=4"
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
|
-
"test": "xo && nyc
|
|
11
|
+
"test": "xo && tsc --project types && nyc ava",
|
|
12
12
|
"bench": "matcha benchmark.js",
|
|
13
13
|
"coveralls": "nyc report --reporter=text-lcov | coveralls"
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
16
|
"index.js",
|
|
17
|
-
"templates.js"
|
|
17
|
+
"templates.js",
|
|
18
|
+
"types/index.d.ts"
|
|
18
19
|
],
|
|
19
20
|
"keywords": [
|
|
20
21
|
"color",
|
|
@@ -45,14 +46,17 @@
|
|
|
45
46
|
"supports-color": "^4.0.0"
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
48
|
-
"
|
|
49
|
+
"ava": "*",
|
|
50
|
+
"coveralls": "^3.0.0",
|
|
51
|
+
"execa": "^0.8.0",
|
|
49
52
|
"import-fresh": "^2.0.0",
|
|
50
53
|
"matcha": "^0.7.0",
|
|
51
|
-
"mocha": "*",
|
|
52
54
|
"nyc": "^11.0.2",
|
|
53
|
-
"resolve-from": "^
|
|
55
|
+
"resolve-from": "^4.0.0",
|
|
56
|
+
"typescript": "^2.5.3",
|
|
54
57
|
"xo": "*"
|
|
55
58
|
},
|
|
59
|
+
"types": "types/index.d.ts",
|
|
56
60
|
"xo": {
|
|
57
61
|
"envs": [
|
|
58
62
|
"node",
|
package/readme.md
CHANGED
|
@@ -9,11 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
> Terminal string styling done right
|
|
11
11
|
|
|
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)
|
|
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) [](https://github.com/sindresorhus/awesome-nodejs)
|
|
13
13
|
|
|
14
|
-
[
|
|
15
|
-
|
|
16
|
-
**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)
|
|
17
15
|
|
|
18
16
|

|
|
19
17
|
|
|
@@ -172,6 +170,7 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
|
|
|
172
170
|
- `inverse`
|
|
173
171
|
- `hidden`
|
|
174
172
|
- `strikethrough` *(Not widely supported)*
|
|
173
|
+
- `visible` (Text is emitted only if enabled)
|
|
175
174
|
|
|
176
175
|
### Colors
|
|
177
176
|
|
|
@@ -183,8 +182,7 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
|
|
|
183
182
|
- `magenta`
|
|
184
183
|
- `cyan`
|
|
185
184
|
- `white`
|
|
186
|
-
- `gray`
|
|
187
|
-
- `blackBright`
|
|
185
|
+
- `gray` ("bright black")
|
|
188
186
|
- `redBright`
|
|
189
187
|
- `greenBright`
|
|
190
188
|
- `yellowBright`
|
|
@@ -278,17 +276,26 @@ The following color models can be used:
|
|
|
278
276
|
If you're on Windows, do yourself a favor and use [`cmder`](http://cmder.net/) instead of `cmd.exe`.
|
|
279
277
|
|
|
280
278
|
|
|
279
|
+
## Origin story
|
|
280
|
+
|
|
281
|
+
[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.
|
|
282
|
+
|
|
283
|
+
|
|
281
284
|
## Related
|
|
282
285
|
|
|
283
286
|
- [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
|
|
284
287
|
- [ansi-styles](https://github.com/chalk/ansi-styles) - ANSI escape codes for styling strings in the terminal
|
|
285
288
|
- [supports-color](https://github.com/chalk/supports-color) - Detect whether a terminal supports color
|
|
286
289
|
- [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes
|
|
290
|
+
- [strip-ansi-stream](https://github.com/chalk/strip-ansi-stream) - Strip ANSI escape codes from a stream
|
|
287
291
|
- [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
|
|
288
292
|
- [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
|
|
289
293
|
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
|
|
290
294
|
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
|
|
291
295
|
- [color-convert](https://github.com/qix-/color-convert) - Converts colors between different models
|
|
296
|
+
- [chalk-animation](https://github.com/bokub/chalk-animation) - Animate strings in the terminal
|
|
297
|
+
- [gradient-string](https://github.com/bokub/gradient-string) - Apply color gradients to strings
|
|
298
|
+
- [chalk-pipe](https://github.com/LitoMore/chalk-pipe) - Create chalk style schemes with simpler style strings
|
|
292
299
|
|
|
293
300
|
|
|
294
301
|
## Maintainers
|
package/templates.js
CHANGED
|
@@ -1,175 +1,128 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
const TEMPLATE_REGEX = /(?:\\(u[a-f\d]{4}|x[a-f\d]{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[a-f\d]{4}|x[a-f\d]{2}|.)|([^\\])/gi;
|
|
6
|
+
|
|
7
|
+
const ESCAPES = new Map([
|
|
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
|
+
}
|
|
2
24
|
|
|
3
|
-
|
|
4
|
-
return {
|
|
5
|
-
styles: [],
|
|
6
|
-
parent,
|
|
7
|
-
contents: []
|
|
8
|
-
};
|
|
25
|
+
return ESCAPES.get(c) || c;
|
|
9
26
|
}
|
|
10
27
|
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const takeWhileReverse = (array, predicate, start) => {
|
|
17
|
-
const out = [];
|
|
28
|
+
function parseArguments(name, args) {
|
|
29
|
+
const results = [];
|
|
30
|
+
const chunks = args.trim().split(/\s*,\s*/g);
|
|
31
|
+
let matches;
|
|
18
32
|
|
|
19
|
-
for (
|
|
20
|
-
if (
|
|
21
|
-
|
|
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));
|
|
22
38
|
} else {
|
|
23
|
-
|
|
39
|
+
throw new Error(`Invalid Chalk template style argument: ${chunk} (in style '${name}')`);
|
|
24
40
|
}
|
|
25
41
|
}
|
|
26
42
|
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Checks if the character at position i in string is a normal character a.k.a a non control character.
|
|
32
|
-
* */
|
|
33
|
-
const isNormalCharacter = (string, i) => {
|
|
34
|
-
const char = string[i];
|
|
35
|
-
const backslash = '\\';
|
|
36
|
-
|
|
37
|
-
if (!(char === backslash || char === '{' || char === '}')) {
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
43
|
+
return results;
|
|
44
|
+
}
|
|
40
45
|
|
|
41
|
-
|
|
46
|
+
function parseStyle(style) {
|
|
47
|
+
STYLE_REGEX.lastIndex = 0;
|
|
42
48
|
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
const results = [];
|
|
50
|
+
let matches;
|
|
45
51
|
|
|
46
|
-
|
|
52
|
+
while ((matches = STYLE_REGEX.exec(style)) !== null) {
|
|
53
|
+
const name = matches[1];
|
|
47
54
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
* */
|
|
52
|
-
const sumStyles = data => {
|
|
53
|
-
const negateRegex = /^~.+/;
|
|
54
|
-
let out = [];
|
|
55
|
-
|
|
56
|
-
for (const style of collectStyles(data)) {
|
|
57
|
-
if (negateRegex.test(style)) {
|
|
58
|
-
const exclude = style.slice(1);
|
|
59
|
-
out = out.filter(x => x !== exclude);
|
|
55
|
+
if (matches[2]) {
|
|
56
|
+
const args = parseArguments(name, matches[2]);
|
|
57
|
+
results.push([name].concat(args));
|
|
60
58
|
} else {
|
|
61
|
-
|
|
59
|
+
results.push([name]);
|
|
62
60
|
}
|
|
63
61
|
}
|
|
64
62
|
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Takes a string and parses it into a tree of data objects which inherit styles from their parent.
|
|
70
|
-
* */
|
|
71
|
-
function parse(string) {
|
|
72
|
-
const root = data(null);
|
|
73
|
-
let pushingStyle = false;
|
|
74
|
-
|
|
75
|
-
let current = root;
|
|
63
|
+
return results;
|
|
64
|
+
}
|
|
76
65
|
|
|
77
|
-
|
|
78
|
-
|
|
66
|
+
function buildStyle(chalk, styles) {
|
|
67
|
+
const enabled = {};
|
|
79
68
|
|
|
80
|
-
|
|
81
|
-
|
|
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
|
+
}
|
|
82
74
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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}`);
|
|
87
80
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (' \t'.indexOf(char) > -1) {
|
|
92
|
-
pushingStyle = false;
|
|
93
|
-
} else if (char === '\n') {
|
|
94
|
-
pushingStyle = false;
|
|
95
|
-
addNormalCharacter();
|
|
96
|
-
} else if (char === '.') {
|
|
97
|
-
current.styles.push('');
|
|
81
|
+
|
|
82
|
+
if (enabled[styleName].length > 0) {
|
|
83
|
+
current = current[styleName].apply(current, enabled[styleName]);
|
|
98
84
|
} else {
|
|
99
|
-
current
|
|
85
|
+
current = current[styleName];
|
|
100
86
|
}
|
|
101
|
-
} else if (isNormalCharacter(string, i)) {
|
|
102
|
-
addNormalCharacter();
|
|
103
|
-
} else if (char === '{') {
|
|
104
|
-
pushingStyle = true;
|
|
105
|
-
const nCurrent = data(current);
|
|
106
|
-
current.contents.push(nCurrent);
|
|
107
|
-
current = nCurrent;
|
|
108
|
-
} else if (char === '}') {
|
|
109
|
-
current = current.parent;
|
|
110
87
|
}
|
|
111
88
|
}
|
|
112
89
|
|
|
113
|
-
|
|
114
|
-
throw new Error('literal template has an unclosed block');
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return root;
|
|
90
|
+
return current;
|
|
118
91
|
}
|
|
119
92
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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();
|
|
133
115
|
} else {
|
|
134
|
-
|
|
116
|
+
chunk.push(chr);
|
|
135
117
|
}
|
|
136
|
-
}
|
|
118
|
+
});
|
|
137
119
|
|
|
138
|
-
|
|
139
|
-
}
|
|
120
|
+
chunks.push(chunk.join(''));
|
|
140
121
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
throw new Error(
|
|
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);
|
|
144
125
|
}
|
|
145
|
-
}
|
|
146
126
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
* */
|
|
150
|
-
function parseStyle(chalk, style) {
|
|
151
|
-
const fnMatch = style.match(/^\s*(\w+)\s*\(\s*([^)]*)\s*\)\s*/);
|
|
152
|
-
if (!fnMatch) {
|
|
153
|
-
assertStyle(chalk, style);
|
|
154
|
-
return chalk[style];
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const name = fnMatch[1].trim();
|
|
158
|
-
const args = fnMatch[2].split(/,/g).map(s => s.trim());
|
|
159
|
-
|
|
160
|
-
assertStyle(chalk, name);
|
|
161
|
-
|
|
162
|
-
return chalk[name].apply(chalk, args);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Performs the actual styling of the string, essentially lifted from cli.js.
|
|
167
|
-
* */
|
|
168
|
-
function style(chalk, flat) {
|
|
169
|
-
return flat.map(data => {
|
|
170
|
-
const fn = data.styles.reduce(parseStyle, chalk);
|
|
171
|
-
return fn(data.content.replace(/\n$/, ''));
|
|
172
|
-
}).join('');
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
module.exports = (chalk, string) => style(chalk, flatten(parse(string)));
|
|
127
|
+
return chunks.join('');
|
|
128
|
+
};
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Type definitions for Chalk
|
|
2
|
+
// Definitions by: Thomas Sauer <https://github.com/t-sauer>
|
|
3
|
+
|
|
4
|
+
export const enum Level {
|
|
5
|
+
None = 0,
|
|
6
|
+
Basic = 1,
|
|
7
|
+
Ansi256 = 2,
|
|
8
|
+
TrueColor = 3
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ChalkOptions {
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
level?: Level;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ChalkConstructor {
|
|
17
|
+
new (options?: ChalkOptions): Chalk;
|
|
18
|
+
(options?: ChalkOptions): Chalk;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ColorSupport {
|
|
22
|
+
level: Level;
|
|
23
|
+
hasBasic: boolean;
|
|
24
|
+
has256: boolean;
|
|
25
|
+
has16m: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Chalk {
|
|
29
|
+
(...text: string[]): string;
|
|
30
|
+
(text: TemplateStringsArray, ...placeholders: string[]): string;
|
|
31
|
+
constructor: ChalkConstructor;
|
|
32
|
+
enabled: boolean;
|
|
33
|
+
level: Level;
|
|
34
|
+
rgb(r: number, g: number, b: number): this;
|
|
35
|
+
hsl(h: number, s: number, l: number): this;
|
|
36
|
+
hsv(h: number, s: number, v: number): this;
|
|
37
|
+
hwb(h: number, w: number, b: number): this;
|
|
38
|
+
bgHex(color: string): this;
|
|
39
|
+
bgKeyword(color: string): this;
|
|
40
|
+
bgRgb(r: number, g: number, b: number): this;
|
|
41
|
+
bgHsl(h: number, s: number, l: number): this;
|
|
42
|
+
bgHsv(h: number, s: number, v: number): this;
|
|
43
|
+
bgHwb(h: number, w: number, b: number): this;
|
|
44
|
+
hex(color: string): this;
|
|
45
|
+
keyword(color: string): this;
|
|
46
|
+
|
|
47
|
+
readonly reset: this;
|
|
48
|
+
readonly bold: this;
|
|
49
|
+
readonly dim: this;
|
|
50
|
+
readonly italic: this;
|
|
51
|
+
readonly underline: this;
|
|
52
|
+
readonly inverse: this;
|
|
53
|
+
readonly hidden: this;
|
|
54
|
+
readonly strikethrough: this;
|
|
55
|
+
|
|
56
|
+
readonly visible: this;
|
|
57
|
+
|
|
58
|
+
readonly black: this;
|
|
59
|
+
readonly red: this;
|
|
60
|
+
readonly green: this;
|
|
61
|
+
readonly yellow: this;
|
|
62
|
+
readonly blue: this;
|
|
63
|
+
readonly magenta: this;
|
|
64
|
+
readonly cyan: this;
|
|
65
|
+
readonly white: this;
|
|
66
|
+
readonly gray: this;
|
|
67
|
+
readonly grey: this;
|
|
68
|
+
readonly blackBright: this;
|
|
69
|
+
readonly redBright: this;
|
|
70
|
+
readonly greenBright: this;
|
|
71
|
+
readonly yellowBright: this;
|
|
72
|
+
readonly blueBright: this;
|
|
73
|
+
readonly magentaBright: this;
|
|
74
|
+
readonly cyanBright: this;
|
|
75
|
+
readonly whiteBright: this;
|
|
76
|
+
|
|
77
|
+
readonly bgBlack: this;
|
|
78
|
+
readonly bgRed: this;
|
|
79
|
+
readonly bgGreen: this;
|
|
80
|
+
readonly bgYellow: this;
|
|
81
|
+
readonly bgBlue: this;
|
|
82
|
+
readonly bgMagenta: this;
|
|
83
|
+
readonly bgCyan: this;
|
|
84
|
+
readonly bgWhite: this;
|
|
85
|
+
readonly bgBlackBright: this;
|
|
86
|
+
readonly bgRedBright: this;
|
|
87
|
+
readonly bgGreenBright: this;
|
|
88
|
+
readonly bgYellowBright: this;
|
|
89
|
+
readonly bgBlueBright: this;
|
|
90
|
+
readonly bgMagentaBright: this;
|
|
91
|
+
readonly bgCyanBright: this;
|
|
92
|
+
readonly bgWhiteBright: this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
declare const chalk: Chalk & { supportsColor: ColorSupport };
|
|
96
|
+
|
|
97
|
+
export default chalk
|