chalk 2.4.1 → 3.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 +411 -0
- package/package.json +21 -29
- package/readme.md +42 -52
- package/source/index.js +233 -0
- package/{templates.js → source/templates.js} +31 -25
- 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/source/index.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
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 > 3 || options.level < 0) {
|
|
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
|
+
return chalkFactory(options);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const chalkFactory = options => {
|
|
36
|
+
const chalk = {};
|
|
37
|
+
applyOptions(chalk, options);
|
|
38
|
+
|
|
39
|
+
chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_);
|
|
40
|
+
|
|
41
|
+
Object.setPrototypeOf(chalk, Chalk.prototype);
|
|
42
|
+
Object.setPrototypeOf(chalk.template, chalk);
|
|
43
|
+
|
|
44
|
+
chalk.template.constructor = () => {
|
|
45
|
+
throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.');
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
chalk.template.Instance = ChalkClass;
|
|
49
|
+
|
|
50
|
+
return chalk.template;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function Chalk(options) {
|
|
54
|
+
return chalkFactory(options);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const [styleName, style] of Object.entries(ansiStyles)) {
|
|
58
|
+
styles[styleName] = {
|
|
59
|
+
get() {
|
|
60
|
+
const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty);
|
|
61
|
+
Object.defineProperty(this, styleName, {value: builder});
|
|
62
|
+
return builder;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
styles.visible = {
|
|
68
|
+
get() {
|
|
69
|
+
const builder = createBuilder(this, this._styler, true);
|
|
70
|
+
Object.defineProperty(this, 'visible', {value: builder});
|
|
71
|
+
return builder;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const usedModels = ['rgb', 'hex', 'keyword', 'hsl', 'hsv', 'hwb', 'ansi', 'ansi256'];
|
|
76
|
+
|
|
77
|
+
for (const model of usedModels) {
|
|
78
|
+
styles[model] = {
|
|
79
|
+
get() {
|
|
80
|
+
const {level} = this;
|
|
81
|
+
return function (...arguments_) {
|
|
82
|
+
const styler = createStyler(ansiStyles.color[levelMapping[level]][model](...arguments_), ansiStyles.color.close, this._styler);
|
|
83
|
+
return createBuilder(this, styler, this._isEmpty);
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
for (const model of usedModels) {
|
|
90
|
+
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
|
|
91
|
+
styles[bgModel] = {
|
|
92
|
+
get() {
|
|
93
|
+
const {level} = this;
|
|
94
|
+
return function (...arguments_) {
|
|
95
|
+
const styler = createStyler(ansiStyles.bgColor[levelMapping[level]][model](...arguments_), ansiStyles.bgColor.close, this._styler);
|
|
96
|
+
return createBuilder(this, styler, this._isEmpty);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const proto = Object.defineProperties(() => {}, {
|
|
103
|
+
...styles,
|
|
104
|
+
level: {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
get() {
|
|
107
|
+
return this._generator.level;
|
|
108
|
+
},
|
|
109
|
+
set(level) {
|
|
110
|
+
this._generator.level = level;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const createStyler = (open, close, parent) => {
|
|
116
|
+
let openAll;
|
|
117
|
+
let closeAll;
|
|
118
|
+
if (parent === undefined) {
|
|
119
|
+
openAll = open;
|
|
120
|
+
closeAll = close;
|
|
121
|
+
} else {
|
|
122
|
+
openAll = parent.openAll + open;
|
|
123
|
+
closeAll = close + parent.closeAll;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
open,
|
|
128
|
+
close,
|
|
129
|
+
openAll,
|
|
130
|
+
closeAll,
|
|
131
|
+
parent
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const createBuilder = (self, _styler, _isEmpty) => {
|
|
136
|
+
const builder = (...arguments_) => {
|
|
137
|
+
// Single argument is hot path, implicit coercion is faster than anything
|
|
138
|
+
// eslint-disable-next-line no-implicit-coercion
|
|
139
|
+
return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// `__proto__` is used because we must return a function, but there is
|
|
143
|
+
// no way to create a function with a different prototype
|
|
144
|
+
builder.__proto__ = proto; // eslint-disable-line no-proto
|
|
145
|
+
|
|
146
|
+
builder._generator = self;
|
|
147
|
+
builder._styler = _styler;
|
|
148
|
+
builder._isEmpty = _isEmpty;
|
|
149
|
+
|
|
150
|
+
return builder;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const applyStyle = (self, string) => {
|
|
154
|
+
if (self.level <= 0 || !string) {
|
|
155
|
+
return self._isEmpty ? '' : string;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let styler = self._styler;
|
|
159
|
+
|
|
160
|
+
if (styler === undefined) {
|
|
161
|
+
return string;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const {openAll, closeAll} = styler;
|
|
165
|
+
if (string.indexOf('\u001B') !== -1) {
|
|
166
|
+
while (styler !== undefined) {
|
|
167
|
+
// Replace any instances already present with a re-opening code
|
|
168
|
+
// otherwise only the part of the string until said closing code
|
|
169
|
+
// will be colored, and the rest will simply be 'plain'.
|
|
170
|
+
string = stringReplaceAll(string, styler.close, styler.open);
|
|
171
|
+
|
|
172
|
+
styler = styler.parent;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// We can move both next actions out of loop, because remaining actions in loop won't have
|
|
177
|
+
// any/visible effect on parts we add here. Close the styling before a linebreak and reopen
|
|
178
|
+
// after next line to fix a bleed issue on macOS: https://github.com/chalk/chalk/pull/92
|
|
179
|
+
const lfIndex = string.indexOf('\n');
|
|
180
|
+
if (lfIndex !== -1) {
|
|
181
|
+
string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return openAll + string + closeAll;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
let template;
|
|
188
|
+
const chalkTag = (chalk, ...strings) => {
|
|
189
|
+
const [firstString] = strings;
|
|
190
|
+
|
|
191
|
+
if (!Array.isArray(firstString)) {
|
|
192
|
+
// If chalk() was called by itself or with a string,
|
|
193
|
+
// return the string itself as a string.
|
|
194
|
+
return strings.join(' ');
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const arguments_ = strings.slice(1);
|
|
198
|
+
const parts = [firstString.raw[0]];
|
|
199
|
+
|
|
200
|
+
for (let i = 1; i < firstString.length; i++) {
|
|
201
|
+
parts.push(
|
|
202
|
+
String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
|
|
203
|
+
String(firstString.raw[i])
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (template === undefined) {
|
|
208
|
+
template = require('./templates');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return template(chalk, parts.join(''));
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
Object.defineProperties(Chalk.prototype, styles);
|
|
215
|
+
|
|
216
|
+
const chalk = Chalk(); // eslint-disable-line new-cap
|
|
217
|
+
chalk.supportsColor = stdoutColor;
|
|
218
|
+
chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
|
|
219
|
+
chalk.stderr.supportsColor = stderrColor;
|
|
220
|
+
|
|
221
|
+
// For TypeScript
|
|
222
|
+
chalk.Level = {
|
|
223
|
+
None: 0,
|
|
224
|
+
Basic: 1,
|
|
225
|
+
Ansi256: 2,
|
|
226
|
+
TrueColor: 3,
|
|
227
|
+
0: 'None',
|
|
228
|
+
1: 'Basic',
|
|
229
|
+
2: 'Ansi256',
|
|
230
|
+
3: 'TrueColor'
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
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,7 +119,7 @@ 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
|
|
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
|
+
};
|
package/index.js
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const escapeStringRegexp = require('escape-string-regexp');
|
|
3
|
-
const ansiStyles = require('ansi-styles');
|
|
4
|
-
const stdoutColor = require('supports-color').stdout;
|
|
5
|
-
|
|
6
|
-
const template = require('./templates.js');
|
|
7
|
-
|
|
8
|
-
const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm');
|
|
9
|
-
|
|
10
|
-
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
|
11
|
-
const levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m'];
|
|
12
|
-
|
|
13
|
-
// `color-convert` models to exclude from the Chalk API due to conflicts and such
|
|
14
|
-
const skipModels = new Set(['gray']);
|
|
15
|
-
|
|
16
|
-
const styles = Object.create(null);
|
|
17
|
-
|
|
18
|
-
function applyOptions(obj, options) {
|
|
19
|
-
options = options || {};
|
|
20
|
-
|
|
21
|
-
// Detect level if not set manually
|
|
22
|
-
const scLevel = stdoutColor ? stdoutColor.level : 0;
|
|
23
|
-
obj.level = options.level === undefined ? scLevel : options.level;
|
|
24
|
-
obj.enabled = 'enabled' in options ? options.enabled : obj.level > 0;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function Chalk(options) {
|
|
28
|
-
// We check for this.template here since calling `chalk.constructor()`
|
|
29
|
-
// by itself will have a `this` of a previously constructed chalk object
|
|
30
|
-
if (!this || !(this instanceof Chalk) || this.template) {
|
|
31
|
-
const chalk = {};
|
|
32
|
-
applyOptions(chalk, options);
|
|
33
|
-
|
|
34
|
-
chalk.template = function () {
|
|
35
|
-
const args = [].slice.call(arguments);
|
|
36
|
-
return chalkTag.apply(null, [chalk.template].concat(args));
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
Object.setPrototypeOf(chalk, Chalk.prototype);
|
|
40
|
-
Object.setPrototypeOf(chalk.template, chalk);
|
|
41
|
-
|
|
42
|
-
chalk.template.constructor = Chalk;
|
|
43
|
-
|
|
44
|
-
return chalk.template;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
applyOptions(this, options);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Use bright blue on Windows as the normal blue color is illegible
|
|
51
|
-
if (isSimpleWindowsTerm) {
|
|
52
|
-
ansiStyles.blue.open = '\u001B[94m';
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
for (const key of Object.keys(ansiStyles)) {
|
|
56
|
-
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
|
57
|
-
|
|
58
|
-
styles[key] = {
|
|
59
|
-
get() {
|
|
60
|
-
const codes = ansiStyles[key];
|
|
61
|
-
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, key);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
styles.visible = {
|
|
67
|
-
get() {
|
|
68
|
-
return build.call(this, this._styles || [], true, 'visible');
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
|
|
73
|
-
for (const model of Object.keys(ansiStyles.color.ansi)) {
|
|
74
|
-
if (skipModels.has(model)) {
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
styles[model] = {
|
|
79
|
-
get() {
|
|
80
|
-
const level = this.level;
|
|
81
|
-
return function () {
|
|
82
|
-
const open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments);
|
|
83
|
-
const codes = {
|
|
84
|
-
open,
|
|
85
|
-
close: ansiStyles.color.close,
|
|
86
|
-
closeRe: ansiStyles.color.closeRe
|
|
87
|
-
};
|
|
88
|
-
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, model);
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g');
|
|
95
|
-
for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
|
|
96
|
-
if (skipModels.has(model)) {
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
|
|
101
|
-
styles[bgModel] = {
|
|
102
|
-
get() {
|
|
103
|
-
const level = this.level;
|
|
104
|
-
return function () {
|
|
105
|
-
const open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments);
|
|
106
|
-
const codes = {
|
|
107
|
-
open,
|
|
108
|
-
close: ansiStyles.bgColor.close,
|
|
109
|
-
closeRe: ansiStyles.bgColor.closeRe
|
|
110
|
-
};
|
|
111
|
-
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, model);
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const proto = Object.defineProperties(() => {}, styles);
|
|
118
|
-
|
|
119
|
-
function build(_styles, _empty, key) {
|
|
120
|
-
const builder = function () {
|
|
121
|
-
return applyStyle.apply(builder, arguments);
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
builder._styles = _styles;
|
|
125
|
-
builder._empty = _empty;
|
|
126
|
-
|
|
127
|
-
const self = this;
|
|
128
|
-
|
|
129
|
-
Object.defineProperty(builder, 'level', {
|
|
130
|
-
enumerable: true,
|
|
131
|
-
get() {
|
|
132
|
-
return self.level;
|
|
133
|
-
},
|
|
134
|
-
set(level) {
|
|
135
|
-
self.level = level;
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
Object.defineProperty(builder, 'enabled', {
|
|
140
|
-
enumerable: true,
|
|
141
|
-
get() {
|
|
142
|
-
return self.enabled;
|
|
143
|
-
},
|
|
144
|
-
set(enabled) {
|
|
145
|
-
self.enabled = enabled;
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
// See below for fix regarding invisible grey/dim combination on Windows
|
|
150
|
-
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
|
|
151
|
-
|
|
152
|
-
// `__proto__` is used because we must return a function, but there is
|
|
153
|
-
// no way to create a function with a different prototype
|
|
154
|
-
builder.__proto__ = proto; // eslint-disable-line no-proto
|
|
155
|
-
|
|
156
|
-
return builder;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function applyStyle() {
|
|
160
|
-
// Support varags, but simply cast to string in case there's only one arg
|
|
161
|
-
const args = arguments;
|
|
162
|
-
const argsLen = args.length;
|
|
163
|
-
let str = String(arguments[0]);
|
|
164
|
-
|
|
165
|
-
if (argsLen === 0) {
|
|
166
|
-
return '';
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
if (argsLen > 1) {
|
|
170
|
-
// Don't slice `arguments`, it prevents V8 optimizations
|
|
171
|
-
for (let a = 1; a < argsLen; a++) {
|
|
172
|
-
str += ' ' + args[a];
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (!this.enabled || this.level <= 0 || !str) {
|
|
177
|
-
return this._empty ? '' : str;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
|
|
181
|
-
// see https://github.com/chalk/chalk/issues/58
|
|
182
|
-
// If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
|
|
183
|
-
const originalDim = ansiStyles.dim.open;
|
|
184
|
-
if (isSimpleWindowsTerm && this.hasGrey) {
|
|
185
|
-
ansiStyles.dim.open = '';
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
for (const code of this._styles.slice().reverse()) {
|
|
189
|
-
// Replace any instances already present with a re-opening code
|
|
190
|
-
// otherwise only the part of the string until said closing code
|
|
191
|
-
// will be colored, and the rest will simply be 'plain'.
|
|
192
|
-
str = code.open + str.replace(code.closeRe, code.open) + code.close;
|
|
193
|
-
|
|
194
|
-
// Close the styling before a linebreak and reopen
|
|
195
|
-
// after next line to fix a bleed issue on macOS
|
|
196
|
-
// https://github.com/chalk/chalk/pull/92
|
|
197
|
-
str = str.replace(/\r?\n/g, `${code.close}$&${code.open}`);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// Reset the original `dim` if we changed it to work around the Windows dimmed gray issue
|
|
201
|
-
ansiStyles.dim.open = originalDim;
|
|
202
|
-
|
|
203
|
-
return str;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function chalkTag(chalk, strings) {
|
|
207
|
-
if (!Array.isArray(strings)) {
|
|
208
|
-
// If chalk() was called by itself or with a string,
|
|
209
|
-
// return the string itself as a string.
|
|
210
|
-
return [].slice.call(arguments, 1).join(' ');
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const args = [].slice.call(arguments, 2);
|
|
214
|
-
const parts = [strings.raw[0]];
|
|
215
|
-
|
|
216
|
-
for (let i = 1; i < strings.length; i++) {
|
|
217
|
-
parts.push(String(args[i - 1]).replace(/[{}\\]/g, '\\$&'));
|
|
218
|
-
parts.push(String(strings.raw[i]));
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return template(chalk, parts.join(''));
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
Object.defineProperties(Chalk.prototype, styles);
|
|
225
|
-
|
|
226
|
-
module.exports = Chalk(); // eslint-disable-line new-cap
|
|
227
|
-
module.exports.supportsColor = stdoutColor;
|
|
228
|
-
module.exports.default = module.exports; // For TypeScript
|
package/index.js.flow
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
type TemplateStringsArray = $ReadOnlyArray<string>;
|
|
4
|
-
|
|
5
|
-
export type Level = $Values<{
|
|
6
|
-
None: 0,
|
|
7
|
-
Basic: 1,
|
|
8
|
-
Ansi256: 2,
|
|
9
|
-
TrueColor: 3
|
|
10
|
-
}>;
|
|
11
|
-
|
|
12
|
-
export type ChalkOptions = {|
|
|
13
|
-
enabled?: boolean,
|
|
14
|
-
level?: Level
|
|
15
|
-
|};
|
|
16
|
-
|
|
17
|
-
export type ColorSupport = {|
|
|
18
|
-
level: Level,
|
|
19
|
-
hasBasic: boolean,
|
|
20
|
-
has256: boolean,
|
|
21
|
-
has16m: boolean
|
|
22
|
-
|};
|
|
23
|
-
|
|
24
|
-
export interface Chalk {
|
|
25
|
-
(...text: string[]): string,
|
|
26
|
-
(text: TemplateStringsArray, ...placeholders: string[]): string,
|
|
27
|
-
constructor(options?: ChalkOptions): Chalk,
|
|
28
|
-
enabled: boolean,
|
|
29
|
-
level: Level,
|
|
30
|
-
rgb(r: number, g: number, b: number): Chalk,
|
|
31
|
-
hsl(h: number, s: number, l: number): Chalk,
|
|
32
|
-
hsv(h: number, s: number, v: number): Chalk,
|
|
33
|
-
hwb(h: number, w: number, b: number): Chalk,
|
|
34
|
-
bgHex(color: string): Chalk,
|
|
35
|
-
bgKeyword(color: string): Chalk,
|
|
36
|
-
bgRgb(r: number, g: number, b: number): Chalk,
|
|
37
|
-
bgHsl(h: number, s: number, l: number): Chalk,
|
|
38
|
-
bgHsv(h: number, s: number, v: number): Chalk,
|
|
39
|
-
bgHwb(h: number, w: number, b: number): Chalk,
|
|
40
|
-
hex(color: string): Chalk,
|
|
41
|
-
keyword(color: string): Chalk,
|
|
42
|
-
|
|
43
|
-
+reset: Chalk,
|
|
44
|
-
+bold: Chalk,
|
|
45
|
-
+dim: Chalk,
|
|
46
|
-
+italic: Chalk,
|
|
47
|
-
+underline: Chalk,
|
|
48
|
-
+inverse: Chalk,
|
|
49
|
-
+hidden: Chalk,
|
|
50
|
-
+strikethrough: Chalk,
|
|
51
|
-
|
|
52
|
-
+visible: Chalk,
|
|
53
|
-
|
|
54
|
-
+black: Chalk,
|
|
55
|
-
+red: Chalk,
|
|
56
|
-
+green: Chalk,
|
|
57
|
-
+yellow: Chalk,
|
|
58
|
-
+blue: Chalk,
|
|
59
|
-
+magenta: Chalk,
|
|
60
|
-
+cyan: Chalk,
|
|
61
|
-
+white: Chalk,
|
|
62
|
-
+gray: Chalk,
|
|
63
|
-
+grey: Chalk,
|
|
64
|
-
+blackBright: Chalk,
|
|
65
|
-
+redBright: Chalk,
|
|
66
|
-
+greenBright: Chalk,
|
|
67
|
-
+yellowBright: Chalk,
|
|
68
|
-
+blueBright: Chalk,
|
|
69
|
-
+magentaBright: Chalk,
|
|
70
|
-
+cyanBright: Chalk,
|
|
71
|
-
+whiteBright: Chalk,
|
|
72
|
-
|
|
73
|
-
+bgBlack: Chalk,
|
|
74
|
-
+bgRed: Chalk,
|
|
75
|
-
+bgGreen: Chalk,
|
|
76
|
-
+bgYellow: Chalk,
|
|
77
|
-
+bgBlue: Chalk,
|
|
78
|
-
+bgMagenta: Chalk,
|
|
79
|
-
+bgCyan: Chalk,
|
|
80
|
-
+bgWhite: Chalk,
|
|
81
|
-
+bgBlackBright: Chalk,
|
|
82
|
-
+bgRedBright: Chalk,
|
|
83
|
-
+bgGreenBright: Chalk,
|
|
84
|
-
+bgYellowBright: Chalk,
|
|
85
|
-
+bgBlueBright: Chalk,
|
|
86
|
-
+bgMagentaBright: Chalk,
|
|
87
|
-
+bgCyanBright: Chalk,
|
|
88
|
-
+bgWhiteBrigh: Chalk,
|
|
89
|
-
|
|
90
|
-
supportsColor: ColorSupport
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
declare module.exports: Chalk;
|