chalker 1.2.0 → 1.3.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 +23 -2
- package/lib/index.js +106 -13
- package/package.json +32 -27
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
# chalker
|
|
5
5
|
|
|
6
|
-
Set ansi colors in strings using `<>` markers and [chalk].
|
|
6
|
+
Set ansi colors in strings using `<>` markers and [chalk] or [ansi-colors].
|
|
7
7
|
|
|
8
8
|
# Usage
|
|
9
9
|
|
|
@@ -31,9 +31,17 @@ logger.log(chalker.remove(msg));
|
|
|
31
31
|
# Install
|
|
32
32
|
|
|
33
33
|
```
|
|
34
|
-
npm i --save chalker
|
|
34
|
+
npm i --save chalker chalk
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
+
or:
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
npm i --save chalker ansi-colors
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`chalker` expects either [chalk] `>= 4` or [ansi-colors] `>= 4` as a peer dependency.
|
|
44
|
+
|
|
37
45
|
# Demo
|
|
38
46
|
|
|
39
47
|
![demo][demo]
|
|
@@ -62,6 +70,9 @@ npm i --save chalker
|
|
|
62
70
|
|
|
63
71
|
#### Advanced Chalk Colors
|
|
64
72
|
|
|
73
|
+
Some advanced color markers use `chalker` compatibility wrappers when Chalk does not provide
|
|
74
|
+
the corresponding API directly.
|
|
75
|
+
|
|
65
76
|
[Chalk advanced colors] can be applied with:
|
|
66
77
|
|
|
67
78
|
| [chalk] API | chalker marker | [chalk] API | chalker marker |
|
|
@@ -105,6 +116,15 @@ chalker(str, [chalkInstance]);
|
|
|
105
116
|
|
|
106
117
|
> If `chalk.supportsColor` is `false`, then it will simply remove the `<>` markers and decode HTML entities only.
|
|
107
118
|
|
|
119
|
+
### `chalker.CHALK`
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
chalker.CHALK = require("ansi-colors");
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Set the default colors library. By default, `chalker` loads `chalk` first and falls back to
|
|
126
|
+
[ansi-colors].
|
|
127
|
+
|
|
108
128
|
### `chalker.remove`
|
|
109
129
|
|
|
110
130
|
```js
|
|
@@ -137,6 +157,7 @@ Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses
|
|
|
137
157
|
---
|
|
138
158
|
|
|
139
159
|
[demo]: ./images/demo.png
|
|
160
|
+
[ansi-colors]: https://www.npmjs.com/package/ansi-colors
|
|
140
161
|
[chalk]: https://www.npmjs.com/package/chalk
|
|
141
162
|
[chalk advanced colors]: https://github.com/chalk/chalk#256-and-truecolor-color-support
|
|
142
163
|
[travis-image]: https://travis-ci.org/jchip/chalker.svg?branch=master
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
/* eslint-disable complexity, max-statements, no-magic-numbers, prefer-template, prefer-spread */
|
|
4
4
|
|
|
5
5
|
const assert = require("assert");
|
|
6
|
-
const
|
|
6
|
+
const optionalRequire = require("optional-require")(require);
|
|
7
|
+
const colorConvert = require("color-convert");
|
|
8
|
+
const chalk = loadColors();
|
|
7
9
|
|
|
8
10
|
//
|
|
9
11
|
// convert color markers in a string to terminal/ansi color codes with chalk
|
|
@@ -62,8 +64,100 @@ const htmlEntities = {
|
|
|
62
64
|
[`®`]: "\xae"
|
|
63
65
|
};
|
|
64
66
|
|
|
67
|
+
function loadColors() {
|
|
68
|
+
const colors = optionalRequire("chalk") || optionalRequire("ansi-colors");
|
|
69
|
+
|
|
70
|
+
if (!colors) {
|
|
71
|
+
throw new Error("chalker requires either chalk or ansi-colors to be installed");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return normalizeColors(colors);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function normalizeColors(colorsModule) {
|
|
78
|
+
const colors =
|
|
79
|
+
colorsModule &&
|
|
80
|
+
colorsModule.default &&
|
|
81
|
+
(typeof colorsModule.default === "function" || typeof colorsModule.default === "object")
|
|
82
|
+
? colorsModule.default
|
|
83
|
+
: colorsModule;
|
|
84
|
+
|
|
85
|
+
return addAnsiColorsCompat(colors);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function addAnsiColorsCompat(colors) {
|
|
89
|
+
if (!colors || typeof colors.alias !== "function" || typeof colors.rgb === "function") {
|
|
90
|
+
return colors;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
colors.rgb = function (r, g, b) {
|
|
94
|
+
return this[defineAnsiColor(colors, "rgb", [r, g, b])];
|
|
95
|
+
};
|
|
96
|
+
colors.bgRgb = function (r, g, b) {
|
|
97
|
+
return this[defineAnsiColor(colors, "bgRgb", [r, g, b], true)];
|
|
98
|
+
};
|
|
99
|
+
colors.hex = function (value) {
|
|
100
|
+
return this.rgb.apply(this, colorConvert.hex.rgb(value));
|
|
101
|
+
};
|
|
102
|
+
colors.bgHex = function (value) {
|
|
103
|
+
return this.bgRgb.apply(this, colorConvert.hex.rgb(value));
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
return colors;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function defineAnsiColor(colors, name, values, bg) {
|
|
110
|
+
const styleName = `chalker_${name}_${values.join("_")}`;
|
|
111
|
+
|
|
112
|
+
if (!colors.styles[styleName]) {
|
|
113
|
+
const prefix = bg ? 48 : 38;
|
|
114
|
+
const close = bg ? 49 : 39;
|
|
115
|
+
colors.alias(
|
|
116
|
+
styleName,
|
|
117
|
+
(text) => `\u001b[${prefix};2;${values.join(";")}m${text}\u001b[${close}m`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return styleName;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function convertColorModel(name, values) {
|
|
125
|
+
const bg = name.startsWith("bg");
|
|
126
|
+
const model = bg ? name[2].toLowerCase() + name.substring(3) : name;
|
|
127
|
+
const rgb =
|
|
128
|
+
model === "keyword"
|
|
129
|
+
? colorConvert.keyword.rgb(String(values[0]).toLowerCase())
|
|
130
|
+
: colorConvert[model] && colorConvert[model].rgb.apply(colorConvert[model], values);
|
|
131
|
+
|
|
132
|
+
assert(rgb, `unknown color ${values[0]}`);
|
|
133
|
+
|
|
134
|
+
return { name: bg ? "bgRgb" : "rgb", values: rgb };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function applyChalkMethod(chalkInstance, name, values) {
|
|
138
|
+
if (typeof chalkInstance[name] === "function") {
|
|
139
|
+
return chalkInstance[name].apply(chalkInstance, values);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (
|
|
143
|
+
name === "keyword" ||
|
|
144
|
+
name === "bgKeyword" ||
|
|
145
|
+
name === "hsl" ||
|
|
146
|
+
name === "bgHsl" ||
|
|
147
|
+
name === "hsv" ||
|
|
148
|
+
name === "bgHsv" ||
|
|
149
|
+
name === "hwb" ||
|
|
150
|
+
name === "bgHwb"
|
|
151
|
+
) {
|
|
152
|
+
const converted = convertColorModel(name, values);
|
|
153
|
+
return chalkInstance[converted.name].apply(chalkInstance, converted.values);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
throw new TypeError(`${name} is not a chalk function`);
|
|
157
|
+
}
|
|
158
|
+
|
|
65
159
|
function decodeHtml(str) {
|
|
66
|
-
return str.replace(/&[\w#]+;/g, m => {
|
|
160
|
+
return str.replace(/&[\w#]+;/g, (m) => {
|
|
67
161
|
if (htmlEntities.hasOwnProperty(m)) return htmlEntities[m];
|
|
68
162
|
if (m.startsWith("&#x")) {
|
|
69
163
|
const s = m.substring(3, m.length - 1);
|
|
@@ -112,7 +206,7 @@ function applyChalkMarkers(markers, text, userChalk) {
|
|
|
112
206
|
let values = marker.substring(openIx + 1, closeIx).trim();
|
|
113
207
|
if (values.indexOf(",") >= 0) {
|
|
114
208
|
// extract rgb/hsl/hsv/hwb values like (255, 10, 20)
|
|
115
|
-
values = values.split(",").map(x => parseInt(x.trim(), 10));
|
|
209
|
+
values = values.split(",").map((x) => parseInt(x.trim(), 10));
|
|
116
210
|
|
|
117
211
|
// default no name to rgb, and bg to bgRgb
|
|
118
212
|
if (!name) name = "rgb";
|
|
@@ -127,23 +221,20 @@ function applyChalkMarkers(markers, text, userChalk) {
|
|
|
127
221
|
}
|
|
128
222
|
|
|
129
223
|
try {
|
|
130
|
-
a =
|
|
224
|
+
a = applyChalkMethod(a, name, values);
|
|
131
225
|
} catch (err) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
: `calling chalk.${name} failed with: ${err.message}`;
|
|
136
|
-
|
|
137
|
-
throw new Error(`marker ${marker} is invalid: ${msg}`);
|
|
226
|
+
throw new Error(
|
|
227
|
+
`marker ${marker} is invalid: calling chalk.${name} failed with: ${err.message}`
|
|
228
|
+
);
|
|
138
229
|
}
|
|
139
230
|
} else {
|
|
140
231
|
// if not found as a basic color, then try with chalk.keyword or chalk.bgKeyword
|
|
141
232
|
try {
|
|
142
233
|
const kw = deQuote(marker, marker);
|
|
143
234
|
if (kw.startsWith("bg-") || kw.startsWith("bg ")) {
|
|
144
|
-
a = a
|
|
235
|
+
a = applyChalkMethod(a, "bgKeyword", [kw.substring(3)]);
|
|
145
236
|
} else {
|
|
146
|
-
a = a
|
|
237
|
+
a = applyChalkMethod(a, "keyword", [kw]);
|
|
147
238
|
}
|
|
148
239
|
} catch (err) {
|
|
149
240
|
throw new Error(`marker ${marker} is not found and invalid as a keyword`);
|
|
@@ -170,7 +261,7 @@ function remove(s, keepHtml) {
|
|
|
170
261
|
}
|
|
171
262
|
|
|
172
263
|
function format(s, userChalk) {
|
|
173
|
-
userChalk = userChalk ||
|
|
264
|
+
userChalk = normalizeColors(userChalk || chalker.CHALK);
|
|
174
265
|
|
|
175
266
|
// skip applying ansi colors if chalk says color support is off
|
|
176
267
|
if (userChalk.supportsColor === false) {
|
|
@@ -251,4 +342,6 @@ chalker.remove = remove;
|
|
|
251
342
|
|
|
252
343
|
chalker.decodeHtml = decodeHtml;
|
|
253
344
|
|
|
345
|
+
chalker.CHALK = chalk;
|
|
346
|
+
|
|
254
347
|
module.exports = chalker;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chalker",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Set ansi colors in strings using XML and chalk",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -8,8 +8,17 @@
|
|
|
8
8
|
"url": "https://github.com/jchip/chalker.git"
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
|
-
"test": "
|
|
12
|
-
"
|
|
11
|
+
"test": "xrun --serial test:unit test:demo",
|
|
12
|
+
"test:unit": "vitest run",
|
|
13
|
+
"test:demo": "xrun --serial test:demo:legacy-cjs test:demo:ansi-colors-cjs test:demo:cjs-dynamic-import test:demo:esm-default test:demo:esm-namespace test:demo:esm-create-require test:demo:esm-named-export-probe",
|
|
14
|
+
"test:demo:legacy-cjs": "cd demo/legacy-cjs && fyn install && fyn test",
|
|
15
|
+
"test:demo:ansi-colors-cjs": "cd demo/ansi-colors-cjs && fyn install && fyn test",
|
|
16
|
+
"test:demo:cjs-dynamic-import": "cd demo/cjs-dynamic-import && fyn install && fyn test",
|
|
17
|
+
"test:demo:esm-default": "cd demo/esm-default && fyn install && fyn test",
|
|
18
|
+
"test:demo:esm-namespace": "cd demo/esm-namespace && fyn install && fyn test",
|
|
19
|
+
"test:demo:esm-create-require": "cd demo/esm-create-require && fyn install && fyn test",
|
|
20
|
+
"test:demo:esm-named-export-probe": "cd demo/esm-named-export-probe && fyn install && fyn test",
|
|
21
|
+
"coverage": "vitest run --coverage"
|
|
13
22
|
},
|
|
14
23
|
"files": [
|
|
15
24
|
"lib"
|
|
@@ -27,32 +36,28 @@
|
|
|
27
36
|
"author": "Joel Chen",
|
|
28
37
|
"license": "Apache-2.0",
|
|
29
38
|
"dependencies": {
|
|
30
|
-
"
|
|
39
|
+
"color-convert": "^2.0.1",
|
|
40
|
+
"optional-require": "^1.1.8"
|
|
31
41
|
},
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"ansi-colors": ">=4",
|
|
44
|
+
"chalk": ">=4"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"ansi-colors": {
|
|
48
|
+
"optional": true
|
|
49
|
+
},
|
|
50
|
+
"chalk": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
35
53
|
},
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"exclude": [
|
|
44
|
-
"coverage",
|
|
45
|
-
"*clap.js",
|
|
46
|
-
"gulpfile.js",
|
|
47
|
-
"dist",
|
|
48
|
-
"test"
|
|
49
|
-
],
|
|
50
|
-
"check-coverage": true,
|
|
51
|
-
"statements": 100,
|
|
52
|
-
"branches": 100,
|
|
53
|
-
"functions": 100,
|
|
54
|
-
"lines": 100,
|
|
55
|
-
"cache": true
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@vitest/coverage-v8": "4.1.5",
|
|
56
|
+
"@xarc/run": "^2.3.0",
|
|
57
|
+
"ansi-colors": "^4.1.3",
|
|
58
|
+
"chalk": "^4.1.2",
|
|
59
|
+
"prettier": "^2.0.2",
|
|
60
|
+
"vitest": "4.1.5"
|
|
56
61
|
},
|
|
57
62
|
"prettier": {
|
|
58
63
|
"printWidth": 100,
|