chalk 2.4.2 → 4.0.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.d.ts +408 -0
- package/package.json +26 -29
- package/readme.md +39 -61
- package/source/index.js +222 -0
- package/{templates.js → source/templates.js} +33 -27
- package/source/util.js +39 -0
- package/index.js +0 -228
- package/index.js.flow +0 -93
- package/types/index.d.ts +0 -97
package/readme.md
CHANGED
|
@@ -9,12 +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/xojs/xo) [
|
|
15
|
-
|
|
16
|
-
<img src="https://cdn.rawgit.com/chalk/ansi-styles/8261697c95bf34b6c7767e2cbe9941a851d59385/screenshot.svg" alt="" width="900">
|
|
12
|
+
[](https://travis-ci.org/chalk/chalk) [](https://coveralls.io/github/chalk/chalk?branch=master) [](https://www.npmjs.com/package/chalk?activeTab=dependents) [](https://www.npmjs.com/package/chalk) [](https://www.youtube.com/watch?v=9auOCbH5Ns4) [](https://github.com/xojs/xo)  [](https://repl.it/github/chalk/chalk)
|
|
17
13
|
|
|
14
|
+
<img src="https://cdn.jsdelivr.net/gh/chalk/ansi-styles@8261697c95bf34b6c7767e2cbe9941a851d59385/screenshot.svg" width="900">
|
|
18
15
|
|
|
19
16
|
## Highlights
|
|
20
17
|
|
|
@@ -26,8 +23,7 @@
|
|
|
26
23
|
- Doesn't extend `String.prototype`
|
|
27
24
|
- Clean and focused
|
|
28
25
|
- Actively maintained
|
|
29
|
-
- [Used by ~
|
|
30
|
-
|
|
26
|
+
- [Used by ~50,000 packages](https://www.npmjs.com/browse/depended/chalk) as of January 1, 2020
|
|
31
27
|
|
|
32
28
|
## Install
|
|
33
29
|
|
|
@@ -35,11 +31,6 @@
|
|
|
35
31
|
$ npm install chalk
|
|
36
32
|
```
|
|
37
33
|
|
|
38
|
-
<a href="https://www.patreon.com/sindresorhus">
|
|
39
|
-
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
|
|
40
|
-
</a>
|
|
41
|
-
|
|
42
|
-
|
|
43
34
|
## Usage
|
|
44
35
|
|
|
45
36
|
```js
|
|
@@ -113,7 +104,6 @@ console.log(chalk.green('Hello %s'), name);
|
|
|
113
104
|
//=> 'Hello Sindre'
|
|
114
105
|
```
|
|
115
106
|
|
|
116
|
-
|
|
117
107
|
## API
|
|
118
108
|
|
|
119
109
|
### chalk.`<style>[.<style>...](string, [string...])`
|
|
@@ -124,57 +114,50 @@ Chain [styles](#styles) and call the last one as a method with a string argument
|
|
|
124
114
|
|
|
125
115
|
Multiple arguments will be separated by space.
|
|
126
116
|
|
|
127
|
-
### chalk.enabled
|
|
128
|
-
|
|
129
|
-
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.
|
|
130
|
-
|
|
131
|
-
Chalk is enabled by default unless explicitly disabled via the constructor or `chalk.level` is `0`.
|
|
132
|
-
|
|
133
|
-
If you need to change this in a reusable module, create a new instance:
|
|
134
|
-
|
|
135
|
-
```js
|
|
136
|
-
const ctx = new chalk.constructor({enabled: false});
|
|
137
|
-
```
|
|
138
|
-
|
|
139
117
|
### chalk.level
|
|
140
118
|
|
|
119
|
+
Specifies the level of color support.
|
|
120
|
+
|
|
141
121
|
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.
|
|
142
122
|
|
|
143
123
|
If you need to change this in a reusable module, create a new instance:
|
|
144
124
|
|
|
145
125
|
```js
|
|
146
|
-
const ctx = new chalk.
|
|
126
|
+
const ctx = new chalk.Instance({level: 0});
|
|
147
127
|
```
|
|
148
128
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
0
|
|
152
|
-
1
|
|
153
|
-
2
|
|
154
|
-
3
|
|
129
|
+
| Level | Description |
|
|
130
|
+
| :---: | :--- |
|
|
131
|
+
| `0` | All colors disabled |
|
|
132
|
+
| `1` | Basic color support (16 colors) |
|
|
133
|
+
| `2` | 256 color support |
|
|
134
|
+
| `3` | Truecolor support (16 million colors) |
|
|
155
135
|
|
|
156
136
|
### chalk.supportsColor
|
|
157
137
|
|
|
158
138
|
Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.
|
|
159
139
|
|
|
160
|
-
Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible,
|
|
140
|
+
Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, use the environment variable `FORCE_COLOR=1` (level 1), `FORCE_COLOR=2` (level 2), or `FORCE_COLOR=3` (level 3) to forcefully enable color, or `FORCE_COLOR=0` to forcefully disable. The use of `FORCE_COLOR` overrides all other color support checks.
|
|
161
141
|
|
|
162
142
|
Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.
|
|
163
143
|
|
|
144
|
+
### chalk.stderr and chalk.stderr.supportsColor
|
|
145
|
+
|
|
146
|
+
`chalk.stderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `chalk.supportsColor` apply to this too. `chalk.stderr.supportsColor` is exposed for convenience.
|
|
164
147
|
|
|
165
148
|
## Styles
|
|
166
149
|
|
|
167
150
|
### Modifiers
|
|
168
151
|
|
|
169
|
-
- `reset`
|
|
170
|
-
- `bold`
|
|
171
|
-
- `dim`
|
|
172
|
-
- `italic` *(Not widely supported)*
|
|
173
|
-
- `underline`
|
|
174
|
-
- `inverse
|
|
175
|
-
- `hidden`
|
|
176
|
-
- `strikethrough` *(Not widely supported)*
|
|
177
|
-
- `visible
|
|
152
|
+
- `reset` - Resets the current color chain.
|
|
153
|
+
- `bold` - Make text bold.
|
|
154
|
+
- `dim` - Emitting only a small amount of light.
|
|
155
|
+
- `italic` - Make text italic. *(Not widely supported)*
|
|
156
|
+
- `underline` - Make text underline. *(Not widely supported)*
|
|
157
|
+
- `inverse`- Inverse background and foreground colors.
|
|
158
|
+
- `hidden` - Prints the text, but makes it invisible.
|
|
159
|
+
- `strikethrough` - Puts a horizontal line through the center of the text. *(Not widely supported)*
|
|
160
|
+
- `visible`- Prints the text only when Chalk has a color level > 0. Can be useful for things that are purely cosmetic.
|
|
178
161
|
|
|
179
162
|
### Colors
|
|
180
163
|
|
|
@@ -182,11 +165,11 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
|
|
|
182
165
|
- `red`
|
|
183
166
|
- `green`
|
|
184
167
|
- `yellow`
|
|
185
|
-
- `blue`
|
|
168
|
+
- `blue`
|
|
186
169
|
- `magenta`
|
|
187
170
|
- `cyan`
|
|
188
171
|
- `white`
|
|
189
|
-
- `
|
|
172
|
+
- `blackBright` (alias: `gray`, `grey`)
|
|
190
173
|
- `redBright`
|
|
191
174
|
- `greenBright`
|
|
192
175
|
- `yellowBright`
|
|
@@ -205,7 +188,7 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
|
|
|
205
188
|
- `bgMagenta`
|
|
206
189
|
- `bgCyan`
|
|
207
190
|
- `bgWhite`
|
|
208
|
-
- `bgBlackBright`
|
|
191
|
+
- `bgBlackBright` (alias: `bgGray`, `bgGrey`)
|
|
209
192
|
- `bgRedBright`
|
|
210
193
|
- `bgGreenBright`
|
|
211
194
|
- `bgYellowBright`
|
|
@@ -214,7 +197,6 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
|
|
|
214
197
|
- `bgCyanBright`
|
|
215
198
|
- `bgWhiteBright`
|
|
216
199
|
|
|
217
|
-
|
|
218
200
|
## Tagged template literal
|
|
219
201
|
|
|
220
202
|
Chalk can be used as a [tagged template literal](http://exploringjs.com/es6/ch_template-literals.html#_tagged-template-literals).
|
|
@@ -226,8 +208,8 @@ const miles = 18;
|
|
|
226
208
|
const calculateFeet = miles => miles * 5280;
|
|
227
209
|
|
|
228
210
|
console.log(chalk`
|
|
229
|
-
|
|
230
|
-
|
|
211
|
+
There are {bold 5280 feet} in a mile.
|
|
212
|
+
In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
|
|
231
213
|
`);
|
|
232
214
|
```
|
|
233
215
|
|
|
@@ -244,7 +226,6 @@ Note that function styles (`rgb()`, `hsl()`, `keyword()`, etc.) may not contain
|
|
|
244
226
|
|
|
245
227
|
All interpolated values (`` chalk`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped.
|
|
246
228
|
|
|
247
|
-
|
|
248
229
|
## 256 and Truecolor color support
|
|
249
230
|
|
|
250
231
|
Chalk supports 256 colors and [Truecolor](https://gist.github.com/XVilka/8346728) (16 million colors) on supported terminal apps.
|
|
@@ -270,20 +251,23 @@ The following color models can be used:
|
|
|
270
251
|
- [`keyword`](https://www.w3.org/wiki/CSS/Properties/color/keywords) (CSS keywords) - Example: `chalk.keyword('orange').bold('Orange!')`
|
|
271
252
|
- [`hsl`](https://en.wikipedia.org/wiki/HSL_and_HSV) - Example: `chalk.hsl(32, 100, 50).bold('Orange!')`
|
|
272
253
|
- [`hsv`](https://en.wikipedia.org/wiki/HSL_and_HSV) - Example: `chalk.hsv(32, 100, 100).bold('Orange!')`
|
|
273
|
-
- [`hwb`](https://en.wikipedia.org/wiki/HWB_color_model)
|
|
274
|
-
- `
|
|
275
|
-
- `ansi256`
|
|
276
|
-
|
|
254
|
+
- [`hwb`](https://en.wikipedia.org/wiki/HWB_color_model) - Example: `chalk.hwb(32, 0, 50).bold('Orange!')`
|
|
255
|
+
- [`ansi`](https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit) - Example: `chalk.ansi(31).bgAnsi(93)('red on yellowBright')`
|
|
256
|
+
- [`ansi256`](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) - Example: `chalk.bgAnsi256(194)('Honeydew, more or less')`
|
|
277
257
|
|
|
278
258
|
## Windows
|
|
279
259
|
|
|
280
|
-
If you're on Windows, do yourself a favor and use [
|
|
281
|
-
|
|
260
|
+
If you're on Windows, do yourself a favor and use [Windows Terminal](https://github.com/microsoft/terminal) instead of `cmd.exe`.
|
|
282
261
|
|
|
283
262
|
## Origin story
|
|
284
263
|
|
|
285
264
|
[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.
|
|
286
265
|
|
|
266
|
+
## chalk for enterprise
|
|
267
|
+
|
|
268
|
+
Available as part of the Tidelift Subscription.
|
|
269
|
+
|
|
270
|
+
The maintainers of chalk and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-chalk?utm_source=npm-chalk&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|
|
287
271
|
|
|
288
272
|
## Related
|
|
289
273
|
|
|
@@ -302,13 +286,7 @@ If you're on Windows, do yourself a favor and use [`cmder`](http://cmder.net/) i
|
|
|
302
286
|
- [chalk-pipe](https://github.com/LitoMore/chalk-pipe) - Create chalk style schemes with simpler style strings
|
|
303
287
|
- [terminal-link](https://github.com/sindresorhus/terminal-link) - Create clickable links in the terminal
|
|
304
288
|
|
|
305
|
-
|
|
306
289
|
## Maintainers
|
|
307
290
|
|
|
308
291
|
- [Sindre Sorhus](https://github.com/sindresorhus)
|
|
309
292
|
- [Josh Junon](https://github.com/qix-)
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
## License
|
|
313
|
-
|
|
314
|
-
MIT
|
package/source/index.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const ansiStyles = require('ansi-styles');
|
|
3
|
+
const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color');
|
|
4
|
+
const {
|
|
5
|
+
stringReplaceAll,
|
|
6
|
+
stringEncaseCRLFWithFirstIndex
|
|
7
|
+
} = require('./util');
|
|
8
|
+
|
|
9
|
+
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
|
10
|
+
const levelMapping = [
|
|
11
|
+
'ansi',
|
|
12
|
+
'ansi',
|
|
13
|
+
'ansi256',
|
|
14
|
+
'ansi16m'
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const styles = Object.create(null);
|
|
18
|
+
|
|
19
|
+
const applyOptions = (object, options = {}) => {
|
|
20
|
+
if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
|
|
21
|
+
throw new Error('The `level` option should be an integer from 0 to 3');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Detect level if not set manually
|
|
25
|
+
const colorLevel = stdoutColor ? stdoutColor.level : 0;
|
|
26
|
+
object.level = options.level === undefined ? colorLevel : options.level;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
class ChalkClass {
|
|
30
|
+
constructor(options) {
|
|
31
|
+
// eslint-disable-next-line no-constructor-return
|
|
32
|
+
return chalkFactory(options);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const chalkFactory = options => {
|
|
37
|
+
const chalk = {};
|
|
38
|
+
applyOptions(chalk, options);
|
|
39
|
+
|
|
40
|
+
chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_);
|
|
41
|
+
|
|
42
|
+
Object.setPrototypeOf(chalk, Chalk.prototype);
|
|
43
|
+
Object.setPrototypeOf(chalk.template, chalk);
|
|
44
|
+
|
|
45
|
+
chalk.template.constructor = () => {
|
|
46
|
+
throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.');
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
chalk.template.Instance = ChalkClass;
|
|
50
|
+
|
|
51
|
+
return chalk.template;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function Chalk(options) {
|
|
55
|
+
return chalkFactory(options);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (const [styleName, style] of Object.entries(ansiStyles)) {
|
|
59
|
+
styles[styleName] = {
|
|
60
|
+
get() {
|
|
61
|
+
const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty);
|
|
62
|
+
Object.defineProperty(this, styleName, {value: builder});
|
|
63
|
+
return builder;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
styles.visible = {
|
|
69
|
+
get() {
|
|
70
|
+
const builder = createBuilder(this, this._styler, true);
|
|
71
|
+
Object.defineProperty(this, 'visible', {value: builder});
|
|
72
|
+
return builder;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const usedModels = ['rgb', 'hex', 'keyword', 'hsl', 'hsv', 'hwb', 'ansi', 'ansi256'];
|
|
77
|
+
|
|
78
|
+
for (const model of usedModels) {
|
|
79
|
+
styles[model] = {
|
|
80
|
+
get() {
|
|
81
|
+
const {level} = this;
|
|
82
|
+
return function (...arguments_) {
|
|
83
|
+
const styler = createStyler(ansiStyles.color[levelMapping[level]][model](...arguments_), ansiStyles.color.close, this._styler);
|
|
84
|
+
return createBuilder(this, styler, this._isEmpty);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
for (const model of usedModels) {
|
|
91
|
+
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
|
|
92
|
+
styles[bgModel] = {
|
|
93
|
+
get() {
|
|
94
|
+
const {level} = this;
|
|
95
|
+
return function (...arguments_) {
|
|
96
|
+
const styler = createStyler(ansiStyles.bgColor[levelMapping[level]][model](...arguments_), ansiStyles.bgColor.close, this._styler);
|
|
97
|
+
return createBuilder(this, styler, this._isEmpty);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const proto = Object.defineProperties(() => {}, {
|
|
104
|
+
...styles,
|
|
105
|
+
level: {
|
|
106
|
+
enumerable: true,
|
|
107
|
+
get() {
|
|
108
|
+
return this._generator.level;
|
|
109
|
+
},
|
|
110
|
+
set(level) {
|
|
111
|
+
this._generator.level = level;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const createStyler = (open, close, parent) => {
|
|
117
|
+
let openAll;
|
|
118
|
+
let closeAll;
|
|
119
|
+
if (parent === undefined) {
|
|
120
|
+
openAll = open;
|
|
121
|
+
closeAll = close;
|
|
122
|
+
} else {
|
|
123
|
+
openAll = parent.openAll + open;
|
|
124
|
+
closeAll = close + parent.closeAll;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
open,
|
|
129
|
+
close,
|
|
130
|
+
openAll,
|
|
131
|
+
closeAll,
|
|
132
|
+
parent
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const createBuilder = (self, _styler, _isEmpty) => {
|
|
137
|
+
const builder = (...arguments_) => {
|
|
138
|
+
// Single argument is hot path, implicit coercion is faster than anything
|
|
139
|
+
// eslint-disable-next-line no-implicit-coercion
|
|
140
|
+
return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// We alter the prototype because we must return a function, but there is
|
|
144
|
+
// no way to create a function with a different prototype
|
|
145
|
+
Object.setPrototypeOf(builder, proto);
|
|
146
|
+
|
|
147
|
+
builder._generator = self;
|
|
148
|
+
builder._styler = _styler;
|
|
149
|
+
builder._isEmpty = _isEmpty;
|
|
150
|
+
|
|
151
|
+
return builder;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const applyStyle = (self, string) => {
|
|
155
|
+
if (self.level <= 0 || !string) {
|
|
156
|
+
return self._isEmpty ? '' : string;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let styler = self._styler;
|
|
160
|
+
|
|
161
|
+
if (styler === undefined) {
|
|
162
|
+
return string;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const {openAll, closeAll} = styler;
|
|
166
|
+
if (string.indexOf('\u001B') !== -1) {
|
|
167
|
+
while (styler !== undefined) {
|
|
168
|
+
// Replace any instances already present with a re-opening code
|
|
169
|
+
// otherwise only the part of the string until said closing code
|
|
170
|
+
// will be colored, and the rest will simply be 'plain'.
|
|
171
|
+
string = stringReplaceAll(string, styler.close, styler.open);
|
|
172
|
+
|
|
173
|
+
styler = styler.parent;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// We can move both next actions out of loop, because remaining actions in loop won't have
|
|
178
|
+
// any/visible effect on parts we add here. Close the styling before a linebreak and reopen
|
|
179
|
+
// after next line to fix a bleed issue on macOS: https://github.com/chalk/chalk/pull/92
|
|
180
|
+
const lfIndex = string.indexOf('\n');
|
|
181
|
+
if (lfIndex !== -1) {
|
|
182
|
+
string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return openAll + string + closeAll;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
let template;
|
|
189
|
+
const chalkTag = (chalk, ...strings) => {
|
|
190
|
+
const [firstString] = strings;
|
|
191
|
+
|
|
192
|
+
if (!Array.isArray(firstString)) {
|
|
193
|
+
// If chalk() was called by itself or with a string,
|
|
194
|
+
// return the string itself as a string.
|
|
195
|
+
return strings.join(' ');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const arguments_ = strings.slice(1);
|
|
199
|
+
const parts = [firstString.raw[0]];
|
|
200
|
+
|
|
201
|
+
for (let i = 1; i < firstString.length; i++) {
|
|
202
|
+
parts.push(
|
|
203
|
+
String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
|
|
204
|
+
String(firstString.raw[i])
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (template === undefined) {
|
|
209
|
+
template = require('./templates');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return template(chalk, parts.join(''));
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
Object.defineProperties(Chalk.prototype, styles);
|
|
216
|
+
|
|
217
|
+
const chalk = Chalk(); // eslint-disable-line new-cap
|
|
218
|
+
chalk.supportsColor = stdoutColor;
|
|
219
|
+
chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
|
|
220
|
+
chalk.stderr.supportsColor = stderrColor;
|
|
221
|
+
|
|
222
|
+
module.exports = chalk;
|
|
@@ -1,8 +1,8 @@
|
|
|
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;
|
|
2
|
+
const TEMPLATE_REGEX = /(?:\\(u(?:[a-f\d]{4}|\{[a-f\d]{1,6}\})|x[a-f\d]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi;
|
|
3
3
|
const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
|
|
4
4
|
const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
|
|
5
|
-
const ESCAPE_REGEX = /\\(u[a-f\d]{4}|x[a-f\d]{2}|.)|([^\\])/gi;
|
|
5
|
+
const ESCAPE_REGEX = /\\(u(?:[a-f\d]{4}|{[a-f\d]{1,6}})|x[a-f\d]{2}|.)|([^\\])/gi;
|
|
6
6
|
|
|
7
7
|
const ESCAPES = new Map([
|
|
8
8
|
['n', '\n'],
|
|
@@ -18,23 +18,31 @@ const ESCAPES = new Map([
|
|
|
18
18
|
]);
|
|
19
19
|
|
|
20
20
|
function unescape(c) {
|
|
21
|
-
|
|
21
|
+
const u = c[0] === 'u';
|
|
22
|
+
const bracket = c[1] === '{';
|
|
23
|
+
|
|
24
|
+
if ((u && !bracket && c.length === 5) || (c[0] === 'x' && c.length === 3)) {
|
|
22
25
|
return String.fromCharCode(parseInt(c.slice(1), 16));
|
|
23
26
|
}
|
|
24
27
|
|
|
28
|
+
if (u && bracket) {
|
|
29
|
+
return String.fromCodePoint(parseInt(c.slice(2, -1), 16));
|
|
30
|
+
}
|
|
31
|
+
|
|
25
32
|
return ESCAPES.get(c) || c;
|
|
26
33
|
}
|
|
27
34
|
|
|
28
|
-
function parseArguments(name,
|
|
35
|
+
function parseArguments(name, arguments_) {
|
|
29
36
|
const results = [];
|
|
30
|
-
const chunks =
|
|
37
|
+
const chunks = arguments_.trim().split(/\s*,\s*/g);
|
|
31
38
|
let matches;
|
|
32
39
|
|
|
33
40
|
for (const chunk of chunks) {
|
|
34
|
-
|
|
35
|
-
|
|
41
|
+
const number = Number(chunk);
|
|
42
|
+
if (!Number.isNaN(number)) {
|
|
43
|
+
results.push(number);
|
|
36
44
|
} else if ((matches = chunk.match(STRING_REGEX))) {
|
|
37
|
-
results.push(matches[2].replace(ESCAPE_REGEX, (m, escape,
|
|
45
|
+
results.push(matches[2].replace(ESCAPE_REGEX, (m, escape, character) => escape ? unescape(escape) : character));
|
|
38
46
|
} else {
|
|
39
47
|
throw new Error(`Invalid Chalk template style argument: ${chunk} (in style '${name}')`);
|
|
40
48
|
}
|
|
@@ -73,36 +81,34 @@ function buildStyle(chalk, styles) {
|
|
|
73
81
|
}
|
|
74
82
|
|
|
75
83
|
let current = chalk;
|
|
76
|
-
for (const styleName of Object.
|
|
77
|
-
if (Array.isArray(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
84
|
+
for (const [styleName, styles] of Object.entries(enabled)) {
|
|
85
|
+
if (!Array.isArray(styles)) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
81
88
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
} else {
|
|
85
|
-
current = current[styleName];
|
|
86
|
-
}
|
|
89
|
+
if (!(styleName in current)) {
|
|
90
|
+
throw new Error(`Unknown Chalk style: ${styleName}`);
|
|
87
91
|
}
|
|
92
|
+
|
|
93
|
+
current = styles.length > 0 ? current[styleName](...styles) : current[styleName];
|
|
88
94
|
}
|
|
89
95
|
|
|
90
96
|
return current;
|
|
91
97
|
}
|
|
92
98
|
|
|
93
|
-
module.exports = (chalk,
|
|
99
|
+
module.exports = (chalk, temporary) => {
|
|
94
100
|
const styles = [];
|
|
95
101
|
const chunks = [];
|
|
96
102
|
let chunk = [];
|
|
97
103
|
|
|
98
104
|
// eslint-disable-next-line max-params
|
|
99
|
-
|
|
100
|
-
if (
|
|
101
|
-
chunk.push(unescape(
|
|
105
|
+
temporary.replace(TEMPLATE_REGEX, (m, escapeCharacter, inverse, style, close, character) => {
|
|
106
|
+
if (escapeCharacter) {
|
|
107
|
+
chunk.push(unescape(escapeCharacter));
|
|
102
108
|
} else if (style) {
|
|
103
|
-
const
|
|
109
|
+
const string = chunk.join('');
|
|
104
110
|
chunk = [];
|
|
105
|
-
chunks.push(styles.length === 0 ?
|
|
111
|
+
chunks.push(styles.length === 0 ? string : buildStyle(chalk, styles)(string));
|
|
106
112
|
styles.push({inverse, styles: parseStyle(style)});
|
|
107
113
|
} else if (close) {
|
|
108
114
|
if (styles.length === 0) {
|
|
@@ -113,15 +119,15 @@ module.exports = (chalk, tmp) => {
|
|
|
113
119
|
chunk = [];
|
|
114
120
|
styles.pop();
|
|
115
121
|
} else {
|
|
116
|
-
chunk.push(
|
|
122
|
+
chunk.push(character);
|
|
117
123
|
}
|
|
118
124
|
});
|
|
119
125
|
|
|
120
126
|
chunks.push(chunk.join(''));
|
|
121
127
|
|
|
122
128
|
if (styles.length > 0) {
|
|
123
|
-
const
|
|
124
|
-
throw new Error(
|
|
129
|
+
const errMessage = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
|
|
130
|
+
throw new Error(errMessage);
|
|
125
131
|
}
|
|
126
132
|
|
|
127
133
|
return chunks.join('');
|
package/source/util.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const stringReplaceAll = (string, substring, replacer) => {
|
|
4
|
+
let index = string.indexOf(substring);
|
|
5
|
+
if (index === -1) {
|
|
6
|
+
return string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const substringLength = substring.length;
|
|
10
|
+
let endIndex = 0;
|
|
11
|
+
let returnValue = '';
|
|
12
|
+
do {
|
|
13
|
+
returnValue += string.substr(endIndex, index - endIndex) + substring + replacer;
|
|
14
|
+
endIndex = index + substringLength;
|
|
15
|
+
index = string.indexOf(substring, endIndex);
|
|
16
|
+
} while (index !== -1);
|
|
17
|
+
|
|
18
|
+
returnValue += string.substr(endIndex);
|
|
19
|
+
return returnValue;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const stringEncaseCRLFWithFirstIndex = (string, prefix, postfix, index) => {
|
|
23
|
+
let endIndex = 0;
|
|
24
|
+
let returnValue = '';
|
|
25
|
+
do {
|
|
26
|
+
const gotCR = string[index - 1] === '\r';
|
|
27
|
+
returnValue += string.substr(endIndex, (gotCR ? index - 1 : index) - endIndex) + prefix + (gotCR ? '\r\n' : '\n') + postfix;
|
|
28
|
+
endIndex = index + 1;
|
|
29
|
+
index = string.indexOf('\n', endIndex);
|
|
30
|
+
} while (index !== -1);
|
|
31
|
+
|
|
32
|
+
returnValue += string.substr(endIndex);
|
|
33
|
+
return returnValue;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
module.exports = {
|
|
37
|
+
stringReplaceAll,
|
|
38
|
+
stringEncaseCRLFWithFirstIndex
|
|
39
|
+
};
|