chalk 4.1.2 → 5.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/license +1 -1
- package/package.json +33 -24
- package/readme.md +44 -68
- package/source/index.d.ts +318 -0
- package/source/index.js +69 -86
- package/source/{util.js → utilities.js} +7 -13
- package/source/vendor/ansi-styles/index.d.ts +190 -0
- package/source/vendor/ansi-styles/index.js +219 -0
- package/source/vendor/supports-color/browser.d.ts +1 -0
- package/source/vendor/supports-color/browser.js +17 -0
- package/source/vendor/supports-color/index.d.ts +55 -0
- package/source/vendor/supports-color/index.js +169 -0
- package/index.d.ts +0 -415
- package/source/templates.js +0 -134
package/source/index.js
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const {
|
|
1
|
+
import ansiStyles from '#ansi-styles';
|
|
2
|
+
import supportsColor from '#supports-color';
|
|
3
|
+
import { // eslint-disable-line import/order
|
|
5
4
|
stringReplaceAll,
|
|
6
|
-
stringEncaseCRLFWithFirstIndex
|
|
7
|
-
}
|
|
5
|
+
stringEncaseCRLFWithFirstIndex,
|
|
6
|
+
} from './utilities.js';
|
|
8
7
|
|
|
9
|
-
const {
|
|
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');
|
|
10
13
|
|
|
11
14
|
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
|
12
15
|
const levelMapping = [
|
|
13
16
|
'ansi',
|
|
14
17
|
'ansi',
|
|
15
18
|
'ansi256',
|
|
16
|
-
'ansi16m'
|
|
19
|
+
'ansi16m',
|
|
17
20
|
];
|
|
18
21
|
|
|
19
22
|
const styles = Object.create(null);
|
|
@@ -28,7 +31,7 @@ const applyOptions = (object, options = {}) => {
|
|
|
28
31
|
object.level = options.level === undefined ? colorLevel : options.level;
|
|
29
32
|
};
|
|
30
33
|
|
|
31
|
-
class
|
|
34
|
+
export class Chalk {
|
|
32
35
|
constructor(options) {
|
|
33
36
|
// eslint-disable-next-line no-constructor-return
|
|
34
37
|
return chalkFactory(options);
|
|
@@ -36,69 +39,80 @@ class ChalkClass {
|
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
const chalkFactory = options => {
|
|
39
|
-
const chalk =
|
|
42
|
+
const chalk = (...strings) => strings.join(' ');
|
|
40
43
|
applyOptions(chalk, options);
|
|
41
44
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
Object.setPrototypeOf(chalk, Chalk.prototype);
|
|
45
|
-
Object.setPrototypeOf(chalk.template, chalk);
|
|
46
|
-
|
|
47
|
-
chalk.template.constructor = () => {
|
|
48
|
-
throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.');
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
chalk.template.Instance = ChalkClass;
|
|
45
|
+
Object.setPrototypeOf(chalk, createChalk.prototype);
|
|
52
46
|
|
|
53
|
-
return chalk
|
|
47
|
+
return chalk;
|
|
54
48
|
};
|
|
55
49
|
|
|
56
|
-
function
|
|
50
|
+
function createChalk(options) {
|
|
57
51
|
return chalkFactory(options);
|
|
58
52
|
}
|
|
59
53
|
|
|
54
|
+
Object.setPrototypeOf(createChalk.prototype, Function.prototype);
|
|
55
|
+
|
|
60
56
|
for (const [styleName, style] of Object.entries(ansiStyles)) {
|
|
61
57
|
styles[styleName] = {
|
|
62
58
|
get() {
|
|
63
|
-
const builder = createBuilder(this, createStyler(style.open, style.close, this
|
|
59
|
+
const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
|
|
64
60
|
Object.defineProperty(this, styleName, {value: builder});
|
|
65
61
|
return builder;
|
|
66
|
-
}
|
|
62
|
+
},
|
|
67
63
|
};
|
|
68
64
|
}
|
|
69
65
|
|
|
70
66
|
styles.visible = {
|
|
71
67
|
get() {
|
|
72
|
-
const builder = createBuilder(this, this
|
|
68
|
+
const builder = createBuilder(this, this[STYLER], true);
|
|
73
69
|
Object.defineProperty(this, 'visible', {value: builder});
|
|
74
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_));
|
|
75
85
|
}
|
|
86
|
+
|
|
87
|
+
if (model === 'hex') {
|
|
88
|
+
return getModelAnsi('rgb', level, type, ...ansiStyles.hexToRgb(...arguments_));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return ansiStyles[type][model](...arguments_);
|
|
76
92
|
};
|
|
77
93
|
|
|
78
|
-
const usedModels = ['rgb', 'hex', '
|
|
94
|
+
const usedModels = ['rgb', 'hex', 'ansi256'];
|
|
79
95
|
|
|
80
96
|
for (const model of usedModels) {
|
|
81
97
|
styles[model] = {
|
|
82
98
|
get() {
|
|
83
99
|
const {level} = this;
|
|
84
100
|
return function (...arguments_) {
|
|
85
|
-
const styler = createStyler(
|
|
86
|
-
return createBuilder(this, styler, this
|
|
101
|
+
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]);
|
|
102
|
+
return createBuilder(this, styler, this[IS_EMPTY]);
|
|
87
103
|
};
|
|
88
|
-
}
|
|
104
|
+
},
|
|
89
105
|
};
|
|
90
|
-
}
|
|
91
106
|
|
|
92
|
-
for (const model of usedModels) {
|
|
93
107
|
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
|
|
94
108
|
styles[bgModel] = {
|
|
95
109
|
get() {
|
|
96
110
|
const {level} = this;
|
|
97
111
|
return function (...arguments_) {
|
|
98
|
-
const styler = createStyler(
|
|
99
|
-
return createBuilder(this, styler, this
|
|
112
|
+
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]);
|
|
113
|
+
return createBuilder(this, styler, this[IS_EMPTY]);
|
|
100
114
|
};
|
|
101
|
-
}
|
|
115
|
+
},
|
|
102
116
|
};
|
|
103
117
|
}
|
|
104
118
|
|
|
@@ -107,12 +121,12 @@ const proto = Object.defineProperties(() => {}, {
|
|
|
107
121
|
level: {
|
|
108
122
|
enumerable: true,
|
|
109
123
|
get() {
|
|
110
|
-
return this.
|
|
124
|
+
return this[GENERATOR].level;
|
|
111
125
|
},
|
|
112
126
|
set(level) {
|
|
113
|
-
this.
|
|
114
|
-
}
|
|
115
|
-
}
|
|
127
|
+
this[GENERATOR].level = level;
|
|
128
|
+
},
|
|
129
|
+
},
|
|
116
130
|
});
|
|
117
131
|
|
|
118
132
|
const createStyler = (open, close, parent) => {
|
|
@@ -131,46 +145,39 @@ const createStyler = (open, close, parent) => {
|
|
|
131
145
|
close,
|
|
132
146
|
openAll,
|
|
133
147
|
closeAll,
|
|
134
|
-
parent
|
|
148
|
+
parent,
|
|
135
149
|
};
|
|
136
150
|
};
|
|
137
151
|
|
|
138
152
|
const createBuilder = (self, _styler, _isEmpty) => {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return applyStyle(builder, chalkTag(builder, ...arguments_));
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// Single argument is hot path, implicit coercion is faster than anything
|
|
146
|
-
// eslint-disable-next-line no-implicit-coercion
|
|
147
|
-
return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
|
|
148
|
-
};
|
|
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(' '));
|
|
149
156
|
|
|
150
157
|
// We alter the prototype because we must return a function, but there is
|
|
151
158
|
// no way to create a function with a different prototype
|
|
152
159
|
Object.setPrototypeOf(builder, proto);
|
|
153
160
|
|
|
154
|
-
builder
|
|
155
|
-
builder
|
|
156
|
-
builder
|
|
161
|
+
builder[GENERATOR] = self;
|
|
162
|
+
builder[STYLER] = _styler;
|
|
163
|
+
builder[IS_EMPTY] = _isEmpty;
|
|
157
164
|
|
|
158
165
|
return builder;
|
|
159
166
|
};
|
|
160
167
|
|
|
161
168
|
const applyStyle = (self, string) => {
|
|
162
169
|
if (self.level <= 0 || !string) {
|
|
163
|
-
return self
|
|
170
|
+
return self[IS_EMPTY] ? '' : string;
|
|
164
171
|
}
|
|
165
172
|
|
|
166
|
-
let styler = self
|
|
173
|
+
let styler = self[STYLER];
|
|
167
174
|
|
|
168
175
|
if (styler === undefined) {
|
|
169
176
|
return string;
|
|
170
177
|
}
|
|
171
178
|
|
|
172
179
|
const {openAll, closeAll} = styler;
|
|
173
|
-
if (string.
|
|
180
|
+
if (string.includes('\u001B')) {
|
|
174
181
|
while (styler !== undefined) {
|
|
175
182
|
// Replace any instances already present with a re-opening code
|
|
176
183
|
// otherwise only the part of the string until said closing code
|
|
@@ -192,38 +199,14 @@ const applyStyle = (self, string) => {
|
|
|
192
199
|
return openAll + string + closeAll;
|
|
193
200
|
};
|
|
194
201
|
|
|
195
|
-
|
|
196
|
-
const chalkTag = (chalk, ...strings) => {
|
|
197
|
-
const [firstString] = strings;
|
|
198
|
-
|
|
199
|
-
if (!isArray(firstString) || !isArray(firstString.raw)) {
|
|
200
|
-
// If chalk() was called by itself or with a string,
|
|
201
|
-
// return the string itself as a string.
|
|
202
|
-
return strings.join(' ');
|
|
203
|
-
}
|
|
202
|
+
Object.defineProperties(createChalk.prototype, styles);
|
|
204
203
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
for (let i = 1; i < firstString.length; i++) {
|
|
209
|
-
parts.push(
|
|
210
|
-
String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
|
|
211
|
-
String(firstString.raw[i])
|
|
212
|
-
);
|
|
213
|
-
}
|
|
204
|
+
const chalk = createChalk();
|
|
205
|
+
export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level : 0});
|
|
214
206
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
return template(chalk, parts.join(''));
|
|
207
|
+
export {
|
|
208
|
+
stdoutColor as supportsColor,
|
|
209
|
+
stderrColor as supportsColorStderr,
|
|
220
210
|
};
|
|
221
211
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
const chalk = Chalk(); // eslint-disable-line new-cap
|
|
225
|
-
chalk.supportsColor = stdoutColor;
|
|
226
|
-
chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
|
|
227
|
-
chalk.stderr.supportsColor = stderrColor;
|
|
228
|
-
|
|
229
|
-
module.exports = chalk;
|
|
212
|
+
export default chalk;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const stringReplaceAll = (string, substring, replacer) => {
|
|
1
|
+
// TODO: When targeting Node.js 16, use `String.prototype.replaceAll`.
|
|
2
|
+
export function stringReplaceAll(string, substring, replacer) {
|
|
4
3
|
let index = string.indexOf(substring);
|
|
5
4
|
if (index === -1) {
|
|
6
5
|
return string;
|
|
@@ -15,11 +14,11 @@ const stringReplaceAll = (string, substring, replacer) => {
|
|
|
15
14
|
index = string.indexOf(substring, endIndex);
|
|
16
15
|
} while (index !== -1);
|
|
17
16
|
|
|
18
|
-
returnValue += string.
|
|
17
|
+
returnValue += string.slice(endIndex);
|
|
19
18
|
return returnValue;
|
|
20
|
-
}
|
|
19
|
+
}
|
|
21
20
|
|
|
22
|
-
|
|
21
|
+
export function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
|
|
23
22
|
let endIndex = 0;
|
|
24
23
|
let returnValue = '';
|
|
25
24
|
do {
|
|
@@ -29,11 +28,6 @@ const stringEncaseCRLFWithFirstIndex = (string, prefix, postfix, index) => {
|
|
|
29
28
|
index = string.indexOf('\n', endIndex);
|
|
30
29
|
} while (index !== -1);
|
|
31
30
|
|
|
32
|
-
returnValue += string.
|
|
31
|
+
returnValue += string.slice(endIndex);
|
|
33
32
|
return returnValue;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
module.exports = {
|
|
37
|
-
stringReplaceAll,
|
|
38
|
-
stringEncaseCRLFWithFirstIndex
|
|
39
|
-
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
declare const ansiStyles: {
|
|
184
|
+
readonly modifier: Modifier;
|
|
185
|
+
readonly color: ColorBase & ForegroundColor;
|
|
186
|
+
readonly bgColor: ColorBase & BackgroundColor;
|
|
187
|
+
readonly codes: ReadonlyMap<number, number>;
|
|
188
|
+
} & ForegroundColor & BackgroundColor & Modifier & ConvertColor;
|
|
189
|
+
|
|
190
|
+
export default ansiStyles;
|
|
@@ -0,0 +1,219 @@
|
|
|
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
|
+
function assembleStyles() {
|
|
10
|
+
const codes = new Map();
|
|
11
|
+
const styles = {
|
|
12
|
+
modifier: {
|
|
13
|
+
reset: [0, 0],
|
|
14
|
+
// 21 isn't widely supported and 22 does the same thing
|
|
15
|
+
bold: [1, 22],
|
|
16
|
+
dim: [2, 22],
|
|
17
|
+
italic: [3, 23],
|
|
18
|
+
underline: [4, 24],
|
|
19
|
+
overline: [53, 55],
|
|
20
|
+
inverse: [7, 27],
|
|
21
|
+
hidden: [8, 28],
|
|
22
|
+
strikethrough: [9, 29],
|
|
23
|
+
},
|
|
24
|
+
color: {
|
|
25
|
+
black: [30, 39],
|
|
26
|
+
red: [31, 39],
|
|
27
|
+
green: [32, 39],
|
|
28
|
+
yellow: [33, 39],
|
|
29
|
+
blue: [34, 39],
|
|
30
|
+
magenta: [35, 39],
|
|
31
|
+
cyan: [36, 39],
|
|
32
|
+
white: [37, 39],
|
|
33
|
+
|
|
34
|
+
// Bright color
|
|
35
|
+
blackBright: [90, 39],
|
|
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
|
+
bgRedBright: [101, 49],
|
|
57
|
+
bgGreenBright: [102, 49],
|
|
58
|
+
bgYellowBright: [103, 49],
|
|
59
|
+
bgBlueBright: [104, 49],
|
|
60
|
+
bgMagentaBright: [105, 49],
|
|
61
|
+
bgCyanBright: [106, 49],
|
|
62
|
+
bgWhiteBright: [107, 49],
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// Alias bright black as gray (and grey)
|
|
67
|
+
styles.color.gray = styles.color.blackBright;
|
|
68
|
+
styles.bgColor.bgGray = styles.bgColor.bgBlackBright;
|
|
69
|
+
styles.color.grey = styles.color.blackBright;
|
|
70
|
+
styles.bgColor.bgGrey = styles.bgColor.bgBlackBright;
|
|
71
|
+
|
|
72
|
+
for (const [groupName, group] of Object.entries(styles)) {
|
|
73
|
+
for (const [styleName, style] of Object.entries(group)) {
|
|
74
|
+
styles[styleName] = {
|
|
75
|
+
open: `\u001B[${style[0]}m`,
|
|
76
|
+
close: `\u001B[${style[1]}m`,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
group[styleName] = styles[styleName];
|
|
80
|
+
|
|
81
|
+
codes.set(style[0], style[1]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
Object.defineProperty(styles, groupName, {
|
|
85
|
+
value: group,
|
|
86
|
+
enumerable: false,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
Object.defineProperty(styles, 'codes', {
|
|
91
|
+
value: codes,
|
|
92
|
+
enumerable: false,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
styles.color.close = '\u001B[39m';
|
|
96
|
+
styles.bgColor.close = '\u001B[49m';
|
|
97
|
+
|
|
98
|
+
styles.color.ansi = wrapAnsi16();
|
|
99
|
+
styles.color.ansi256 = wrapAnsi256();
|
|
100
|
+
styles.color.ansi16m = wrapAnsi16m();
|
|
101
|
+
styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
|
|
102
|
+
styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
|
|
103
|
+
styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
|
|
104
|
+
|
|
105
|
+
// From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
|
|
106
|
+
Object.defineProperties(styles, {
|
|
107
|
+
rgbToAnsi256: {
|
|
108
|
+
value: (red, green, blue) => {
|
|
109
|
+
// We use the extended greyscale palette here, with the exception of
|
|
110
|
+
// black and white. normal palette only has 4 greyscale shades.
|
|
111
|
+
if (red === green && green === blue) {
|
|
112
|
+
if (red < 8) {
|
|
113
|
+
return 16;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (red > 248) {
|
|
117
|
+
return 231;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return Math.round(((red - 8) / 247) * 24) + 232;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return 16
|
|
124
|
+
+ (36 * Math.round(red / 255 * 5))
|
|
125
|
+
+ (6 * Math.round(green / 255 * 5))
|
|
126
|
+
+ Math.round(blue / 255 * 5);
|
|
127
|
+
},
|
|
128
|
+
enumerable: false,
|
|
129
|
+
},
|
|
130
|
+
hexToRgb: {
|
|
131
|
+
value: hex => {
|
|
132
|
+
const matches = /(?<colorString>[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16));
|
|
133
|
+
if (!matches) {
|
|
134
|
+
return [0, 0, 0];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let {colorString} = matches.groups;
|
|
138
|
+
|
|
139
|
+
if (colorString.length === 3) {
|
|
140
|
+
colorString = [...colorString].map(character => character + character).join('');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const integer = Number.parseInt(colorString, 16);
|
|
144
|
+
|
|
145
|
+
return [
|
|
146
|
+
/* eslint-disable no-bitwise */
|
|
147
|
+
(integer >> 16) & 0xFF,
|
|
148
|
+
(integer >> 8) & 0xFF,
|
|
149
|
+
integer & 0xFF,
|
|
150
|
+
/* eslint-enable no-bitwise */
|
|
151
|
+
];
|
|
152
|
+
},
|
|
153
|
+
enumerable: false,
|
|
154
|
+
},
|
|
155
|
+
hexToAnsi256: {
|
|
156
|
+
value: hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
|
|
157
|
+
enumerable: false,
|
|
158
|
+
},
|
|
159
|
+
ansi256ToAnsi: {
|
|
160
|
+
value: code => {
|
|
161
|
+
if (code < 8) {
|
|
162
|
+
return 30 + code;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (code < 16) {
|
|
166
|
+
return 90 + (code - 8);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
let red;
|
|
170
|
+
let green;
|
|
171
|
+
let blue;
|
|
172
|
+
|
|
173
|
+
if (code >= 232) {
|
|
174
|
+
red = (((code - 232) * 10) + 8) / 255;
|
|
175
|
+
green = red;
|
|
176
|
+
blue = red;
|
|
177
|
+
} else {
|
|
178
|
+
code -= 16;
|
|
179
|
+
|
|
180
|
+
const remainder = code % 36;
|
|
181
|
+
|
|
182
|
+
red = Math.floor(code / 36) / 5;
|
|
183
|
+
green = Math.floor(remainder / 6) / 5;
|
|
184
|
+
blue = (remainder % 6) / 5;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const value = Math.max(red, green, blue) * 2;
|
|
188
|
+
|
|
189
|
+
if (value === 0) {
|
|
190
|
+
return 30;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// eslint-disable-next-line no-bitwise
|
|
194
|
+
let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red));
|
|
195
|
+
|
|
196
|
+
if (value === 2) {
|
|
197
|
+
result += 60;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return result;
|
|
201
|
+
},
|
|
202
|
+
enumerable: false,
|
|
203
|
+
},
|
|
204
|
+
rgbToAnsi: {
|
|
205
|
+
value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)),
|
|
206
|
+
enumerable: false,
|
|
207
|
+
},
|
|
208
|
+
hexToAnsi: {
|
|
209
|
+
value: hex => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)),
|
|
210
|
+
enumerable: false,
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
return styles;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const ansiStyles = assembleStyles();
|
|
218
|
+
|
|
219
|
+
export default ansiStyles;
|