chalk 3.0.0 → 4.1.2

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 CHANGED
@@ -1,25 +1,3 @@
1
- declare const enum LevelEnum {
2
- /**
3
- All colors disabled.
4
- */
5
- None = 0,
6
-
7
- /**
8
- Basic 16 colors support.
9
- */
10
- Basic = 1,
11
-
12
- /**
13
- ANSI 256 colors support.
14
- */
15
- Ansi256 = 2,
16
-
17
- /**
18
- Truecolor 16 million colors support.
19
- */
20
- TrueColor = 3
21
- }
22
-
23
1
  /**
24
2
  Basic foreground colors.
25
3
 
@@ -89,22 +67,34 @@ declare type Modifiers =
89
67
  | 'visible';
90
68
 
91
69
  declare namespace chalk {
92
- type Level = LevelEnum;
70
+ /**
71
+ Levels:
72
+ - `0` - All colors disabled.
73
+ - `1` - Basic 16 colors support.
74
+ - `2` - ANSI 256 colors support.
75
+ - `3` - Truecolor 16 million colors support.
76
+ */
77
+ type Level = 0 | 1 | 2 | 3;
93
78
 
94
79
  interface Options {
95
80
  /**
96
81
  Specify the color support for Chalk.
82
+
97
83
  By default, color support is automatically detected based on the environment.
84
+
85
+ Levels:
86
+ - `0` - All colors disabled.
87
+ - `1` - Basic 16 colors support.
88
+ - `2` - ANSI 256 colors support.
89
+ - `3` - Truecolor 16 million colors support.
98
90
  */
99
91
  level?: Level;
100
92
  }
101
93
 
102
- interface Instance {
103
- /**
104
- Return a new Chalk instance.
105
- */
106
- new (options?: Options): Chalk;
107
- }
94
+ /**
95
+ Return a new Chalk instance.
96
+ */
97
+ type Instance = new (options?: Options) => Chalk;
108
98
 
109
99
  /**
110
100
  Detect whether the terminal supports color.
@@ -147,6 +137,13 @@ declare namespace chalk {
147
137
  DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
148
138
  `);
149
139
  ```
140
+
141
+ @example
142
+ ```
143
+ import chalk = require('chalk');
144
+
145
+ log(chalk.red.bgBlack`2 + 3 = {bold ${2 + 3}}`)
146
+ ```
150
147
  */
151
148
  (text: TemplateStringsArray, ...placeholders: unknown[]): string;
152
149
 
