@depup/chalk 6.0.0-depup.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/README.md +25 -0
- package/changes.json +5 -0
- package/license +9 -0
- package/package.json +108 -0
- package/readme.md +331 -0
- package/source/index.d.ts +437 -0
- package/source/index.js +261 -0
- package/source/utilities.js +33 -0
- package/source/vendor/ansi-styles/index.d.ts +299 -0
- package/source/vendor/ansi-styles/index.js +261 -0
- package/source/vendor/supports-color/browser.d.ts +1 -0
- package/source/vendor/supports-color/browser.js +32 -0
- package/source/vendor/supports-color/index.d.ts +55 -0
- package/source/vendor/supports-color/index.js +206 -0
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
// TODO: Make it this when TS supports that.
|
|
2
|
+
// import {ModifierName, ForegroundColor, BackgroundColor, ColorName} from '#ansi-styles';
|
|
3
|
+
// import {ColorInfo, ColorSupportLevel} from '#supports-color';
|
|
4
|
+
import {
|
|
5
|
+
ModifierName,
|
|
6
|
+
ForegroundColorName,
|
|
7
|
+
BackgroundColorName,
|
|
8
|
+
ColorName,
|
|
9
|
+
} from './vendor/ansi-styles/index.js';
|
|
10
|
+
import {ColorInfo, ColorSupportLevel} from './vendor/supports-color/index.js';
|
|
11
|
+
|
|
12
|
+
export interface Options {
|
|
13
|
+
/**
|
|
14
|
+
Specify the color support for Chalk.
|
|
15
|
+
|
|
16
|
+
By default, color support is automatically detected based on the environment.
|
|
17
|
+
|
|
18
|
+
Levels:
|
|
19
|
+
- `0` - All colors disabled.
|
|
20
|
+
- `1` - Basic 16 colors support.
|
|
21
|
+
- `2` - ANSI 256 colors support.
|
|
22
|
+
- `3` - Truecolor 16 million colors support.
|
|
23
|
+
|
|
24
|
+
Omit this option, or pass `undefined`, to have the level detected instead.
|
|
25
|
+
|
|
26
|
+
@throws If the value is neither `undefined` nor an integer from 0 to 3.
|
|
27
|
+
*/
|
|
28
|
+
readonly level?: ColorSupportLevel | undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
Return a new Chalk instance.
|
|
33
|
+
*/
|
|
34
|
+
export const Chalk: new (options?: Options) => ChalkInstance; // eslint-disable-line @typescript-eslint/naming-convention
|
|
35
|
+
|
|
36
|
+
export interface ChalkInstance {
|
|
37
|
+
(...text: unknown[]): string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
The color support for Chalk.
|
|
41
|
+
|
|
42
|
+
By default, color support is automatically detected based on the environment.
|
|
43
|
+
|
|
44
|
+
Levels:
|
|
45
|
+
- `0` - All colors disabled.
|
|
46
|
+
- `1` - Basic 16 colors support.
|
|
47
|
+
- `2` - ANSI 256 colors support.
|
|
48
|
+
- `3` - Truecolor 16 million colors support.
|
|
49
|
+
|
|
50
|
+
@throws If the assigned value is not an integer from 0 to 3.
|
|
51
|
+
*/
|
|
52
|
+
level: ColorSupportLevel;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
Use RGB values to set text color.
|
|
56
|
+
|
|
57
|
+
@example
|
|
58
|
+
```
|
|
59
|
+
import chalk from 'chalk';
|
|
60
|
+
|
|
61
|
+
chalk.rgb(222, 173, 237);
|
|
62
|
+
```
|
|
63
|
+
*/
|
|
64
|
+
rgb: (red: number, green: number, blue: number) => this;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
Use HEX value to set text color.
|
|
68
|
+
|
|
69
|
+
@param color - Hexadecimal value representing the desired color.
|
|
70
|
+
|
|
71
|
+
@example
|
|
72
|
+
```
|
|
73
|
+
import chalk from 'chalk';
|
|
74
|
+
|
|
75
|
+
chalk.hex('#DEADED');
|
|
76
|
+
```
|
|
77
|
+
*/
|
|
78
|
+
hex: (color: string) => this;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
Use an [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color.
|
|
82
|
+
|
|
83
|
+
The value is downsampled to the 16-color palette on terminals that only support basic colors (level 1), so `chalk.ansi256(196)` becomes 91 (ANSI escape for bright red).
|
|
84
|
+
|
|
85
|
+
@example
|
|
86
|
+
```
|
|
87
|
+
import chalk from 'chalk';
|
|
88
|
+
|
|
89
|
+
chalk.ansi256(201);
|
|
90
|
+
```
|
|
91
|
+
*/
|
|
92
|
+
ansi256: (index: number) => this;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
Use RGB values to set background color.
|
|
96
|
+
|
|
97
|
+
@example
|
|
98
|
+
```
|
|
99
|
+
import chalk from 'chalk';
|
|
100
|
+
|
|
101
|
+
chalk.bgRgb(222, 173, 237);
|
|
102
|
+
```
|
|
103
|
+
*/
|
|
104
|
+
bgRgb: (red: number, green: number, blue: number) => this;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
Use HEX value to set background color.
|
|
108
|
+
|
|
109
|
+
@param color - Hexadecimal value representing the desired color.
|
|
110
|
+
|
|
111
|
+
@example
|
|
112
|
+
```
|
|
113
|
+
import chalk from 'chalk';
|
|
114
|
+
|
|
115
|
+
chalk.bgHex('#DEADED');
|
|
116
|
+
```
|
|
117
|
+
*/
|
|
118
|
+
bgHex: (color: string) => this;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
Use an [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set background color.
|
|
122
|
+
|
|
123
|
+
The value is downsampled to the 16-color palette on terminals that only support basic colors (level 1), so `chalk.bgAnsi256(196)` becomes 101 (ANSI escape for bright red background).
|
|
124
|
+
|
|
125
|
+
@example
|
|
126
|
+
```
|
|
127
|
+
import chalk from 'chalk';
|
|
128
|
+
|
|
129
|
+
chalk.bgAnsi256(201);
|
|
130
|
+
```
|
|
131
|
+
*/
|
|
132
|
+
bgAnsi256: (index: number) => this;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
Use RGB values to set underline color.
|
|
136
|
+
|
|
137
|
+
The underline color is only visible when an underline style is also applied.
|
|
138
|
+
|
|
139
|
+
@example
|
|
140
|
+
```
|
|
141
|
+
import chalk from 'chalk';
|
|
142
|
+
|
|
143
|
+
chalk.underlineRgb(222, 173, 237).underlineCurly('Hello, world!');
|
|
144
|
+
```
|
|
145
|
+
*/
|
|
146
|
+
underlineRgb: (red: number, green: number, blue: number) => this;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
Use HEX value to set underline color.
|
|
150
|
+
|
|
151
|
+
The underline color is only visible when an underline style is also applied.
|
|
152
|
+
|
|
153
|
+
@param color - Hexadecimal value representing the desired color.
|
|
154
|
+
|
|
155
|
+
@example
|
|
156
|
+
```
|
|
157
|
+
import chalk from 'chalk';
|
|
158
|
+
|
|
159
|
+
chalk.underlineHex('#DEADED').underlineCurly('Hello, world!');
|
|
160
|
+
```
|
|
161
|
+
*/
|
|
162
|
+
underlineHex: (color: string) => this;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
Use an [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set underline color.
|
|
166
|
+
|
|
167
|
+
The underline color is only visible when an underline style is also applied.
|
|
168
|
+
|
|
169
|
+
The value is downsampled to the first 16 palette entries on terminals that only support basic colors (level 1), so `chalk.underlineAnsi256(196)` becomes 9 (the palette index for bright red).
|
|
170
|
+
|
|
171
|
+
@example
|
|
172
|
+
```
|
|
173
|
+
import chalk from 'chalk';
|
|
174
|
+
|
|
175
|
+
chalk.underlineAnsi256(201).underlineCurly('Hello, world!');
|
|
176
|
+
```
|
|
177
|
+
*/
|
|
178
|
+
underlineAnsi256: (index: number) => this;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
Modifier: Reset the current style.
|
|
182
|
+
*/
|
|
183
|
+
readonly reset: this;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
Modifier: Make the text bold.
|
|
187
|
+
*/
|
|
188
|
+
readonly bold: this;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
Modifier: Make the text have lower opacity.
|
|
192
|
+
*/
|
|
193
|
+
readonly dim: this;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
Modifier: Make the text italic. *(Not widely supported)*
|
|
197
|
+
*/
|
|
198
|
+
readonly italic: this;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
Modifier: Put a horizontal line below the text. *(Not widely supported)*
|
|
202
|
+
*/
|
|
203
|
+
readonly underline: this;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
Modifier: Put a double horizontal line below the text. *(Not widely supported)*
|
|
207
|
+
*/
|
|
208
|
+
readonly underlineDouble: this;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
Modifier: Put a curly horizontal line below the text. *(Not widely supported)*
|
|
212
|
+
*/
|
|
213
|
+
readonly underlineCurly: this;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
Modifier: Put a dotted horizontal line below the text. *(Not widely supported)*
|
|
217
|
+
*/
|
|
218
|
+
readonly underlineDotted: this;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
Modifier: Put a dashed horizontal line below the text. *(Not widely supported)*
|
|
222
|
+
*/
|
|
223
|
+
readonly underlineDashed: this;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
Modifier: Put a horizontal line above the text. *(Not widely supported)*
|
|
227
|
+
*/
|
|
228
|
+
readonly overline: this;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
Modifier: Invert background and foreground colors.
|
|
232
|
+
*/
|
|
233
|
+
readonly inverse: this;
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
Modifier: Print the text but make it invisible.
|
|
237
|
+
*/
|
|
238
|
+
readonly hidden: this;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
Modifier: Puts a horizontal line through the center of the text. *(Not widely supported)*
|
|
242
|
+
*/
|
|
243
|
+
readonly strikethrough: this;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
Modifier: Print the text only when Chalk has a color level above zero.
|
|
247
|
+
|
|
248
|
+
Can be useful for things that are purely cosmetic.
|
|
249
|
+
*/
|
|
250
|
+
readonly visible: this;
|
|
251
|
+
|
|
252
|
+
readonly black: this;
|
|
253
|
+
readonly red: this;
|
|
254
|
+
readonly green: this;
|
|
255
|
+
readonly yellow: this;
|
|
256
|
+
readonly blue: this;
|
|
257
|
+
readonly magenta: this;
|
|
258
|
+
readonly cyan: this;
|
|
259
|
+
readonly white: this;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
Alias for `blackBright`.
|
|
263
|
+
*/
|
|
264
|
+
readonly gray: this;
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
Alias for `blackBright`.
|
|
268
|
+
*/
|
|
269
|
+
readonly grey: this;
|
|
270
|
+
|
|
271
|
+
readonly blackBright: this;
|
|
272
|
+
readonly redBright: this;
|
|
273
|
+
readonly greenBright: this;
|
|
274
|
+
readonly yellowBright: this;
|
|
275
|
+
readonly blueBright: this;
|
|
276
|
+
readonly magentaBright: this;
|
|
277
|
+
readonly cyanBright: this;
|
|
278
|
+
readonly whiteBright: this;
|
|
279
|
+
|
|
280
|
+
readonly bgBlack: this;
|
|
281
|
+
readonly bgRed: this;
|
|
282
|
+
readonly bgGreen: this;
|
|
283
|
+
readonly bgYellow: this;
|
|
284
|
+
readonly bgBlue: this;
|
|
285
|
+
readonly bgMagenta: this;
|
|
286
|
+
readonly bgCyan: this;
|
|
287
|
+
readonly bgWhite: this;
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
Alias for `bgBlackBright`.
|
|
291
|
+
*/
|
|
292
|
+
readonly bgGray: this;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
Alias for `bgBlackBright`.
|
|
296
|
+
*/
|
|
297
|
+
readonly bgGrey: this;
|
|
298
|
+
|
|
299
|
+
readonly bgBlackBright: this;
|
|
300
|
+
readonly bgRedBright: this;
|
|
301
|
+
readonly bgGreenBright: this;
|
|
302
|
+
readonly bgYellowBright: this;
|
|
303
|
+
readonly bgBlueBright: this;
|
|
304
|
+
readonly bgMagentaBright: this;
|
|
305
|
+
readonly bgCyanBright: this;
|
|
306
|
+
readonly bgWhiteBright: this;
|
|
307
|
+
|
|
308
|
+
readonly underlineBlack: this;
|
|
309
|
+
readonly underlineRed: this;
|
|
310
|
+
readonly underlineGreen: this;
|
|
311
|
+
readonly underlineYellow: this;
|
|
312
|
+
readonly underlineBlue: this;
|
|
313
|
+
readonly underlineMagenta: this;
|
|
314
|
+
readonly underlineCyan: this;
|
|
315
|
+
readonly underlineWhite: this;
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
Alias for `underlineBlackBright`.
|
|
319
|
+
*/
|
|
320
|
+
readonly underlineGray: this;
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
Alias for `underlineBlackBright`.
|
|
324
|
+
*/
|
|
325
|
+
readonly underlineGrey: this;
|
|
326
|
+
|
|
327
|
+
readonly underlineBlackBright: this;
|
|
328
|
+
readonly underlineRedBright: this;
|
|
329
|
+
readonly underlineGreenBright: this;
|
|
330
|
+
readonly underlineYellowBright: this;
|
|
331
|
+
readonly underlineBlueBright: this;
|
|
332
|
+
readonly underlineMagentaBright: this;
|
|
333
|
+
readonly underlineCyanBright: this;
|
|
334
|
+
readonly underlineWhiteBright: this;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
Main Chalk object that allows to chain styles together.
|
|
339
|
+
|
|
340
|
+
Call the last one as a method with a string argument.
|
|
341
|
+
|
|
342
|
+
Order doesn't matter, and later styles take precedent in case of a conflict.
|
|
343
|
+
|
|
344
|
+
This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
|
|
345
|
+
*/
|
|
346
|
+
declare const chalk: ChalkInstance;
|
|
347
|
+
|
|
348
|
+
export const supportsColor: ColorInfo;
|
|
349
|
+
|
|
350
|
+
export const chalkStderr: typeof chalk;
|
|
351
|
+
export const supportsColorStderr: typeof supportsColor;
|
|
352
|
+
|
|
353
|
+
export {
|
|
354
|
+
ModifierName,
|
|
355
|
+
ForegroundColorName,
|
|
356
|
+
BackgroundColorName,
|
|
357
|
+
UnderlineColorName,
|
|
358
|
+
ColorName,
|
|
359
|
+
modifierNames,
|
|
360
|
+
foregroundColorNames,
|
|
361
|
+
backgroundColorNames,
|
|
362
|
+
underlineColorNames,
|
|
363
|
+
colorNames,
|
|
364
|
+
// } from '#ansi-styles';
|
|
365
|
+
} from './vendor/ansi-styles/index.js';
|
|
366
|
+
|
|
367
|
+
export {
|
|
368
|
+
ColorInfo,
|
|
369
|
+
ColorSupport,
|
|
370
|
+
ColorSupportLevel,
|
|
371
|
+
// } from '#supports-color';
|
|
372
|
+
} from './vendor/supports-color/index.js';
|
|
373
|
+
|
|
374
|
+
// TODO: Remove these aliases in the next major version
|
|
375
|
+
/**
|
|
376
|
+
@deprecated Use `ModifierName` instead.
|
|
377
|
+
|
|
378
|
+
Basic modifier names.
|
|
379
|
+
*/
|
|
380
|
+
export type Modifiers = ModifierName;
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
@deprecated Use `ForegroundColorName` instead.
|
|
384
|
+
|
|
385
|
+
Basic foreground color names.
|
|
386
|
+
|
|
387
|
+
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
|
388
|
+
*/
|
|
389
|
+
export type ForegroundColor = ForegroundColorName;
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
@deprecated Use `BackgroundColorName` instead.
|
|
393
|
+
|
|
394
|
+
Basic background color names.
|
|
395
|
+
|
|
396
|
+
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
|
397
|
+
*/
|
|
398
|
+
export type BackgroundColor = BackgroundColorName;
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
@deprecated Use `ColorName` instead.
|
|
402
|
+
|
|
403
|
+
Basic color names. The combination of foreground and background color names.
|
|
404
|
+
|
|
405
|
+
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
|
406
|
+
*/
|
|
407
|
+
export type Color = ColorName;
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
@deprecated Use `modifierNames` instead.
|
|
411
|
+
|
|
412
|
+
Basic modifier names.
|
|
413
|
+
*/
|
|
414
|
+
export const modifiers: readonly ModifierName[];
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
@deprecated Use `foregroundColorNames` instead.
|
|
418
|
+
|
|
419
|
+
Basic foreground color names.
|
|
420
|
+
*/
|
|
421
|
+
export const foregroundColors: readonly ForegroundColorName[];
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
@deprecated Use `backgroundColorNames` instead.
|
|
425
|
+
|
|
426
|
+
Basic background color names.
|
|
427
|
+
*/
|
|
428
|
+
export const backgroundColors: readonly BackgroundColorName[];
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
@deprecated Use `colorNames` instead.
|
|
432
|
+
|
|
433
|
+
Basic color names. The combination of foreground and background color names.
|
|
434
|
+
*/
|
|
435
|
+
export const colors: readonly ColorName[];
|
|
436
|
+
|
|
437
|
+
export default chalk;
|
package/source/index.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import {
|
|
2
|
+
stringReplaceAll,
|
|
3
|
+
stringEncaseCRLFWithFirstIndex,
|
|
4
|
+
} from './utilities.js';
|
|
5
|
+
import ansiStyles from '#ansi-styles';
|
|
6
|
+
import supportsColor from '#supports-color';
|
|
7
|
+
|
|
8
|
+
const {stdout: stdoutColor, stderr: stderrColor} = supportsColor;
|
|
9
|
+
|
|
10
|
+
const GENERATOR = Symbol('GENERATOR');
|
|
11
|
+
const STYLER = Symbol('STYLER');
|
|
12
|
+
const IS_EMPTY = Symbol('IS_EMPTY');
|
|
13
|
+
const LEVEL = Symbol('LEVEL');
|
|
14
|
+
|
|
15
|
+
const styles = Object.create(null);
|
|
16
|
+
|
|
17
|
+
const assertValidLevel = level => {
|
|
18
|
+
if (!Number.isSafeInteger(level) || level < 0 || level > 3) {
|
|
19
|
+
throw new Error('The `level` should be an integer from 0 to 3');
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// The level is stored under a symbol so the hot path can read it as a plain property, while `level` itself is an accessor that rejects values the rest of the code could not handle.
|
|
24
|
+
const levelDescriptor = {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get() {
|
|
27
|
+
return this[LEVEL];
|
|
28
|
+
},
|
|
29
|
+
set(level) {
|
|
30
|
+
assertValidLevel(level);
|
|
31
|
+
this[LEVEL] = level;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const applyOptions = (object, options = {}) => {
|
|
36
|
+
if (options.level !== undefined) {
|
|
37
|
+
assertValidLevel(options.level);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Detect level if not set manually. Written under the symbol rather than through `level`, as the prototype carrying that accessor is not installed until after this runs.
|
|
41
|
+
const colorLevel = stdoutColor ? stdoutColor.level : 0;
|
|
42
|
+
object[LEVEL] = options.level === undefined ? colorLevel : options.level;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export class Chalk {
|
|
46
|
+
constructor(options) {
|
|
47
|
+
// eslint-disable-next-line no-constructor-return
|
|
48
|
+
return chalkFactory(options);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const chalkFactory = options => {
|
|
53
|
+
const chalk = (...strings) => strings.join(' ');
|
|
54
|
+
applyOptions(chalk, options);
|
|
55
|
+
|
|
56
|
+
Object.setPrototypeOf(chalk, createChalk.prototype);
|
|
57
|
+
|
|
58
|
+
return chalk;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function createChalk(options) {
|
|
62
|
+
return chalkFactory(options);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// eslint-disable-next-line unicorn/no-top-level-side-effects -- The prototype chain must be set up at module load.
|
|
66
|
+
Object.setPrototypeOf(createChalk.prototype, Function.prototype);
|
|
67
|
+
|
|
68
|
+
for (const [styleName, style] of Object.entries(ansiStyles)) {
|
|
69
|
+
styles[styleName] = {
|
|
70
|
+
get() {
|
|
71
|
+
const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
|
|
72
|
+
Object.defineProperty(this, styleName, {value: builder});
|
|
73
|
+
return builder;
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
styles.visible = {
|
|
79
|
+
get() {
|
|
80
|
+
const builder = createBuilder(this, this[STYLER], true);
|
|
81
|
+
Object.defineProperty(this, 'visible', {value: builder});
|
|
82
|
+
return builder;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// Resolve a color model to one converter per `level`, so that a call only has to look up the converter for the current level instead of re-deciding the model and level every time.
|
|
87
|
+
const createModelConverters = (model, type) => {
|
|
88
|
+
const style = ansiStyles[type];
|
|
89
|
+
|
|
90
|
+
if (model === 'rgb') {
|
|
91
|
+
const ansi = (red, green, blue) => style.ansi(ansiStyles.rgbToAnsi(red, green, blue));
|
|
92
|
+
const ansi256 = (red, green, blue) => style.ansi256(ansiStyles.rgbToAnsi256(red, green, blue));
|
|
93
|
+
return [ansi, ansi, ansi256, style.ansi16m];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (model === 'hex') {
|
|
97
|
+
const ansi = hex => style.ansi(ansiStyles.hexToAnsi(hex));
|
|
98
|
+
const ansi256 = hex => style.ansi256(ansiStyles.hexToAnsi256(hex));
|
|
99
|
+
return [ansi, ansi, ansi256, hex => style.ansi16m(...ansiStyles.hexToRgb(hex))];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// `ansi256` is already the native form, so only the 16-color levels need converting.
|
|
103
|
+
const ansi = code => style.ansi(ansiStyles.ansi256ToAnsi(code));
|
|
104
|
+
return [ansi, ansi, style.ansi256, style.ansi256];
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const usedModels = ['rgb', 'hex', 'ansi256'];
|
|
108
|
+
|
|
109
|
+
for (const model of usedModels) {
|
|
110
|
+
const capitalizedModel = model[0].toUpperCase() + model.slice(1);
|
|
111
|
+
|
|
112
|
+
for (const [styleName, type] of [
|
|
113
|
+
[model, 'color'],
|
|
114
|
+
['bg' + capitalizedModel, 'bgColor'],
|
|
115
|
+
['underline' + capitalizedModel, 'underlineColor'],
|
|
116
|
+
]) {
|
|
117
|
+
const {close} = ansiStyles[type];
|
|
118
|
+
const converters = createModelConverters(model, type);
|
|
119
|
+
|
|
120
|
+
styles[styleName] = {
|
|
121
|
+
get() {
|
|
122
|
+
// The level is read on call rather than captured here so the function can be cached on the instance instead of being reallocated on every property access.
|
|
123
|
+
// `rgb` is the widest model, so naming the three parameters avoids a rest array.
|
|
124
|
+
const styleFunction = function (first, second, third) {
|
|
125
|
+
const open = converters[this.level](first, second, third);
|
|
126
|
+
return createBuilder(this, createStyler(open, close, this[STYLER]), this[IS_EMPTY]);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
Object.defineProperty(this, styleName, {value: styleFunction});
|
|
130
|
+
return styleFunction;
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const proto = Object.defineProperties(
|
|
137
|
+
() => {},
|
|
138
|
+
{
|
|
139
|
+
...styles,
|
|
140
|
+
level: {
|
|
141
|
+
enumerable: true,
|
|
142
|
+
get() {
|
|
143
|
+
return this[GENERATOR].level;
|
|
144
|
+
},
|
|
145
|
+
set(level) {
|
|
146
|
+
this[GENERATOR].level = level;
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
const createStyler = (open, close, parent) => {
|
|
153
|
+
let openAll;
|
|
154
|
+
let closeAll;
|
|
155
|
+
if (parent === undefined) {
|
|
156
|
+
openAll = open;
|
|
157
|
+
closeAll = close;
|
|
158
|
+
} else {
|
|
159
|
+
openAll = parent.openAll + open;
|
|
160
|
+
closeAll = close + parent.closeAll;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
open,
|
|
165
|
+
close,
|
|
166
|
+
openAll,
|
|
167
|
+
closeAll,
|
|
168
|
+
parent,
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const createBuilder = (self, _styler, _isEmpty) => {
|
|
173
|
+
// Single argument is hot path, implicit coercion is faster than anything
|
|
174
|
+
const builder = (...arguments_) => {
|
|
175
|
+
if (arguments_.length === 1) {
|
|
176
|
+
// eslint-disable-next-line no-implicit-coercion
|
|
177
|
+
return applyStyle(builder, '' + arguments_[0]);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (arguments_.length === 2) {
|
|
181
|
+
return applyStyle(builder, arguments_[0] + ' ' + arguments_[1]);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return applyStyle(builder, arguments_.join(' '));
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// We alter the prototype because we must return a function, but there is
|
|
188
|
+
// no way to create a function with a different prototype
|
|
189
|
+
Object.setPrototypeOf(builder, proto);
|
|
190
|
+
|
|
191
|
+
// Point every builder at the root generator instead of its immediate parent, so reading the level costs one property load rather than walking a `level` getter per link of the chain.
|
|
192
|
+
builder[GENERATOR] = self[GENERATOR] ?? self;
|
|
193
|
+
builder[STYLER] = _styler;
|
|
194
|
+
builder[IS_EMPTY] = _isEmpty;
|
|
195
|
+
|
|
196
|
+
return builder;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const applyStyle = (self, string) => {
|
|
200
|
+
// Read the level directly off the generator to skip the `level` getter dispatch on this hot path
|
|
201
|
+
if (self[GENERATOR][LEVEL] <= 0 || !string) {
|
|
202
|
+
// eslint-disable-next-line unicorn/no-computed-property-existence-check -- Reads the boolean value, not a property existence check.
|
|
203
|
+
return self[IS_EMPTY] ? '' : string;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
let styler = self[STYLER];
|
|
207
|
+
|
|
208
|
+
if (styler === undefined) {
|
|
209
|
+
return string;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const {openAll, closeAll} = styler;
|
|
213
|
+
if (string.includes('\u{1B}')) {
|
|
214
|
+
while (styler !== undefined) {
|
|
215
|
+
// Replace any instances already present with a re-opening code
|
|
216
|
+
// otherwise only the part of the string until said closing code
|
|
217
|
+
// will be colored, and the rest will simply be 'plain'.
|
|
218
|
+
string = stringReplaceAll(string, styler.close, styler.open);
|
|
219
|
+
|
|
220
|
+
styler = styler.parent;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// We can move both next actions out of loop, because remaining actions in loop won't have
|
|
225
|
+
// any/visible effect on parts we add here. Close the styling before a linebreak and reopen
|
|
226
|
+
// after next line to fix a bleed issue on macOS: https://github.com/chalk/chalk/pull/92
|
|
227
|
+
const lfIndex = string.indexOf('\n');
|
|
228
|
+
if (lfIndex !== -1) {
|
|
229
|
+
string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return openAll + string + closeAll;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
// `level` lives on the prototype rather than on each instance, so it costs nothing to construct an instance and matches how builders already expose it. It is inherited rather than own, so it does not show up in `Object.keys()`, same as for a builder.
|
|
236
|
+
// eslint-disable-next-line unicorn/no-top-level-side-effects -- The style getters must be installed at module load.
|
|
237
|
+
Object.defineProperties(createChalk.prototype, {...styles, level: levelDescriptor});
|
|
238
|
+
|
|
239
|
+
const chalk = createChalk();
|
|
240
|
+
export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level : 0});
|
|
241
|
+
|
|
242
|
+
export {
|
|
243
|
+
modifierNames,
|
|
244
|
+
foregroundColorNames,
|
|
245
|
+
backgroundColorNames,
|
|
246
|
+
underlineColorNames,
|
|
247
|
+
colorNames,
|
|
248
|
+
|
|
249
|
+
// TODO: Remove these aliases in the next major version
|
|
250
|
+
modifierNames as modifiers,
|
|
251
|
+
foregroundColorNames as foregroundColors,
|
|
252
|
+
backgroundColorNames as backgroundColors,
|
|
253
|
+
colorNames as colors,
|
|
254
|
+
} from './vendor/ansi-styles/index.js';
|
|
255
|
+
|
|
256
|
+
export {
|
|
257
|
+
stdoutColor as supportsColor,
|
|
258
|
+
stderrColor as supportsColorStderr,
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
export default chalk;
|