chalk-template 0.3.1 → 0.5.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.
Files changed (4) hide show
  1. package/index.d.ts +59 -2
  2. package/index.js +72 -58
  3. package/package.json +10 -7
  4. package/readme.md +36 -7
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,7 +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
+
25
+ /**
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
28
+
29
+ @example
30
+ ```
31
+ import {chalkTemplateStderr as chalkTemplate} from 'chalk-template';
32
+
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}'));
62
+ ```
63
+ */
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
@@ -24,7 +24,7 @@ function unescape(c) {
24
24
  const bracket = c[1] === '{';
25
25
 
26
26
  if ((u && !bracket && c.length === 5) || (c[0] === 'x' && c.length === 3)) {
27
- return String.fromCharCode(Number.parseInt(c.slice(1), 16));
27
+ return String.fromCodePoint(Number.parseInt(c.slice(1), 16));
28
28
  }
29
29
 
30
30
  if (u && bracket) {
@@ -92,81 +92,95 @@ function parseStyle(style) {
92
92
  return results;
93
93
  }
94
94
 
95
- function buildStyle(styles) {
96
- const enabled = {};
95
+ function makeTemplate(chalk) {
96
+ function buildStyle(styles) {
97
+ const enabled = {};
97
98
 
98
- for (const layer of styles) {
99
- for (const style of layer.styles) {
100
- enabled[style[0]] = layer.inverse ? null : style.slice(1);
99
+ for (const layer of styles) {
100
+ for (const style of layer.styles) {
101
+ enabled[style[0]] = layer.inverse ? null : style.slice(1);
102
+ }
101
103
  }
102
- }
103
104
 
104
- let current = chalk;
105
- for (const [styleName, styles] of Object.entries(enabled)) {
106
- if (!Array.isArray(styles)) {
107
- continue;
108
- }
105
+ let current = chalk;
106
+ for (const [styleName, styles] of Object.entries(enabled)) {
107
+ if (!Array.isArray(styles)) {
108
+ continue;
109
+ }
109
110
 
110
- if (!(styleName in current)) {
111
- throw new Error(`Unknown Chalk style: ${styleName}`);
111
+ if (!(styleName in current)) {
112
+ throw new Error(`Unknown Chalk style: ${styleName}`);
113
+ }
114
+
115
+ current = styles.length > 0 ? current[styleName](...styles) : current[styleName];
112
116
  }
113
117
 
114
- current = styles.length > 0 ? current[styleName](...styles) : current[styleName];
118
+ return current;
115
119
  }
116
120
 
117
- return current;
118
- }
119
-
120
- function template(string) {
121
- const styles = [];
122
- const chunks = [];
123
- let chunk = [];
124
-
125
- // eslint-disable-next-line max-params
126
- string.replace(TEMPLATE_REGEX, (_, escapeCharacter, inverse, style, close, character) => {
127
- if (escapeCharacter) {
128
- chunk.push(unescape(escapeCharacter));
129
- } else if (style) {
130
- const string = chunk.join('');
131
- chunk = [];
132
- chunks.push(styles.length === 0 ? string : buildStyle(styles)(string));
133
- styles.push({inverse, styles: parseStyle(style)});
134
- } else if (close) {
135
- if (styles.length === 0) {
136
- throw new Error('Found extraneous } in Chalk template literal');
121
+ function template(string) {
122
+ const styles = [];
123
+ const chunks = [];
124
+ let chunk = [];
125
+
126
+ // eslint-disable-next-line max-params
127
+ string.replace(TEMPLATE_REGEX, (_, escapeCharacter, inverse, style, close, character) => {
128
+ if (escapeCharacter) {
129
+ chunk.push(unescape(escapeCharacter));
130
+ } else if (style) {
131
+ const string = chunk.join('');
132
+ chunk = [];
133
+ chunks.push(styles.length === 0 ? string : buildStyle(styles)(string));
134
+ styles.push({inverse, styles: parseStyle(style)});
135
+ } else if (close) {
136
+ if (styles.length === 0) {
137
+ throw new Error('Found extraneous } in Chalk template literal');
138
+ }
139
+
140
+ chunks.push(buildStyle(styles)(chunk.join('')));
141
+ chunk = [];
142
+ styles.pop();
143
+ } else {
144
+ chunk.push(character);
137
145
  }
146
+ });
138
147
 
139
- chunks.push(buildStyle(styles)(chunk.join('')));
140
- chunk = [];
141
- styles.pop();
142
- } else {
143
- chunk.push(character);
144
- }
145
- });
148
+ chunks.push(chunk.join(''));
146
149
 
147
- chunks.push(chunk.join(''));
150
+ if (styles.length > 0) {
151
+ throw new Error(`Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`);
152
+ }
148
153
 
149
- if (styles.length > 0) {
150
- throw new Error(`Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`);
154
+ return chunks.join('');
151
155
  }
152
156
 
153
- return chunks.join('');
157
+ return template;
154
158
  }
155
159
 
156
- export default function chalkTemplate(firstString, ...arguments_) {
157
- if (!Array.isArray(firstString) || !Array.isArray(firstString.raw)) {
158
- // If chalkTemplate() was called by itself or with a string
159
- throw new TypeError('A tagged template literal must be provided');
160
- }
160
+ function makeChalkTemplate(template) {
161
+ function chalkTemplate(firstString, ...arguments_) {
162
+ if (!Array.isArray(firstString) || !Array.isArray(firstString.raw)) {
163
+ // If chalkTemplate() was called by itself or with a string
164
+ throw new TypeError('A tagged template literal must be provided');
165
+ }
161
166
 
162
- const parts = [firstString.raw[0]];
167
+ const parts = [firstString.raw[0]];
163
168
 
164
- for (let index = 1; index < firstString.raw.length; index++) {
165
- parts.push(
166
- String(arguments_[index - 1]).replace(/[{}\\]/g, '\\$&'),
167
- String(firstString.raw[index]),
168
- );
169
+ for (let index = 1; index < firstString.raw.length; index++) {
170
+ parts.push(
171
+ String(arguments_[index - 1]).replace(/[{}\\]/g, '\\$&'),
172
+ String(firstString.raw[index]),
173
+ );
174
+ }
175
+
176
+ return template(parts.join(''));
169
177
  }
170
178
 
171
- return template(parts.join(''));
179
+ return chalkTemplate;
172
180
  }
181
+
182
+ export const template = makeTemplate(chalk);
183
+ export default makeChalkTemplate(template);
184
+
185
+ export const templateStderr = makeTemplate(chalk.stderr);
186
+ export const chalkTemplateStderr = makeChalkTemplate(templateStderr);
package/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "chalk-template",
3
- "version": "0.3.1",
3
+ "version": "0.5.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": "./index.js",
9
+ "exports": {
10
+ "types": "./index.d.ts",
11
+ "default": "./index.js"
12
+ },
10
13
  "engines": {
11
- "node": ">=12"
14
+ "node": ">=14.16"
12
15
  },
13
16
  "scripts": {
14
- "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 && tsd"
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"
15
18
  },
16
19
  "files": [
17
20
  "index.js",
@@ -46,9 +49,9 @@
46
49
  "chalk": "^4.1.2"
47
50
  },
48
51
  "devDependencies": {
49
- "ava": "^3.15.0",
52
+ "ava": "^5.1.0",
50
53
  "cross-env": "^7.0.3",
51
- "tsd": "^0.18.0",
52
- "xo": "^0.45.0"
54
+ "tsd": "^0.25.0",
55
+ "xo": "^0.53.1"
53
56
  }
54
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
- const log = console.log;
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(chalk`
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 (`}`).
@@ -58,6 +69,24 @@ Note that function styles (`rgb()`, etc.) may not contain spaces between paramet
58
69
 
59
70
  All interpolated values (`` chalkTemplate`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped.
60
71
 
72
+ ## Template function
73
+
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.
77
+
78
+ ```js
79
+ import {template} from 'chalk-template';
80
+
81
+ console.log(template('Today is {red hot}'));
82
+ ```
83
+
84
+ ```js
85
+ import {templateStderr} from 'chalk-template';
86
+
87
+ console.error(templateStderr('Today is {red hot}'));
88
+ ```
89
+
61
90
  ## Related
62
91
 
63
92
  - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right