@@ -161,7 +158,14 @@ declare namespace chalk {
161
158
 
162
159
  /**
163
160
  The color support for Chalk.
161
+
164
162
  By default, color support is automatically detected based on the environment.
163
+
164
+ Levels:
165
+ - `0` - All colors disabled.
166
+ - `1` - Basic 16 colors support.
167
+ - `2` - ANSI 256 colors support.
168
+ - `3` - Truecolor 16 million colors support.
165
169
  */
166
170
  level: Level;
167
171
 
@@ -400,7 +404,7 @@ This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
400
404
  */
401
405
  declare const chalk: chalk.Chalk & chalk.ChalkFunction & {
402
406
  supportsColor: chalk.ColorSupport | false;
403
- Level: typeof LevelEnum;
407
+ Level: chalk.Level;
404
408
  Color: Color;
405
409
  ForegroundColor: ForegroundColor;
406
410
  BackgroundColor: BackgroundColor;
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "chalk",
3
- "version": "3.0.0",
3
+ "version": "4.1.2",
4
4
  "description": "Terminal string styling done right",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/chalk",
7
+ "funding": "https://github.com/chalk/chalk?sponsor=1",
7
8
  "main": "source",
8
9
  "engines": {
9
- "node": ">=8"
10
+ "node": ">=10"
10
11
  },
11
12
  "scripts": {
12
13
  "test": "xo && nyc ava && tsd",
@@ -46,18 +47,22 @@
46
47
  "devDependencies": {
47
48
  "ava": "^2.4.0",
48
49
  "coveralls": "^3.0.7",
49
- "execa": "^3.2.0",
50
+ "execa": "^4.0.0",
50
51
  "import-fresh": "^3.1.0",
51
52
  "matcha": "^0.7.0",
52
- "nyc": "^14.1.1",
53
+ "nyc": "^15.0.0",
53
54
  "resolve-from": "^5.0.0",
54
55
  "tsd": "^0.7.4",
55
- "xo": "^0.25.3"
56
+ "xo": "^0.28.2"
56
57
  },
57
58
  "xo": {
58
59
  "rules": {
59
60
  "unicorn/prefer-string-slice": "off",
60
- "unicorn/prefer-includes": "off"
61
+ "unicorn/prefer-includes": "off",
62
+ "@typescript-eslint/member-ordering": "off",
63
+ "no-redeclare": "off",
64
+ "unicorn/string-content": "off",
65
+ "unicorn/better-regex": "off"
61
66
  }
62
67
  }
63
68
  }
package/readme.md CHANGED
@@ -9,10 +9,57 @@
9
9
 
10
10
  > Terminal string styling done right
11
11
 
12
- [![Build Status](https://travis-ci.org/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk) [![Coverage Status](https://coveralls.io/repos/github/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/github/chalk/chalk?branch=master) [![npm dependents](https://badgen.net/npm/dependents/chalk)](https://www.npmjs.com/package/chalk?activeTab=dependents) [![Downloads](https://badgen.net/npm/dt/chalk)](https://www.npmjs.com/package/chalk) [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo) ![TypeScript-ready](https://img.shields.io/npm/types/chalk.svg)
12
+ [![Build Status](https://travis-ci.org/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk) [![Coverage Status](https://coveralls.io/repos/github/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/github/chalk/chalk?branch=master) [![npm dependents](https://badgen.net/npm/dependents/chalk)](https://www.npmjs.com/package/chalk?activeTab=dependents) [![Downloads](https://badgen.net/npm/dt/chalk)](https://www.npmjs.com/package/chalk) [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo) ![TypeScript-ready](https://img.shields.io/npm/types/chalk.svg) [![run on repl.it](https://repl.it/badge/github/chalk/chalk)](https://repl.it/github/chalk/chalk)
13
13
 
14
14
  <img src="https://cdn.jsdelivr.net/gh/chalk/ansi-styles@8261697c95bf34b6c7767e2cbe9941a851d59385/screenshot.svg" width="900">
15
15
 
16
+ <br>
17
+
18
+ ---
19
+
20
+ <div align="center">
21
+ <p>
22
+ <p>
23
+ <sup>
24
+ Sindre Sorhus' open source work is supported by the community on <a href="https://github.com/sponsors/sindresorhus">GitHub Sponsors</a> and <a href="https://stakes.social/0x44d871aebF0126Bf646753E2C976Aa7e68A66c15">Dev</a>
25
+ </sup>
26
+ </p>
27
+ <sup>Special thanks to:</sup>
28
+ <br>
29
+ <br>
30
+ <a href="https://standardresume.co/tech">
31
+ <img src="https://sindresorhus.com/assets/thanks/standard-resume-logo.svg" width="160"/>
32
+ </a>
33
+ <br>
34
+ <br>
35
+ <a href="https://retool.com/?utm_campaign=sindresorhus">
36
+ <img src="https://sindresorhus.com/assets/thanks/retool-logo.svg" width="230"/>
37
+ </a>
38
+ <br>
39
+ <br>
40
+ <a href="https://doppler.com/?utm_campaign=github_repo&utm_medium=referral&utm_content=chalk&utm_source=github">
41
+ <div>
42
+ <img src="https://dashboard.doppler.com/imgs/logo-long.svg" width="240" alt="Doppler">
43
+ </div>
44
+ <b>All your environment variables, in one place</b>
45
+ <div>
46
+ <span>Stop struggling with scattered API keys, hacking together home-brewed tools,</span>
47
+ <br>
48
+ <span>and avoiding access controls. Keep your team and servers in sync with Doppler.</span>
49
+ </div>
50
+ </a>
51
+ <br>
52
+ <a href="https://uibakery.io/?utm_source=chalk&utm_medium=sponsor&utm_campaign=github">
53
+ <div>
54
+ <img src="https://sindresorhus.com/assets/thanks/uibakery-logo.jpg" width="270" alt="UI Bakery">
55
+ </div>
56
+ </a>
57
+ </p>
58
+ </div>
59
+
60
+ ---
61
+
62
+ <br>
16
63
 
17
64
  ## Highlights
18
65
 
@@ -24,8 +71,7 @@
24
71
  - Doesn't extend `String.prototype`
25
72
  - Clean and focused
26
73
  - Actively maintained
27
- - [Used by ~46,000 packages](https://www.npmjs.com/browse/depended/chalk) as of October 1, 2019
28
-
74
+ - [Used by ~50,000 packages](https://www.npmjs.com/browse/depended/chalk) as of January 1, 2020
29
75
 
30
76
  ## Install
31
77
 
@@ -33,7 +79,6 @@
33
79
  $ npm install chalk
34
80
  ```
35
81
 
36
-
37
82
  ## Usage
38
83
 
39
84
  ```js
@@ -107,7 +152,6 @@ console.log(chalk.green('Hello %s'), name);
107
152
  //=> 'Hello Sindre'
108
153
  ```
109
154
 
110
-
111
155
  ## API
112
156
 
113
157
  ### chalk.`<style>[.<style>...](string, [string...])`
@@ -149,7 +193,6 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
149
193
 
150
194
  `chalk.stderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `chalk.supportsColor` apply to this too. `chalk.stderr.supportsColor` is exposed for convenience.
151
195
 
152
-
153
196
  ## Styles
154
197
 
155
198
  ### Modifiers
@@ -202,10 +245,9 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
202
245
  - `bgCyanBright`
203
246
  - `bgWhiteBright`
204
247
 
205
-
206
248
  ## Tagged template literal
207
249
 
208
- Chalk can be used as a [tagged template literal](http://exploringjs.com/es6/ch_template-literals.html#_tagged-template-literals).
250
+ Chalk can be used as a [tagged template literal](https://exploringjs.com/es6/ch_template-literals.html#_tagged-template-literals).
209
251
 
210
252
  ```js
211
253
  const chalk = require('chalk');
@@ -221,10 +263,11 @@ console.log(chalk`
221
263
 
222
264
  Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`).
223
265
 
224
- Template styles are chained exactly like normal Chalk styles. The following two statements are equivalent:
266
+ Template styles are chained exactly like normal Chalk styles. The following three statements are equivalent:
225
267
 
226
268
  ```js
227
269
  console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
270
+ console.log(chalk.bold.rgb(10, 100, 200)`Hello!`);
228
271
  console.log(chalk`{bold.rgb(10,100,200) Hello!}`);
229
272
  ```
230
273
 
@@ -232,7 +275,6 @@ Note that function styles (`rgb()`, `hsl()`, `keyword()`, etc.) may not contain
232
275
 
233
276
  All interpolated values (`` chalk`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped.
234
277
 
235
-
236
278
  ## 256 and Truecolor color support
237
279
 
238
280
  Chalk supports 256 colors and [Truecolor](https://gist.github.com/XVilka/8346728) (16 million colors) on supported terminal apps.
@@ -262,24 +304,20 @@ The following color models can be used:
262
304
  - [`ansi`](https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit) - Example: `chalk.ansi(31).bgAnsi(93)('red on yellowBright')`
263
305
  - [`ansi256`](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) - Example: `chalk.bgAnsi256(194)('Honeydew, more or less')`
264
306
 
265
-
266
307
  ## Windows
267
308
 
268
309
  If you're on Windows, do yourself a favor and use [Windows Terminal](https://github.com/microsoft/terminal) instead of `cmd.exe`.
269
310
 
270
-
271
311
  ## Origin story
272
312
 
273
313
  [colors.js](https://github.com/Marak/colors.js) used to be the most popular string styling module, but it has serious deficiencies like extending `String.prototype` which causes all kinds of [problems](https://github.com/yeoman/yo/issues/68) and the package is unmaintained. Although there are other packages, they either do too much or not enough. Chalk is a clean and focused alternative.
274
314
 
275
-
276
315
  ## chalk for enterprise
277
316
 
278
317
  Available as part of the Tidelift Subscription.
279
318
 
280
319
  The maintainers of chalk and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-chalk?utm_source=npm-chalk&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
281
320
 
282
-
283
321
  ## Related
284
322
 
285
323
  - [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
@@ -297,7 +335,6 @@ The maintainers of chalk and thousands of other packages are working with Tideli
297
335
  - [chalk-pipe](https://github.com/LitoMore/chalk-pipe) - Create chalk style schemes with simpler style strings
298
336
  - [terminal-link](https://github.com/sindresorhus/terminal-link) - Create clickable links in the terminal
299
337
 
300
-
301
338
  ## Maintainers
302
339
 
303
340
  - [Sindre Sorhus](https://github.com/sindresorhus)
package/source/index.js CHANGED
@@ -6,6 +6,8 @@ const {
6
6
  stringEncaseCRLFWithFirstIndex
7
7
  } = require('./util');
8
8
 
9
+ const {isArray} = Array;
10
+
9
11
  // `supportsColor.level` → `ansiStyles.color[name]` mapping
10
12
  const levelMapping = [
11
13
  'ansi',
@@ -17,7 +19,7 @@ const levelMapping = [
17
19
  const styles = Object.create(null);
18
20
 
19
21
  const applyOptions = (object, options = {}) => {
20
- if (options.level > 3 || options.level < 0) {
22
+ if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
21
23
  throw new Error('The `level` option should be an integer from 0 to 3');
22
24
  }
23
25
 
@@ -28,6 +30,7 @@ const applyOptions = (object, options = {}) => {
28
30
 
29
31
  class ChalkClass {
30
32
  constructor(options) {
33
+ // eslint-disable-next-line no-constructor-return
31
34
  return chalkFactory(options);
32
35
  }
33
36
  }
@@ -134,14 +137,19 @@ const createStyler = (open, close, parent) => {
134
137
 
135
138
  const createBuilder = (self, _styler, _isEmpty) => {
136
139
  const builder = (...arguments_) => {
140
+ if (isArray(arguments_[0]) && isArray(arguments_[0].raw)) {
141
+ // Called as a template literal, for example: chalk.red`2 + 3 = {bold ${2+3}}`
142
+ return applyStyle(builder, chalkTag(builder, ...arguments_));
143
+ }
144
+
137
145
  // Single argument is hot path, implicit coercion is faster than anything
138
146
  // eslint-disable-next-line no-implicit-coercion
139
147
  return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
140
148
  };
141
149
 
142
- // `__proto__` is used because we must return a function, but there is
150
+ // We alter the prototype because we must return a function, but there is
143
151
  // no way to create a function with a different prototype
144
- builder.__proto__ = proto; // eslint-disable-line no-proto
152
+ Object.setPrototypeOf(builder, proto);
145
153
 
146
154
  builder._generator = self;
147
155
  builder._styler = _styler;
@@ -188,7 +196,7 @@ let template;
188
196
  const chalkTag = (chalk, ...strings) => {
189
197
  const [firstString] = strings;
190
198
 
191
- if (!Array.isArray(firstString)) {
199
+ if (!isArray(firstString) || !isArray(firstString.raw)) {
192
200
  // If chalk() was called by itself or with a string,
193
201
  // return the string itself as a string.
194
202
  return strings.join(' ');
@@ -218,16 +226,4 @@ chalk.supportsColor = stdoutColor;
218
226
  chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
219
227
  chalk.stderr.supportsColor = stderrColor;
220
228
 
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
229
  module.exports = chalk;
@@ -2,7 +2,7 @@
2
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}|\{[a-f\d]{1,6}\})|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'],
@@ -126,8 +126,8 @@ module.exports = (chalk, temporary) => {
126
126
  chunks.push(chunk.join(''));
127
127
 
128
128
  if (styles.length > 0) {
129
- const errMsg = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
130
- throw new Error(errMsg);
129
+ const errMessage = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
130
+ throw new Error(errMessage);
131
131
  }
132
132
 
133
133
  return chunks.join('');