chalk-template 0.2.0 → 0.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.
Files changed (3) hide show
  1. package/index.js +22 -2
  2. package/package.json +1 -1
  3. package/readme.md +8 -1
package/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import chalk from 'chalk';
2
2
 
3
3
  // eslint-disable-next-line unicorn/better-regex
4
- 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;
5
- const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
4
+ 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;
5
+ const STYLE_REGEX = /(?:^|\.)(?:(?:(\w+)(?:\(([^)]*)\))?)|(?:#(?=[:a-fA-F\d]{2,})([a-fA-F\d]{6})?(?::([a-fA-F\d]{6}))?))/g;
6
6
  const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
7
7
  const ESCAPE_REGEX = /\\(u(?:[a-f\d]{4}|{[a-f\d]{1,6}})|x[a-f\d]{2}|.)|([^\\])/gi;
8
8
 
@@ -53,6 +53,18 @@ function parseArguments(name, arguments_) {
53
53
  return results;
54
54
  }
55
55
 
56
+ function parseHex(hex) {
57
+ const n = Number.parseInt(hex, 16);
58
+ return [
59
+ // eslint-disable-next-line no-bitwise
60
+ (n >> 16) & 0xFF,
61
+ // eslint-disable-next-line no-bitwise
62
+ (n >> 8) & 0xFF,
63
+ // eslint-disable-next-line no-bitwise
64
+ n & 0xFF,
65
+ ];
66
+ }
67
+
56
68
  function parseStyle(style) {
57
69
  STYLE_REGEX.lastIndex = 0;
58
70
 
@@ -64,6 +76,14 @@ function parseStyle(style) {
64
76
 
65
77
  if (matches[2]) {
66
78
  results.push([name, ...parseArguments(name, matches[2])]);
79
+ } else if (matches[3] || matches[4]) {
80
+ if (matches[3]) {
81
+ results.push(['rgb', ...parseHex(matches[3])]);
82
+ }
83
+
84
+ if (matches[4]) {
85
+ results.push(['bgRgb', ...parseHex(matches[4])]);
86
+ }
67
87
  } else {
68
88
  results.push([name]);
69
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chalk-template",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Terminal string styling with tagged template literals",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/chalk-template",
package/readme.md CHANGED
@@ -31,6 +31,13 @@ console.log(chalk`
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
+
35
+
36
+ console.log(chalk`
37
+ There are also {#FF0000 shorthand hex styles} for
38
+ both the {#ABCDEF foreground}, {#:123456 background},
39
+ or {#ABCDEF:123456 both}.
40
+ `);
34
41
  ```
35
42
 
36
43
  ## API
@@ -47,7 +54,7 @@ console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
47
54
  console.log(chalkTemplate`{bold.rgb(10,100,200) Hello!}`);
48
55
  ```
49
56
 
50
- Note that function styles (`rgb()`, `hex()`, etc.) may not contain spaces between parameters.
57
+ Note that function styles (`rgb()`, etc.) may not contain spaces between parameters.
51
58
 
52
59
  All interpolated values (`` chalkTemplate`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped.
53
60