cucen 1.6.2

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/package.json ADDED
@@ -0,0 +1,83 @@
1
+ {
2
+ "name": "cucen",
3
+ "version": "1.6.2",
4
+ "description": "Terminal string styling done right",
5
+ "license": "MIT",
6
+ "repository": "cucen/cucen",
7
+ "funding": "https://npmjs.com/cucen/cucen?sponsor=1",
8
+ "type": "module",
9
+ "main": "./source/index.js",
10
+ "exports": "./source/index.js",
11
+ "imports": {
12
+ "#ansi-styles": "./source/vendor/ansi-styles/index.js",
13
+ "#supports-color": {
14
+ "node": "./source/vendor/supports-color/index.js",
15
+ "default": "./source/vendor/supports-color/browser.js"
16
+ }
17
+ },
18
+ "types": "./source/index.d.ts",
19
+ "sideEffects": false,
20
+ "engines": {
21
+ "node": "^12.17.0 || ^14.13 || >=16.0.0"
22
+ },
23
+ "scripts": {
24
+ "test": "xo && c8 ava && tsd",
25
+ "bench": "matcha benchmark.js"
26
+ },
27
+ "files": [
28
+ "source",
29
+ "!source/index.test-d.ts"
30
+ ],
31
+ "keywords": [
32
+ "color",
33
+ "colour",
34
+ "colors",
35
+ "terminal",
36
+ "console",
37
+ "cli",
38
+ "string",
39
+ "ansi",
40
+ "style",
41
+ "styles",
42
+ "tty",
43
+ "formatting",
44
+ "rgb",
45
+ "256",
46
+ "shell",
47
+ "xterm",
48
+ "log",
49
+ "logging",
50
+ "command-line",
51
+ "text"
52
+ ],
53
+ "devDependencies": {
54
+ "@types/node": "^16.11.10",
55
+ "ava": "^3.15.0",
56
+ "c8": "^7.10.0",
57
+ "color-convert": "^2.0.1",
58
+ "execa": "^6.0.0",
59
+ "log-update": "^5.0.0",
60
+ "matcha": "^0.7.0",
61
+ "tsd": "^0.19.0",
62
+ "xo": "^0.57.0",
63
+ "yoctodelay": "^2.0.0"
64
+ },
65
+ "xo": {
66
+ "rules": {
67
+ "unicorn/prefer-string-slice": "off",
68
+ "@typescript-eslint/consistent-type-imports": "off",
69
+ "@typescript-eslint/consistent-type-exports": "off",
70
+ "@typescript-eslint/consistent-type-definitions": "off",
71
+ "unicorn/expiring-todo-comments": "off"
72
+ }
73
+ },
74
+ "c8": {
75
+ "reporter": [
76
+ "text",
77
+ "lcov"
78
+ ],
79
+ "exclude": [
80
+ "source/vendor"
81
+ ]
82
+ }
83
+ }
@@ -0,0 +1,325 @@
1
+ // TODO: Make it this when TS suports 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 Cucen.
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
+ readonly level?: ColorSupportLevel;
25
+ }
26
+
27
+ /**
28
+ Return a new Cucen instance.
29
+ */
30
+ export const Cucen: new (options?: Options) => CucenInstance; // eslint-disable-line @typescript-eslint/naming-convention
31
+
32
+ export interface CucenInstance {
33
+ (...text: unknown[]): string;
34
+
35
+ /**
36
+ The color support for Cucen.
37
+
38
+ By default, color support is automatically detected based on the environment.
39
+
40
+ Levels:
41
+ - `0` - All colors disabled.
42
+ - `1` - Basic 16 colors support.
43
+ - `2` - ANSI 256 colors support.
44
+ - `3` - Truecolor 16 million colors support.
45
+ */
46
+ level: ColorSupportLevel;
47
+
48
+ /**
49
+ Use RGB values to set text color.
50
+
51
+ @example
52
+ ```
53
+ import cucen from 'cucen';
54
+
55
+ cucen.rgb(222, 173, 237);
56
+ ```
57
+ */
58
+ rgb: (red: number, green: number, blue: number) => this;
59
+
60
+ /**
61
+ Use HEX value to set text color.
62
+
63
+ @param color - Hexadecimal value representing the desired color.
64
+
65
+ @example
66
+ ```
67
+ import cucen from 'cucen';
68
+
69
+ cucen.hex('#DEADED');
70
+ ```
71
+ */
72
+ hex: (color: string) => this;
73
+
74
+ /**
75
+ Use an [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color.
76
+
77
+ @example
78
+ ```
79
+ import cucen from 'cucen';
80
+
81
+ cucen.ansi256(201);
82
+ ```
83
+ */
84
+ ansi256: (index: number) => this;
85
+
86
+ /**
87
+ Use RGB values to set background color.
88
+
89
+ @example
90
+ ```
91
+ import cucen from 'cucen';
92
+
93
+ cucen.bgRgb(222, 173, 237);
94
+ ```
95
+ */
96
+ bgRgb: (red: number, green: number, blue: number) => this;
97
+
98
+ /**
99
+ Use HEX value to set background color.
100
+
101
+ @param color - Hexadecimal value representing the desired color.
102
+
103
+ @example
104
+ ```
105
+ import cucen from 'cucen';
106
+
107
+ cucen.bgHex('#DEADED');
108
+ ```
109
+ */
110
+ bgHex: (color: string) => this;
111
+
112
+ /**
113
+ Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set background color.
114
+
115
+ @example
116
+ ```
117
+ import cucen from 'cucen';
118
+
119
+ cucen.bgAnsi256(201);
120
+ ```
121
+ */
122
+ bgAnsi256: (index: number) => this;
123
+
124
+ /**
125
+ Modifier: Reset the current style.
126
+ */
127
+ readonly reset: this;
128
+
129
+ /**
130
+ Modifier: Make the text bold.
131
+ */
132
+ readonly bold: this;
133
+
134
+ /**
135
+ Modifier: Make the text have lower opacity.
136
+ */
137
+ readonly dim: this;
138
+
139
+ /**
140
+ Modifier: Make the text italic. *(Not widely supported)*
141
+ */
142
+ readonly italic: this;
143
+
144
+ /**
145
+ Modifier: Put a horizontal line below the text. *(Not widely supported)*
146
+ */
147
+ readonly underline: this;
148
+
149
+ /**
150
+ Modifier: Put a horizontal line above the text. *(Not widely supported)*
151
+ */
152
+ readonly overline: this;
153
+
154
+ /**
155
+ Modifier: Invert background and foreground colors.
156
+ */
157
+ readonly inverse: this;
158
+
159
+ /**
160
+ Modifier: Print the text but make it invisible.
161
+ */
162
+ readonly hidden: this;
163
+
164
+ /**
165
+ Modifier: Puts a horizontal line through the center of the text. *(Not widely supported)*
166
+ */
167
+ readonly strikethrough: this;
168
+
169
+ /**
170
+ Modifier: Print the text only when Cucen has a color level above zero.
171
+
172
+ Can be useful for things that are purely cosmetic.
173
+ */
174
+ readonly visible: this;
175
+
176
+ readonly black: this;
177
+ readonly red: this;
178
+ readonly green: this;
179
+ readonly yellow: this;
180
+ readonly blue: this;
181
+ readonly magenta: this;
182
+ readonly cyan: this;
183
+ readonly white: this;
184
+
185
+ /*
186
+ Alias for `blackBright`.
187
+ */
188
+ readonly gray: this;
189
+
190
+ /*
191
+ Alias for `blackBright`.
192
+ */
193
+ readonly grey: this;
194
+
195
+ readonly blackBright: this;
196
+ readonly redBright: this;
197
+ readonly greenBright: this;
198
+ readonly yellowBright: this;
199
+ readonly blueBright: this;
200
+ readonly magentaBright: this;
201
+ readonly cyanBright: this;
202
+ readonly whiteBright: this;
203
+
204
+ readonly bgBlack: this;
205
+ readonly bgRed: this;
206
+ readonly bgGreen: this;
207
+ readonly bgYellow: this;
208
+ readonly bgBlue: this;
209
+ readonly bgMagenta: this;
210
+ readonly bgCyan: this;
211
+ readonly bgWhite: this;
212
+
213
+ /*
214
+ Alias for `bgBlackBright`.
215
+ */
216
+ readonly bgGray: this;
217
+
218
+ /*
219
+ Alias for `bgBlackBright`.
220
+ */
221
+ readonly bgGrey: this;
222
+
223
+ readonly bgBlackBright: this;
224
+ readonly bgRedBright: this;
225
+ readonly bgGreenBright: this;
226
+ readonly bgYellowBright: this;
227
+ readonly bgBlueBright: this;
228
+ readonly bgMagentaBright: this;
229
+ readonly bgCyanBright: this;
230
+ readonly bgWhiteBright: this;
231
+ }
232
+
233
+ /**
234
+ Main Cucen object that allows to chain styles together.
235
+
236
+ Call the last one as a method with a string argument.
237
+
238
+ Order doesn't matter, and later styles take precedent in case of a conflict.
239
+
240
+ This simply means that `cucen.red.yellow.green` is equivalent to `cucen.green`.
241
+ */
242
+ declare const cucen: CucenInstance;
243
+
244
+ export const supportsColor: ColorInfo;
245
+
246
+ export const cucenStderr: typeof cucen;
247
+ export const supportsColorStderr: typeof supportsColor;
248
+
249
+ export {
250
+ ModifierName, ForegroundColorName, BackgroundColorName, ColorName,
251
+ modifierNames, foregroundColorNames, backgroundColorNames, colorNames,
252
+ // } from '#ansi-styles';
253
+ } from './vendor/ansi-styles/index.js';
254
+
255
+ export {
256
+ ColorInfo,
257
+ ColorSupport,
258
+ ColorSupportLevel,
259
+ // } from '#supports-color';
260
+ } from './vendor/supports-color/index.js';
261
+
262
+ // TODO: Remove these aliases in the next major version
263
+ /**
264
+ @deprecated Use `ModifierName` instead.
265
+
266
+ Basic modifier names.
267
+ */
268
+ export type Modifiers = ModifierName;
269
+
270
+ /**
271
+ @deprecated Use `ForegroundColorName` instead.
272
+
273
+ Basic foreground color names.
274
+
275
+ [More colors here.](https://github.com/cucen/cucen/blob/main/readme.md#256-and-truecolor-color-support)
276
+ */
277
+ export type ForegroundColor = ForegroundColorName;
278
+
279
+ /**
280
+ @deprecated Use `BackgroundColorName` instead.
281
+
282
+ Basic background color names.
283
+
284
+ [More colors here.](https://github.com/cucen/cucen/blob/main/readme.md#256-and-truecolor-color-support)
285
+ */
286
+ export type BackgroundColor = BackgroundColorName;
287
+
288
+ /**
289
+ @deprecated Use `ColorName` instead.
290
+
291
+ Basic color names. The combination of foreground and background color names.
292
+
293
+ [More colors here.](https://github.com/cucen/cucen/blob/main/readme.md#256-and-truecolor-color-support)
294
+ */
295
+ export type Color = ColorName;
296
+
297
+ /**
298
+ @deprecated Use `modifierNames` instead.
299
+
300
+ Basic modifier names.
301
+ */
302
+ export const modifiers: readonly Modifiers[];
303
+
304
+ /**
305
+ @deprecated Use `foregroundColorNames` instead.
306
+
307
+ Basic foreground color names.
308
+ */
309
+ export const foregroundColors: readonly ForegroundColor[];
310
+
311
+ /**
312
+ @deprecated Use `backgroundColorNames` instead.
313
+
314
+ Basic background color names.
315
+ */
316
+ export const backgroundColors: readonly BackgroundColor[];
317
+
318
+ /**
319
+ @deprecated Use `colorNames` instead.
320
+
321
+ Basic color names. The combination of foreground and background color names.
322
+ */
323
+ export const colors: readonly Color[];
324
+
325
+ export default cucen;
@@ -0,0 +1,225 @@
1
+ import ansiStyles from '#ansi-styles';
2
+ import supportsColor from '#supports-color';
3
+ import { // eslint-disable-line import/order
4
+ stringReplaceAll,
5
+ stringEncaseCRLFWithFirstIndex,
6
+ } from './utilities.js';
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
+
14
+ // `supportsColor.level` → `ansiStyles.color[name]` mapping
15
+ const levelMapping = [
16
+ 'ansi',
17
+ 'ansi',
18
+ 'ansi256',
19
+ 'ansi16m',
20
+ ];
21
+
22
+ const styles = Object.create(null);
23
+
24
+ const applyOptions = (object, options = {}) => {
25
+ if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
26
+ throw new Error('The `level` option should be an integer from 0 to 3');
27
+ }
28
+
29
+ // Detect level if not set manually
30
+ const colorLevel = stdoutColor ? stdoutColor.level : 0;
31
+ object.level = options.level === undefined ? colorLevel : options.level;
32
+ };
33
+
34
+ export class Cucen {
35
+ constructor(options) {
36
+ // eslint-disable-next-line no-constructor-return
37
+ return cucenFactory(options);
38
+ }
39
+ }
40
+
41
+ const cucenFactory = options => {
42
+ const cucen = (...strings) => strings.join(' ');
43
+ applyOptions(cucen, options);
44
+
45
+ Object.setPrototypeOf(cucen, createCucen.prototype);
46
+
47
+ return cucen;
48
+ };
49
+
50
+ function createCucen(options) {
51
+ return cucenFactory(options);
52
+ }
53
+
54
+ Object.setPrototypeOf(createCucen.prototype, Function.prototype);
55
+
56
+ for (const [styleName, style] of Object.entries(ansiStyles)) {
57
+ styles[styleName] = {
58
+ get() {
59
+ const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
60
+ Object.defineProperty(this, styleName, {value: builder});
61
+ return builder;
62
+ },
63
+ };
64
+ }
65
+
66
+ styles.visible = {
67
+ get() {
68
+ const builder = createBuilder(this, this[STYLER], true);
69
+ Object.defineProperty(this, 'visible', {value: builder});
70
+ return builder;
71
+ },
72
+ };
73
+
74
+ const getModelAnsi = (model, level, type, ...arguments_) => {
75
+ if (model === 'rgb') {
76
+ if (level === 'ansi16m') {
77
+ return ansiStyles[type].ansi16m(...arguments_);
78
+ }
79
+
80
+ if (level === 'ansi256') {
81
+ return ansiStyles[type].ansi256(ansiStyles.rgbToAnsi256(...arguments_));
82
+ }
83
+
84
+ return ansiStyles[type].ansi(ansiStyles.rgbToAnsi(...arguments_));
85
+ }
86
+
87
+ if (model === 'hex') {
88
+ return getModelAnsi('rgb', level, type, ...ansiStyles.hexToRgb(...arguments_));
89
+ }
90
+
91
+ return ansiStyles[type][model](...arguments_);
92
+ };
93
+
94
+ const usedModels = ['rgb', 'hex', 'ansi256'];
95
+
96
+ for (const model of usedModels) {
97
+ styles[model] = {
98
+ get() {
99
+ const {level} = this;
100
+ return function (...arguments_) {
101
+ const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]);
102
+ return createBuilder(this, styler, this[IS_EMPTY]);
103
+ };
104
+ },
105
+ };
106
+
107
+ const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
108
+ styles[bgModel] = {
109
+ get() {
110
+ const {level} = this;
111
+ return function (...arguments_) {
112
+ const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]);
113
+ return createBuilder(this, styler, this[IS_EMPTY]);
114
+ };
115
+ },
116
+ };
117
+ }
118
+
119
+ const proto = Object.defineProperties(() => {}, {
120
+ ...styles,
121
+ level: {
122
+ enumerable: true,
123
+ get() {
124
+ return this[GENERATOR].level;
125
+ },
126
+ set(level) {
127
+ this[GENERATOR].level = level;
128
+ },
129
+ },
130
+ });
131
+
132
+ const createStyler = (open, close, parent) => {
133
+ let openAll;
134
+ let closeAll;
135
+ if (parent === undefined) {
136
+ openAll = open;
137
+ closeAll = close;
138
+ } else {
139
+ openAll = parent.openAll + open;
140
+ closeAll = close + parent.closeAll;
141
+ }
142
+
143
+ return {
144
+ open,
145
+ close,
146
+ openAll,
147
+ closeAll,
148
+ parent,
149
+ };
150
+ };
151
+
152
+ const createBuilder = (self, _styler, _isEmpty) => {
153
+ // Single argument is hot path, implicit coercion is faster than anything
154
+ // eslint-disable-next-line no-implicit-coercion
155
+ const builder = (...arguments_) => applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
156
+
157
+ // We alter the prototype because we must return a function, but there is
158
+ // no way to create a function with a different prototype
159
+ Object.setPrototypeOf(builder, proto);
160
+
161
+ builder[GENERATOR] = self;
162
+ builder[STYLER] = _styler;
163
+ builder[IS_EMPTY] = _isEmpty;
164
+
165
+ return builder;
166
+ };
167
+
168
+ const applyStyle = (self, string) => {
169
+ if (self.level <= 0 || !string) {
170
+ return self[IS_EMPTY] ? '' : string;
171
+ }
172
+
173
+ let styler = self[STYLER];
174
+
175
+ if (styler === undefined) {
176
+ return string;
177
+ }
178
+
179
+ const {openAll, closeAll} = styler;
180
+ if (string.includes('\u001B')) {
181
+ while (styler !== undefined) {
182
+ // Replace any instances already present with a re-opening code
183
+ // otherwise only the part of the string until said closing code
184
+ // will be colored, and the rest will simply be 'plain'.
185
+ string = stringReplaceAll(string, styler.close, styler.open);
186
+
187
+ styler = styler.parent;
188
+ }
189
+ }
190
+
191
+ // We can move both next actions out of loop, because remaining actions in loop won't have
192
+ // any/visible effect on parts we add here. Close the styling before a linebreak and reopen
193
+ // after next line to fix a bleed issue on macOS: https://github.com/cucen/cucen/pull/92
194
+ const lfIndex = string.indexOf('\n');
195
+ if (lfIndex !== -1) {
196
+ string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
197
+ }
198
+
199
+ return openAll + string + closeAll;
200
+ };
201
+
202
+ Object.defineProperties(createCucen.prototype, styles);
203
+
204
+ const cucen = createCucen();
205
+ export const cucenStderr = createCucen({level: stderrColor ? stderrColor.level : 0});
206
+
207
+ export {
208
+ modifierNames,
209
+ foregroundColorNames,
210
+ backgroundColorNames,
211
+ colorNames,
212
+
213
+ // TODO: Remove these aliases in the next major version
214
+ modifierNames as modifiers,
215
+ foregroundColorNames as foregroundColors,
216
+ backgroundColorNames as backgroundColors,
217
+ colorNames as colors,
218
+ } from './vendor/ansi-styles/index.js';
219
+
220
+ export {
221
+ stdoutColor as supportsColor,
222
+ stderrColor as supportsColorStderr,
223
+ };
224
+
225
+ export default cucen;
@@ -0,0 +1,33 @@
1
+ // TODO: When targeting Node.js 16, use `String.prototype.replaceAll`.
2
+ export function stringReplaceAll(string, substring, replacer) {
3
+ let index = string.indexOf(substring);
4
+ if (index === -1) {
5
+ return string;
6
+ }
7
+
8
+ const substringLength = substring.length;
9
+ let endIndex = 0;
10
+ let returnValue = '';
11
+ do {
12
+ returnValue += string.slice(endIndex, index) + substring + replacer;
13
+ endIndex = index + substringLength;
14
+ index = string.indexOf(substring, endIndex);
15
+ } while (index !== -1);
16
+
17
+ returnValue += string.slice(endIndex);
18
+ return returnValue;
19
+ }
20
+
21
+ export function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
22
+ let endIndex = 0;
23
+ let returnValue = '';
24
+ do {
25
+ const gotCR = string[index - 1] === '\r';
26
+ returnValue += string.slice(endIndex, (gotCR ? index - 1 : index)) + prefix + (gotCR ? '\r\n' : '\n') + postfix;
27
+ endIndex = index + 1;
28
+ index = string.indexOf('\n', endIndex);
29
+ } while (index !== -1);
30
+
31
+ returnValue += string.slice(endIndex);
32
+ return returnValue;
33
+ }
@@ -0,0 +1,236 @@
1
+ export interface CSPair { // eslint-disable-line @typescript-eslint/naming-convention
2
+ /**
3
+ The ANSI terminal control sequence for starting this style.
4
+ */
5
+ readonly open: string;
6
+
7
+ /**
8
+ The ANSI terminal control sequence for ending this style.
9
+ */
10
+ readonly close: string;
11
+ }
12
+
13
+ export interface ColorBase {
14
+ /**
15
+ The ANSI terminal control sequence for ending this color.
16
+ */
17
+ readonly close: string;
18
+
19
+ ansi(code: number): string;
20
+
21
+ ansi256(code: number): string;
22
+
23
+ ansi16m(red: number, green: number, blue: number): string;
24
+ }
25
+
26
+ export interface Modifier {
27
+ /**
28
+ Resets the current color chain.
29
+ */
30
+ readonly reset: CSPair;
31
+
32
+ /**
33
+ Make text bold.
34
+ */
35
+ readonly bold: CSPair;
36
+
37
+ /**
38
+ Emitting only a small amount of light.
39
+ */
40
+ readonly dim: CSPair;
41
+
42
+ /**
43
+ Make text italic. (Not widely supported)
44
+ */
45
+ readonly italic: CSPair;
46
+
47
+ /**
48
+ Make text underline. (Not widely supported)
49
+ */
50
+ readonly underline: CSPair;
51
+
52
+ /**
53
+ Make text overline.
54
+
55
+ Supported on VTE-based terminals, the GNOME terminal, mintty, and Git Bash.
56
+ */
57
+ readonly overline: CSPair;
58
+
59
+ /**
60
+ Inverse background and foreground colors.
61
+ */
62
+ readonly inverse: CSPair;
63
+
64
+ /**
65
+ Prints the text, but makes it invisible.
66
+ */
67
+ readonly hidden: CSPair;
68
+
69
+ /**
70
+ Puts a horizontal line through the center of the text. (Not widely supported)
71
+ */
72
+ readonly strikethrough: CSPair;
73
+ }
74
+
75
+ export interface ForegroundColor {
76
+ readonly black: CSPair;
77
+ readonly red: CSPair;
78
+ readonly green: CSPair;
79
+ readonly yellow: CSPair;
80
+ readonly blue: CSPair;
81
+ readonly cyan: CSPair;
82
+ readonly magenta: CSPair;
83
+ readonly white: CSPair;
84
+
85
+ /**
86
+ Alias for `blackBright`.
87
+ */
88
+ readonly gray: CSPair;
89
+
90
+ /**
91
+ Alias for `blackBright`.
92
+ */
93
+ readonly grey: CSPair;
94
+
95
+ readonly blackBright: CSPair;
96
+ readonly redBright: CSPair;
97
+ readonly greenBright: CSPair;
98
+ readonly yellowBright: CSPair;
99
+ readonly blueBright: CSPair;
100
+ readonly cyanBright: CSPair;
101
+ readonly magentaBright: CSPair;
102
+ readonly whiteBright: CSPair;
103
+ }
104
+
105
+ export interface BackgroundColor {
106
+ readonly bgBlack: CSPair;
107
+ readonly bgRed: CSPair;
108
+ readonly bgGreen: CSPair;
109
+ readonly bgYellow: CSPair;
110
+ readonly bgBlue: CSPair;
111
+ readonly bgCyan: CSPair;
112
+ readonly bgMagenta: CSPair;
113
+ readonly bgWhite: CSPair;
114
+
115
+ /**
116
+ Alias for `bgBlackBright`.
117
+ */
118
+ readonly bgGray: CSPair;
119
+
120
+ /**
121
+ Alias for `bgBlackBright`.
122
+ */
123
+ readonly bgGrey: CSPair;
124
+
125
+ readonly bgBlackBright: CSPair;
126
+ readonly bgRedBright: CSPair;
127
+ readonly bgGreenBright: CSPair;
128
+ readonly bgYellowBright: CSPair;
129
+ readonly bgBlueBright: CSPair;
130
+ readonly bgCyanBright: CSPair;
131
+ readonly bgMagentaBright: CSPair;
132
+ readonly bgWhiteBright: CSPair;
133
+ }
134
+
135
+ export interface ConvertColor {
136
+ /**
137
+ Convert from the RGB color space to the ANSI 256 color space.
138
+
139
+ @param red - (`0...255`)
140
+ @param green - (`0...255`)
141
+ @param blue - (`0...255`)
142
+ */
143
+ rgbToAnsi256(red: number, green: number, blue: number): number;
144
+
145
+ /**
146
+ Convert from the RGB HEX color space to the RGB color space.
147
+
148
+ @param hex - A hexadecimal string containing RGB data.
149
+ */
150
+ hexToRgb(hex: string): [red: number, green: number, blue: number];
151
+
152
+ /**
153
+ Convert from the RGB HEX color space to the ANSI 256 color space.
154
+
155
+ @param hex - A hexadecimal string containing RGB data.
156
+ */
157
+ hexToAnsi256(hex: string): number;
158
+
159
+ /**
160
+ Convert from the ANSI 256 color space to the ANSI 16 color space.
161
+
162
+ @param code - A number representing the ANSI 256 color.
163
+ */
164
+ ansi256ToAnsi(code: number): number;
165
+
166
+ /**
167
+ Convert from the RGB color space to the ANSI 16 color space.
168
+
169
+ @param red - (`0...255`)
170
+ @param green - (`0...255`)
171
+ @param blue - (`0...255`)
172
+ */
173
+ rgbToAnsi(red: number, green: number, blue: number): number;
174
+
175
+ /**
176
+ Convert from the RGB HEX color space to the ANSI 16 color space.
177
+
178
+ @param hex - A hexadecimal string containing RGB data.
179
+ */
180
+ hexToAnsi(hex: string): number;
181
+ }
182
+
183
+ /**
184
+ Basic modifier names.
185
+ */
186
+ export type ModifierName = keyof Modifier;
187
+
188
+ /**
189
+ Basic foreground color names.
190
+
191
+ [More colors here.](https://npmjs.com/cucen/cucen/blob/main/readme.md#256-and-truecolor-color-support)
192
+ */
193
+ export type ForegroundColorName = keyof ForegroundColor;
194
+
195
+ /**
196
+ Basic background color names.
197
+
198
+ [More colors here.](https://npmjs.com/cucen/cucen/blob/main/readme.md#256-and-truecolor-color-support)
199
+ */
200
+ export type BackgroundColorName = keyof BackgroundColor;
201
+
202
+ /**
203
+ Basic color names. The combination of foreground and background color names.
204
+
205
+ [More colors here.](https://npmjs.com/cucen/cucen/blob/main/readme.md#256-and-truecolor-color-support)
206
+ */
207
+ export type ColorName = ForegroundColorName | BackgroundColorName;
208
+
209
+ /**
210
+ Basic modifier names.
211
+ */
212
+ export const modifierNames: readonly ModifierName[];
213
+
214
+ /**
215
+ Basic foreground color names.
216
+ */
217
+ export const foregroundColorNames: readonly ForegroundColorName[];
218
+
219
+ /**
220
+ Basic background color names.
221
+ */
222
+ export const backgroundColorNames: readonly BackgroundColorName[];
223
+
224
+ /*
225
+ Basic color names. The combination of foreground and background color names.
226
+ */
227
+ export const colorNames: readonly ColorName[];
228
+
229
+ declare const ansiStyles: {
230
+ readonly modifier: Modifier;
231
+ readonly color: ColorBase & ForegroundColor;
232
+ readonly bgColor: ColorBase & BackgroundColor;
233
+ readonly codes: ReadonlyMap<number, number>;
234
+ } & ForegroundColor & BackgroundColor & Modifier & ConvertColor;
235
+
236
+ export default ansiStyles;
@@ -0,0 +1,223 @@
1
+ const ANSI_BACKGROUND_OFFSET = 10;
2
+
3
+ const wrapAnsi16 = (offset = 0) => code => `\u001B[${code + offset}m`;
4
+
5
+ const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`;
6
+
7
+ const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`;
8
+
9
+ const styles = {
10
+ modifier: {
11
+ reset: [0, 0],
12
+ // 21 isn't widely supported and 22 does the same thing
13
+ bold: [1, 22],
14
+ dim: [2, 22],
15
+ italic: [3, 23],
16
+ underline: [4, 24],
17
+ overline: [53, 55],
18
+ inverse: [7, 27],
19
+ hidden: [8, 28],
20
+ strikethrough: [9, 29],
21
+ },
22
+ color: {
23
+ black: [30, 39],
24
+ red: [31, 39],
25
+ green: [32, 39],
26
+ yellow: [33, 39],
27
+ blue: [34, 39],
28
+ magenta: [35, 39],
29
+ cyan: [36, 39],
30
+ white: [37, 39],
31
+
32
+ // Bright color
33
+ blackBright: [90, 39],
34
+ gray: [90, 39], // Alias of `blackBright`
35
+ grey: [90, 39], // Alias of `blackBright`
36
+ redBright: [91, 39],
37
+ greenBright: [92, 39],
38
+ yellowBright: [93, 39],
39
+ blueBright: [94, 39],
40
+ magentaBright: [95, 39],
41
+ cyanBright: [96, 39],
42
+ whiteBright: [97, 39],
43
+ },
44
+ bgColor: {
45
+ bgBlack: [40, 49],
46
+ bgRed: [41, 49],
47
+ bgGreen: [42, 49],
48
+ bgYellow: [43, 49],
49
+ bgBlue: [44, 49],
50
+ bgMagenta: [45, 49],
51
+ bgCyan: [46, 49],
52
+ bgWhite: [47, 49],
53
+
54
+ // Bright color
55
+ bgBlackBright: [100, 49],
56
+ bgGray: [100, 49], // Alias of `bgBlackBright`
57
+ bgGrey: [100, 49], // Alias of `bgBlackBright`
58
+ bgRedBright: [101, 49],
59
+ bgGreenBright: [102, 49],
60
+ bgYellowBright: [103, 49],
61
+ bgBlueBright: [104, 49],
62
+ bgMagentaBright: [105, 49],
63
+ bgCyanBright: [106, 49],
64
+ bgWhiteBright: [107, 49],
65
+ },
66
+ };
67
+
68
+ export const modifierNames = Object.keys(styles.modifier);
69
+ export const foregroundColorNames = Object.keys(styles.color);
70
+ export const backgroundColorNames = Object.keys(styles.bgColor);
71
+ export const colorNames = [...foregroundColorNames, ...backgroundColorNames];
72
+
73
+ function assembleStyles() {
74
+ const codes = new Map();
75
+
76
+ for (const [groupName, group] of Object.entries(styles)) {
77
+ for (const [styleName, style] of Object.entries(group)) {
78
+ styles[styleName] = {
79
+ open: `\u001B[${style[0]}m`,
80
+ close: `\u001B[${style[1]}m`,
81
+ };
82
+
83
+ group[styleName] = styles[styleName];
84
+
85
+ codes.set(style[0], style[1]);
86
+ }
87
+
88
+ Object.defineProperty(styles, groupName, {
89
+ value: group,
90
+ enumerable: false,
91
+ });
92
+ }
93
+
94
+ Object.defineProperty(styles, 'codes', {
95
+ value: codes,
96
+ enumerable: false,
97
+ });
98
+
99
+ styles.color.close = '\u001B[39m';
100
+ styles.bgColor.close = '\u001B[49m';
101
+
102
+ styles.color.ansi = wrapAnsi16();
103
+ styles.color.ansi256 = wrapAnsi256();
104
+ styles.color.ansi16m = wrapAnsi16m();
105
+ styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
106
+ styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
107
+ styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
108
+
109
+ // From https://npmjs.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
110
+ Object.defineProperties(styles, {
111
+ rgbToAnsi256: {
112
+ value(red, green, blue) {
113
+ // We use the extended greyscale palette here, with the exception of
114
+ // black and white. normal palette only has 4 greyscale shades.
115
+ if (red === green && green === blue) {
116
+ if (red < 8) {
117
+ return 16;
118
+ }
119
+
120
+ if (red > 248) {
121
+ return 231;
122
+ }
123
+
124
+ return Math.round(((red - 8) / 247) * 24) + 232;
125
+ }
126
+
127
+ return 16
128
+ + (36 * Math.round(red / 255 * 5))
129
+ + (6 * Math.round(green / 255 * 5))
130
+ + Math.round(blue / 255 * 5);
131
+ },
132
+ enumerable: false,
133
+ },
134
+ hexToRgb: {
135
+ value(hex) {
136
+ const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
137
+ if (!matches) {
138
+ return [0, 0, 0];
139
+ }
140
+
141
+ let [colorString] = matches;
142
+
143
+ if (colorString.length === 3) {
144
+ colorString = [...colorString].map(character => character + character).join('');
145
+ }
146
+
147
+ const integer = Number.parseInt(colorString, 16);
148
+
149
+ return [
150
+ /* eslint-disable no-bitwise */
151
+ (integer >> 16) & 0xFF,
152
+ (integer >> 8) & 0xFF,
153
+ integer & 0xFF,
154
+ /* eslint-enable no-bitwise */
155
+ ];
156
+ },
157
+ enumerable: false,
158
+ },
159
+ hexToAnsi256: {
160
+ value: hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
161
+ enumerable: false,
162
+ },
163
+ ansi256ToAnsi: {
164
+ value(code) {
165
+ if (code < 8) {
166
+ return 30 + code;
167
+ }
168
+
169
+ if (code < 16) {
170
+ return 90 + (code - 8);
171
+ }
172
+
173
+ let red;
174
+ let green;
175
+ let blue;
176
+
177
+ if (code >= 232) {
178
+ red = (((code - 232) * 10) + 8) / 255;
179
+ green = red;
180
+ blue = red;
181
+ } else {
182
+ code -= 16;
183
+
184
+ const remainder = code % 36;
185
+
186
+ red = Math.floor(code / 36) / 5;
187
+ green = Math.floor(remainder / 6) / 5;
188
+ blue = (remainder % 6) / 5;
189
+ }
190
+
191
+ const value = Math.max(red, green, blue) * 2;
192
+
193
+ if (value === 0) {
194
+ return 30;
195
+ }
196
+
197
+ // eslint-disable-next-line no-bitwise
198
+ let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red));
199
+
200
+ if (value === 2) {
201
+ result += 60;
202
+ }
203
+
204
+ return result;
205
+ },
206
+ enumerable: false,
207
+ },
208
+ rgbToAnsi: {
209
+ value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)),
210
+ enumerable: false,
211
+ },
212
+ hexToAnsi: {
213
+ value: hex => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)),
214
+ enumerable: false,
215
+ },
216
+ });
217
+
218
+ return styles;
219
+ }
220
+
221
+ const ansiStyles = assembleStyles();
222
+
223
+ export default ansiStyles;
@@ -0,0 +1 @@
1
+ export {default} from './index.js';
@@ -0,0 +1,34 @@
1
+ /* eslint-env browser */
2
+
3
+ const level = (() => {
4
+ if (!('navigator' in globalThis)) {
5
+ return 0;
6
+ }
7
+
8
+ if (globalThis.navigator.userAgentData) {
9
+ const brand = navigator.userAgentData.brands.find(({brand}) => brand === 'Chromium');
10
+ if (brand && brand.version > 93) {
11
+ return 3;
12
+ }
13
+ }
14
+
15
+ if (/\b(Chrome|Chromium)\//.test(globalThis.navigator.userAgent)) {
16
+ return 1;
17
+ }
18
+
19
+ return 0;
20
+ })();
21
+
22
+ const colorSupport = level !== 0 && {
23
+ level,
24
+ hasBasic: true,
25
+ has256: level >= 2,
26
+ has16m: level >= 3,
27
+ };
28
+
29
+ const supportsColor = {
30
+ stdout: colorSupport,
31
+ stderr: colorSupport,
32
+ };
33
+
34
+ export default supportsColor;
@@ -0,0 +1,55 @@
1
+ import type {WriteStream} from 'node:tty';
2
+
3
+ export type Options = {
4
+ /**
5
+ Whether `process.argv` should be sniffed for `--color` and `--no-color` flags.
6
+
7
+ @default true
8
+ */
9
+ readonly sniffFlags?: boolean;
10
+ };
11
+
12
+ /**
13
+ Levels:
14
+ - `0` - All colors disabled.
15
+ - `1` - Basic 16 colors support.
16
+ - `2` - ANSI 256 colors support.
17
+ - `3` - Truecolor 16 million colors support.
18
+ */
19
+ export type ColorSupportLevel = 0 | 1 | 2 | 3;
20
+
21
+ /**
22
+ Detect whether the terminal supports color.
23
+ */
24
+ export type ColorSupport = {
25
+ /**
26
+ The color level.
27
+ */
28
+ level: ColorSupportLevel;
29
+
30
+ /**
31
+ Whether basic 16 colors are supported.
32
+ */
33
+ hasBasic: boolean;
34
+
35
+ /**
36
+ Whether ANSI 256 colors are supported.
37
+ */
38
+ has256: boolean;
39
+
40
+ /**
41
+ Whether Truecolor 16 million colors are supported.
42
+ */
43
+ has16m: boolean;
44
+ };
45
+
46
+ export type ColorInfo = ColorSupport | false;
47
+
48
+ export function createSupportsColor(stream?: WriteStream, options?: Options): ColorInfo;
49
+
50
+ declare const supportsColor: {
51
+ stdout: ColorInfo;
52
+ stderr: ColorInfo;
53
+ };
54
+
55
+ export default supportsColor;
@@ -0,0 +1,190 @@
1
+ import process from 'node:process';
2
+ import os from 'node:os';
3
+ import tty from 'node:tty';
4
+
5
+ // From: https://npmjs.com/sindresorhus/has-flag/blob/main/index.js
6
+ /// function hasFlag(flag, argv = globalThis.Deno?.args ?? process.argv) {
7
+ function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process.argv) {
8
+ const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--');
9
+ const position = argv.indexOf(prefix + flag);
10
+ const terminatorPosition = argv.indexOf('--');
11
+ return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
12
+ }
13
+
14
+ const {env} = process;
15
+
16
+ let flagForceColor;
17
+ if (
18
+ hasFlag('no-color')
19
+ || hasFlag('no-colors')
20
+ || hasFlag('color=false')
21
+ || hasFlag('color=never')
22
+ ) {
23
+ flagForceColor = 0;
24
+ } else if (
25
+ hasFlag('color')
26
+ || hasFlag('colors')
27
+ || hasFlag('color=true')
28
+ || hasFlag('color=always')
29
+ ) {
30
+ flagForceColor = 1;
31
+ }
32
+
33
+ function envForceColor() {
34
+ if ('FORCE_COLOR' in env) {
35
+ if (env.FORCE_COLOR === 'true') {
36
+ return 1;
37
+ }
38
+
39
+ if (env.FORCE_COLOR === 'false') {
40
+ return 0;
41
+ }
42
+
43
+ return env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
44
+ }
45
+ }
46
+
47
+ function translateLevel(level) {
48
+ if (level === 0) {
49
+ return false;
50
+ }
51
+
52
+ return {
53
+ level,
54
+ hasBasic: true,
55
+ has256: level >= 2,
56
+ has16m: level >= 3,
57
+ };
58
+ }
59
+
60
+ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
61
+ const noFlagForceColor = envForceColor();
62
+ if (noFlagForceColor !== undefined) {
63
+ flagForceColor = noFlagForceColor;
64
+ }
65
+
66
+ const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
67
+
68
+ if (forceColor === 0) {
69
+ return 0;
70
+ }
71
+
72
+ if (sniffFlags) {
73
+ if (hasFlag('color=16m')
74
+ || hasFlag('color=full')
75
+ || hasFlag('color=truecolor')) {
76
+ return 3;
77
+ }
78
+
79
+ if (hasFlag('color=256')) {
80
+ return 2;
81
+ }
82
+ }
83
+
84
+ // Check for Azure DevOps pipelines.
85
+ // Has to be above the `!streamIsTTY` check.
86
+ if ('TF_BUILD' in env && 'AGENT_NAME' in env) {
87
+ return 1;
88
+ }
89
+
90
+ if (haveStream && !streamIsTTY && forceColor === undefined) {
91
+ return 0;
92
+ }
93
+
94
+ const min = forceColor || 0;
95
+
96
+ if (env.TERM === 'dumb') {
97
+ return min;
98
+ }
99
+
100
+ if (process.platform === 'win32') {
101
+ // Windows 10 build 10586 is the first Windows release that supports 256 colors.
102
+ // Windows 10 build 14931 is the first release that supports 16m/TrueColor.
103
+ const osRelease = os.release().split('.');
104
+ if (
105
+ Number(osRelease[0]) >= 10
106
+ && Number(osRelease[2]) >= 10_586
107
+ ) {
108
+ return Number(osRelease[2]) >= 14_931 ? 3 : 2;
109
+ }
110
+
111
+ return 1;
112
+ }
113
+
114
+ if ('CI' in env) {
115
+ if (['GITHUB_ACTIONS', 'GITEA_ACTIONS', 'CIRCLECI'].some(key => key in env)) {
116
+ return 3;
117
+ }
118
+
119
+ if (['TRAVIS', 'APPVEYOR', 'GITLAB_CI', 'BUILDKITE', 'DRONE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
120
+ return 1;
121
+ }
122
+
123
+ return min;
124
+ }
125
+
126
+ if ('TEAMCITY_VERSION' in env) {
127
+ return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
128
+ }
129
+
130
+ if (env.COLORTERM === 'truecolor') {
131
+ return 3;
132
+ }
133
+
134
+ if (env.TERM === 'xterm-kitty') {
135
+ return 3;
136
+ }
137
+
138
+ if (env.TERM === 'xterm-ghostty') {
139
+ return 3;
140
+ }
141
+
142
+ if (env.TERM === 'wezterm') {
143
+ return 3;
144
+ }
145
+
146
+ if ('TERM_PROGRAM' in env) {
147
+ const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
148
+
149
+ switch (env.TERM_PROGRAM) {
150
+ case 'iTerm.app': {
151
+ return version >= 3 ? 3 : 2;
152
+ }
153
+
154
+ case 'Apple_Terminal': {
155
+ return 2;
156
+ }
157
+ // No default
158
+ }
159
+ }
160
+
161
+ if (/-256(color)?$/i.test(env.TERM)) {
162
+ return 2;
163
+ }
164
+
165
+ if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
166
+ return 1;
167
+ }
168
+
169
+ if ('COLORTERM' in env) {
170
+ return 1;
171
+ }
172
+
173
+ return min;
174
+ }
175
+
176
+ export function createSupportsColor(stream, options = {}) {
177
+ const level = _supportsColor(stream, {
178
+ streamIsTTY: stream && stream.isTTY,
179
+ ...options,
180
+ });
181
+
182
+ return translateLevel(level);
183
+ }
184
+
185
+ const supportsColor = {
186
+ stdout: createSupportsColor({isTTY: tty.isatty(1)}),
187
+ stderr: createSupportsColor({isTTY: tty.isatty(2)}),
188
+ };
189
+
190
+ export default supportsColor;