chalk-template 0.4.0 → 1.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 +50 -5
- package/index.js +74 -59
- package/package.json +10 -8
- package/readme.md +27 -8
package/index.d.ts
CHANGED
@@ -5,7 +5,7 @@ Terminal string styling with [tagged template literals](https://developer.mozill
|
|
5
5
|
```
|
6
6
|
import chalkTemplate from 'chalk-template';
|
7
7
|
|
8
|
-
log(chalkTemplate`
|
8
|
+
console.log(chalkTemplate`
|
9
9
|
CPU: {red ${cpu.totalPercent}%}
|
10
10
|
RAM: {green ${ram.used / ram.total * 100}%}
|
11
11
|
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
|
@@ -17,19 +17,64 @@ DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
|
|
17
17
|
import chalkTemplate from 'chalk-template';
|
18
18
|
import chalk from 'chalk';
|
19
19
|
|
20
|
-
log(chalk.red.bgBlack(chalkTemplate`2 + 3 = {bold ${2 + 3}}`));
|
20
|
+
console.log(chalk.red.bgBlack(chalkTemplate`2 + 3 = {bold ${2 + 3}}`));
|
21
21
|
```
|
22
22
|
*/
|
23
23
|
export default function chalkTemplate(text: TemplateStringsArray, ...placeholders: unknown[]): string;
|
24
24
|
|
25
25
|
/**
|
26
|
-
Terminal string styling
|
26
|
+
Terminal string styling with [tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates),
|
27
|
+
configured for standard error instead of standard output
|
27
28
|
|
28
29
|
@example
|
29
30
|
```
|
30
|
-
import {
|
31
|
+
import {chalkTemplateStderr as chalkTemplate} from 'chalk-template';
|
31
32
|
|
32
|
-
log(
|
33
|
+
console.log(chalkTemplate`
|
34
|
+
CPU: {red ${cpu.totalPercent}%}
|
35
|
+
RAM: {green ${ram.used / ram.total * 100}%}
|
36
|
+
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
|
37
|
+
`);
|
38
|
+
```
|
39
|
+
|
40
|
+
@example
|
41
|
+
```
|
42
|
+
import {chalkTemplateStderr as chalkTemplate} from 'chalk-template';
|
43
|
+
import {chalkStderr as chalk} from 'chalk';
|
44
|
+
|
45
|
+
console.log(chalk.red.bgBlack(chalkTemplate`2 + 3 = {bold ${2 + 3}}`));
|
46
|
+
```
|
47
|
+
*/
|
48
|
+
export function chalkTemplateStderr(text: TemplateStringsArray, ...placeholders: unknown[]): string;
|
49
|
+
|
50
|
+
/**
|
51
|
+
Terminal string styling.
|
52
|
+
|
53
|
+
This function can be useful if you need to wrap the template function. However, prefer the default export whenever possible.
|
54
|
+
|
55
|
+
__Note:__ It's up to you to properly escape the input.
|
56
|
+
|
57
|
+
@example
|
58
|
+
```
|
59
|
+
import {template} from 'chalk-template';
|
60
|
+
|
61
|
+
console.log(template('Today is {red hot}'));
|
33
62
|
```
|
34
63
|
*/
|
35
64
|
export function template(text: string): string;
|
65
|
+
|
66
|
+
/**
|
67
|
+
Terminal string styling, configured for stderr.
|
68
|
+
|
69
|
+
This function can be useful if you need to wrap the template function. However, prefer the `chalkTemplateStderr` export whenever possible.
|
70
|
+
|
71
|
+
__Note:__ It's up to you to properly escape the input.
|
72
|
+
|
73
|
+
@example
|
74
|
+
```
|
75
|
+
import {templateStderr as template} from 'chalk-template';
|
76
|
+
|
77
|
+
console.log(template('Today is {red hot}'));
|
78
|
+
```
|
79
|
+
*/
|
80
|
+
export function templateStderr(text: string): string;
|
package/index.js
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
// eslint-disable-next-line unicorn/import-style
|
2
|
+
import chalk, {chalkStderr} from 'chalk';
|
2
3
|
|
3
4
|
// eslint-disable-next-line unicorn/better-regex
|
4
5
|
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;
|
@@ -24,7 +25,7 @@ function unescape(c) {
|
|
24
25
|
const bracket = c[1] === '{';
|
25
26
|
|
26
27
|
if ((u && !bracket && c.length === 5) || (c[0] === 'x' && c.length === 3)) {
|
27
|
-
return String.
|
28
|
+
return String.fromCodePoint(Number.parseInt(c.slice(1), 16));
|
28
29
|
}
|
29
30
|
|
30
31
|
if (u && bracket) {
|
@@ -92,81 +93,95 @@ function parseStyle(style) {
|
|
92
93
|
return results;
|
93
94
|
}
|
94
95
|
|
95
|
-
function
|
96
|
-
|
96
|
+
function makeTemplate(chalk) {
|
97
|
+
function buildStyle(styles) {
|
98
|
+
const enabled = {};
|
97
99
|
|
98
|
-
|
99
|
-
|
100
|
-
|
100
|
+
for (const layer of styles) {
|
101
|
+
for (const style of layer.styles) {
|
102
|
+
enabled[style[0]] = layer.inverse ? null : style.slice(1);
|
103
|
+
}
|
101
104
|
}
|
102
|
-
}
|
103
105
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
106
|
+
let current = chalk;
|
107
|
+
for (const [styleName, styles] of Object.entries(enabled)) {
|
108
|
+
if (!Array.isArray(styles)) {
|
109
|
+
continue;
|
110
|
+
}
|
109
111
|
|
110
|
-
|
111
|
-
|
112
|
+
if (!(styleName in current)) {
|
113
|
+
throw new Error(`Unknown Chalk style: ${styleName}`);
|
114
|
+
}
|
115
|
+
|
116
|
+
current = styles.length > 0 ? current[styleName](...styles) : current[styleName];
|
112
117
|
}
|
113
118
|
|
114
|
-
|
119
|
+
return current;
|
115
120
|
}
|
116
121
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
122
|
+
function template(string) {
|
123
|
+
const styles = [];
|
124
|
+
const chunks = [];
|
125
|
+
let chunk = [];
|
126
|
+
|
127
|
+
// eslint-disable-next-line max-params
|
128
|
+
string.replace(TEMPLATE_REGEX, (_, escapeCharacter, inverse, style, close, character) => {
|
129
|
+
if (escapeCharacter) {
|
130
|
+
chunk.push(unescape(escapeCharacter));
|
131
|
+
} else if (style) {
|
132
|
+
const string = chunk.join('');
|
133
|
+
chunk = [];
|
134
|
+
chunks.push(styles.length === 0 ? string : buildStyle(styles)(string));
|
135
|
+
styles.push({inverse, styles: parseStyle(style)});
|
136
|
+
} else if (close) {
|
137
|
+
if (styles.length === 0) {
|
138
|
+
throw new Error('Found extraneous } in Chalk template literal');
|
139
|
+
}
|
140
|
+
|
141
|
+
chunks.push(buildStyle(styles)(chunk.join('')));
|
142
|
+
chunk = [];
|
143
|
+
styles.pop();
|
144
|
+
} else {
|
145
|
+
chunk.push(character);
|
137
146
|
}
|
147
|
+
});
|
138
148
|
|
139
|
-
|
140
|
-
chunk = [];
|
141
|
-
styles.pop();
|
142
|
-
} else {
|
143
|
-
chunk.push(character);
|
144
|
-
}
|
145
|
-
});
|
149
|
+
chunks.push(chunk.join(''));
|
146
150
|
|
147
|
-
|
151
|
+
if (styles.length > 0) {
|
152
|
+
throw new Error(`Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`);
|
153
|
+
}
|
148
154
|
|
149
|
-
|
150
|
-
throw new Error(`Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`);
|
155
|
+
return chunks.join('');
|
151
156
|
}
|
152
157
|
|
153
|
-
return
|
158
|
+
return template;
|
154
159
|
}
|
155
160
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
+
function makeChalkTemplate(template) {
|
162
|
+
function chalkTemplate(firstString, ...arguments_) {
|
163
|
+
if (!Array.isArray(firstString) || !Array.isArray(firstString.raw)) {
|
164
|
+
// If chalkTemplate() was called by itself or with a string
|
165
|
+
throw new TypeError('A tagged template literal must be provided');
|
166
|
+
}
|
161
167
|
|
162
|
-
|
168
|
+
const parts = [firstString.raw[0]];
|
163
169
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
170
|
+
for (let index = 1; index < firstString.raw.length; index++) {
|
171
|
+
parts.push(
|
172
|
+
String(arguments_[index - 1]).replace(/[{}\\]/g, '\\$&'),
|
173
|
+
String(firstString.raw[index]),
|
174
|
+
);
|
175
|
+
}
|
176
|
+
|
177
|
+
return template(parts.join(''));
|
169
178
|
}
|
170
179
|
|
171
|
-
return
|
180
|
+
return chalkTemplate;
|
172
181
|
}
|
182
|
+
|
183
|
+
export const template = makeTemplate(chalk);
|
184
|
+
export default makeChalkTemplate(template);
|
185
|
+
|
186
|
+
export const templateStderr = makeTemplate(chalkStderr);
|
187
|
+
export const chalkTemplateStderr = makeChalkTemplate(templateStderr);
|
package/package.json
CHANGED
@@ -1,14 +1,17 @@
|
|
1
1
|
{
|
2
2
|
"name": "chalk-template",
|
3
|
-
"version": "0.
|
3
|
+
"version": "1.0.0",
|
4
4
|
"description": "Terminal string styling with tagged template literals",
|
5
5
|
"license": "MIT",
|
6
6
|
"repository": "chalk/chalk-template",
|
7
7
|
"funding": "https://github.com/chalk/chalk-template?sponsor=1",
|
8
8
|
"type": "module",
|
9
|
-
"exports":
|
9
|
+
"exports": {
|
10
|
+
"types": "./index.d.ts",
|
11
|
+
"default": "./index.js"
|
12
|
+
},
|
10
13
|
"engines": {
|
11
|
-
"node": ">=
|
14
|
+
"node": ">=14.16"
|
12
15
|
},
|
13
16
|
"scripts": {
|
14
17
|
"test": "xo && ava test/index.js && cross-env FORCE_COLOR=0 ava test/no-color.js && cross-env FORCE_COLOR=3 TERM=dumb ava test/full-color.js && cross-env FORCE_COLOR=3 TERM=dumb ava test/template.js && tsd"
|
@@ -43,13 +46,12 @@
|
|
43
46
|
"text"
|
44
47
|
],
|
45
48
|
"dependencies": {
|
46
|
-
"chalk": "^
|
49
|
+
"chalk": "^5.2.0"
|
47
50
|
},
|
48
51
|
"devDependencies": {
|
49
|
-
"ava": "^
|
52
|
+
"ava": "^5.2.0",
|
50
53
|
"cross-env": "^7.0.3",
|
51
|
-
"tsd": "^0.
|
52
|
-
"
|
53
|
-
"xo": "^0.45.0"
|
54
|
+
"tsd": "^0.27.0",
|
55
|
+
"xo": "^0.53.1"
|
54
56
|
}
|
55
57
|
}
|
package/readme.md
CHANGED
@@ -10,36 +10,47 @@ npm install chalk-template
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
+
For printing to standard output (stdout):
|
14
|
+
|
13
15
|
```js
|
14
16
|
import chalkTemplate from 'chalk-template';
|
15
17
|
import chalk from 'chalk';
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
log(chalkTemplate`
|
19
|
+
console.log(chalkTemplate`
|
20
20
|
CPU: {red ${cpu.totalPercent}%}
|
21
21
|
RAM: {green ${ram.used / ram.total * 100}%}
|
22
22
|
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
|
23
23
|
`);
|
24
24
|
|
25
|
-
log(chalk.red.bgBlack(chalkTemplate`2 + 3 = {bold ${2 + 3}}`));
|
25
|
+
console.log(chalk.red.bgBlack(chalkTemplate`2 + 3 = {bold ${2 + 3}}`));
|
26
26
|
|
27
27
|
const miles = 18;
|
28
28
|
const calculateFeet = miles => miles * 5280;
|
29
29
|
|
30
|
-
console.log(
|
30
|
+
console.log(chalkTemplate`
|
31
31
|
There are {bold 5280 feet} in a mile.
|
32
32
|
In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
|
33
33
|
`);
|
34
34
|
|
35
|
-
|
36
|
-
console.log(chalk`
|
35
|
+
console.log(chalkTemplate`
|
37
36
|
There are also {#FF0000 shorthand hex styles} for
|
38
37
|
both the {#ABCDEF foreground}, {#:123456 background},
|
39
38
|
or {#ABCDEF:123456 both}.
|
40
39
|
`);
|
41
40
|
```
|
42
41
|
|
42
|
+
For printing to standard error (stderr):
|
43
|
+
|
44
|
+
```js
|
45
|
+
import {chalkTemplateStderr} from 'chalk-template';
|
46
|
+
|
47
|
+
console.error(chalkTemplateStderr`
|
48
|
+
CPU: {red ${cpu.totalPercent}%}
|
49
|
+
RAM: {green ${ram.used / ram.total * 100}%}
|
50
|
+
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
|
51
|
+
`);
|
52
|
+
```
|
53
|
+
|
43
54
|
## API
|
44
55
|
|
45
56
|
Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`).
|
@@ -60,7 +71,9 @@ All interpolated values (`` chalkTemplate`${foo}` ``) are converted to strings v
|
|
60
71
|
|
61
72
|
## Template function
|
62
73
|
|
63
|
-
|
74
|
+
This function can be useful if you need to wrap the template function. However, prefer the default export whenever possible.
|
75
|
+
|
76
|
+
**Note:** It's up to you to properly escape the input.
|
64
77
|
|
65
78
|
```js
|
66
79
|
import {template} from 'chalk-template';
|
@@ -68,6 +81,12 @@ import {template} from 'chalk-template';
|
|
68
81
|
console.log(template('Today is {red hot}'));
|
69
82
|
```
|
70
83
|
|
84
|
+
```js
|
85
|
+
import {templateStderr} from 'chalk-template';
|
86
|
+
|
87
|
+
console.error(templateStderr('Today is {red hot}'));
|
88
|
+
```
|
89
|
+
|
71
90
|
## Related
|
72
91
|
|
73
92
|
- [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
|