supports-color 8.1.1 → 9.1.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/browser.js CHANGED
@@ -1,24 +1,17 @@
1
1
  /* eslint-env browser */
2
- 'use strict';
3
2
 
4
- function getChromeVersion() {
5
- const matches = /(Chrome|Chromium)\/(?<chromeVersion>\d+)\./.exec(navigator.userAgent);
3
+ const isBlinkBasedBrowser = /\b(Chrome|Chromium)\//.test(navigator.userAgent);
6
4
 
7
- if (!matches) {
8
- return;
9
- }
10
-
11
- return Number.parseInt(matches.groups.chromeVersion, 10);
12
- }
13
-
14
- const colorSupport = getChromeVersion() >= 69 ? {
5
+ const colorSupport = isBlinkBasedBrowser ? {
15
6
  level: 1,
16
7
  hasBasic: true,
17
8
  has256: false,
18
- has16m: false
9
+ has16m: false,
19
10
  } : false;
20
11
 
21
- module.exports = {
12
+ const supportsColor = {
22
13
  stdout: colorSupport,
23
- stderr: colorSupport
14
+ stderr: colorSupport,
24
15
  };
16
+
17
+ export default supportsColor;
package/index.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ import {WriteStream} from 'node:tty';
2
+
3
+ export interface Options {
4
+ /**
5
+ Whether `process.argv` should be sniffed for `--color` and `--no-color` flags.
6
+
7
+ @default true
8
+ */
9
+ readonly sniffFlags?: boolean;
10
+ }
11
+
12
+ /**
13
+ Levels:
14
+ - `0` - All colors disabled.
15
+ - `1` - Basic 16 colors support.
16
+ - `2` - ANSI 256 colors support.
17
+ - `3` - Truecolor 16 million colors support.
18
+ */
19
+ export type ColorSupportLevel = 0 | 1 | 2 | 3;
20
+
21
+ /**
22
+ Detect whether the terminal supports color.
23
+ */
24
+ export interface ColorSupport {
25
+ /**
26
+ The color level.
27
+ */
28
+ level: ColorSupportLevel;
29
+
30
+ /**
31
+ Whether basic 16 colors are supported.
32
+ */
33
+ hasBasic: boolean;
34
+
35
+ /**
36
+ Whether ANSI 256 colors are supported.
37
+ */
38
+ has256: boolean;
39
+
40
+ /**
41
+ Whether Truecolor 16 million colors are supported.
42
+ */
43
+ has16m: boolean;
44
+ }
45
+
46
+ export type ColorInfo = ColorSupport | false;
47
+
48
+ export function createSupportsColor(stream: WriteStream, options?: Options): ColorInfo;
49
+
50
+ declare const supportsColor: {
51
+ stdout: ColorInfo;
52
+ stderr: ColorInfo;
53
+ };
54
+
55
+ export default supportsColor;
package/index.js CHANGED
@@ -1,20 +1,31 @@
1
- 'use strict';
2
- const os = require('os');
3
- const tty = require('tty');
4
- const hasFlag = require('has-flag');
1
+ import process from 'node:process';
2
+ import os from 'node:os';
3
+ import tty from 'node:tty';
4
+
5
+ // From: https://github.com/sindresorhus/has-flag/blob/main/index.js
6
+ function hasFlag(flag, argv = process.argv) {
7
+ const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--');
8
+ const position = argv.indexOf(prefix + flag);
9
+ const terminatorPosition = argv.indexOf('--');
10
+ return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
11
+ }
5
12
 
6
13
  const {env} = process;
7
14
 
8
15
  let flagForceColor;
9
- if (hasFlag('no-color') ||
10
- hasFlag('no-colors') ||
11
- hasFlag('color=false') ||
12
- hasFlag('color=never')) {
16
+ if (
17
+ hasFlag('no-color')
18
+ || hasFlag('no-colors')
19
+ || hasFlag('color=false')
20
+ || hasFlag('color=never')
21
+ ) {
13
22
  flagForceColor = 0;
14
- } else if (hasFlag('color') ||
15
- hasFlag('colors') ||
16
- hasFlag('color=true') ||
17
- hasFlag('color=always')) {
23
+ } else if (
24
+ hasFlag('color')
25
+ || hasFlag('colors')
26
+ || hasFlag('color=true')
27
+ || hasFlag('color=always')
28
+ ) {
18
29
  flagForceColor = 1;
19
30
  }
20
31
 
@@ -41,11 +52,11 @@ function translateLevel(level) {
41
52
  level,
42
53
  hasBasic: true,
43
54
  has256: level >= 2,
44
- has16m: level >= 3
55
+ has16m: level >= 3,
45
56
  };
46
57
  }
47
58
 
48
- function supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
59
+ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
49
60
  const noFlagForceColor = envForceColor();
50
61
  if (noFlagForceColor !== undefined) {
51
62
  flagForceColor = noFlagForceColor;
@@ -58,9 +69,9 @@ function supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
58
69
  }
59
70
 
60
71
  if (sniffFlags) {
61
- if (hasFlag('color=16m') ||
62
- hasFlag('color=full') ||
63
- hasFlag('color=truecolor')) {
72
+ if (hasFlag('color=16m')
73
+ || hasFlag('color=full')
74
+ || hasFlag('color=truecolor')) {
64
75
  return 3;
65
76
  }
66
77
 
@@ -84,10 +95,10 @@ function supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
84
95
  // Windows 10 build 14931 is the first release that supports 16m/TrueColor.
85
96
  const osRelease = os.release().split('.');
86
97
  if (
87
- Number(osRelease[0]) >= 10 &&
88
- Number(osRelease[2]) >= 10586
98
+ Number(osRelease[0]) >= 10
99
+ && Number(osRelease[2]) >= 10_586
89
100
  ) {
90
- return Number(osRelease[2]) >= 14931 ? 3 : 2;
101
+ return Number(osRelease[2]) >= 14_931 ? 3 : 2;
91
102
  }
92
103
 
93
104
  return 1;
@@ -136,17 +147,18 @@ function supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
136
147
  return min;
137
148
  }
138
149
 
139
- function getSupportLevel(stream, options = {}) {
140
- const level = supportsColor(stream, {
150
+ export function createSupportsColor(stream, options = {}) {
151
+ const level = _supportsColor(stream, {
141
152
  streamIsTTY: stream && stream.isTTY,
142
- ...options
153
+ ...options,
143
154
  });
144
155
 
145
156
  return translateLevel(level);
146
157
  }
147
158
 
148
- module.exports = {
149
- supportsColor: getSupportLevel,
150
- stdout: getSupportLevel({isTTY: tty.isatty(1)}),
151
- stderr: getSupportLevel({isTTY: tty.isatty(2)})
159
+ const supportsColor = {
160
+ stdout: createSupportsColor({isTTY: tty.isatty(1)}),
161
+ stderr: createSupportsColor({isTTY: tty.isatty(2)}),
152
162
  };
163
+
164
+ export default supportsColor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "supports-color",
3
- "version": "8.1.1",
3
+ "version": "9.1.0",
4
4
  "description": "Detect whether a terminal supports color",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/supports-color",
@@ -10,20 +10,23 @@
10
10
  "email": "sindresorhus@gmail.com",
11
11
  "url": "https://sindresorhus.com"
12
12
  },
13
+ "type": "module",
14
+ "exports": {
15
+ "node": "./index.js",
16
+ "default": "./browser.js"
17
+ },
13
18
  "engines": {
14
- "node": ">=10"
19
+ "node": ">=12"
15
20
  },
16
21
  "scripts": {
17
- "test": "xo && ava"
22
+ "//test": "xo && ava && tsd",
23
+ "test": "xo && tsd"
18
24
  },
19
25
  "files": [
20
26
  "index.js",
27
+ "index.d.ts",
21
28
  "browser.js"
22
29
  ],
23
- "exports": {
24
- "node": "./index.js",
25
- "default": "./browser.js"
26
- },
27
30
  "keywords": [
28
31
  "color",
29
32
  "colour",
@@ -46,13 +49,12 @@
46
49
  "truecolor",
47
50
  "16m"
48
51
  ],
49
- "dependencies": {
50
- "has-flag": "^4.0.0"
51
- },
52
52
  "devDependencies": {
53
- "ava": "^2.4.0",
54
- "import-fresh": "^3.2.2",
55
- "xo": "^0.35.0"
56
- },
57
- "browser": "browser.js"
53
+ "@types/node": "^16.11.7",
54
+ "ava": "^3.15.0",
55
+ "import-fresh": "^3.3.0",
56
+ "tsd": "^0.18.0",
57
+ "typescript": "^4.4.3",
58
+ "xo": "^0.44.0"
59
+ }
58
60
  }
package/readme.md CHANGED
@@ -11,7 +11,7 @@ $ npm install supports-color
11
11
  ## Usage
12
12
 
13
13
  ```js
14
- const supportsColor = require('supports-color');
14
+ import supportsColor from 'supports-color';
15
15
 
16
16
  if (supportsColor.stdout) {
17
17
  console.log('Terminal stdout supports color');
@@ -28,7 +28,7 @@ if (supportsColor.stderr.has16m) {
28
28
 
29
29
  ## API
30
30
 
31
- Returns an `Object` with a `stdout` and `stderr` property for testing either streams. Each property is an `Object`, or `false` if color is not supported.
31
+ Returns an `object` with a `stdout` and `stderr` property for testing either streams. Each property is an `Object`, or `false` if color is not supported.
32
32
 
33
33
  The `stdout`/`stderr` objects specifies a level of support for color through a `.level` property and a corresponding flag:
34
34
 
@@ -36,13 +36,23 @@ The `stdout`/`stderr` objects specifies a level of support for color through a `
36
36
  - `.level = 2` and `.has256 = true`: 256 color support
37
37
  - `.level = 3` and `.has16m = true`: Truecolor support (16 million colors)
38
38
 
39
- ### `require('supports-color').supportsColor(stream, options?)`
39
+ ### Custom instance
40
40
 
41
- Additionally, `supports-color` exposes the `.supportsColor()` function that takes an arbitrary write stream (e.g. `process.stdout`) and an optional options object to (re-)evaluate color support for an arbitrary stream.
41
+ The package also exposes the named export `createSupportColor` function that takes an arbitrary write stream (for example, `process.stdout`) and an optional options object to (re-)evaluate color support for an arbitrary stream.
42
42
 
43
- For example, `require('supports-color').stdout` is the equivalent of `require('supports-color').supportsColor(process.stdout)`.
43
+ ```js
44
+ import {createSupportsColor} from 'supports-color';
45
+
46
+ const stdoutSupportsColor = createSupportsColor(process.stdout);
47
+
48
+ if (stdoutSupportsColor) {
49
+ console.log('Terminal stdout supports color');
50
+ }
51
+
52
+ // `stdoutSupportsColor` is the same as `supportsColor.stdout`
53
+ ```
44
54
 
45
- The options object supports a single boolean property `sniffFlags`. By default it is `true`, which instructs `supportsColor()` to sniff `process.argv` for the multitude of `--color` flags (see _Info_ below). If `false`, then `process.argv` is not considered when determining color support.
55
+ The options object supports a single boolean property `sniffFlags`. By default it is `true`, which instructs the detection to sniff `process.argv` for the multitude of `--color` flags (see _Info_ below). If `false`, then `process.argv` is not considered when determining color support.
46
56
 
47
57
  ## Info
48
58
 
@@ -56,6 +66,8 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
56
66
 
57
67
  - [supports-color-cli](https://github.com/chalk/supports-color-cli) - CLI for this module
58
68
  - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
69
+ - [is-unicode-supported](https://github.com/sindresorhus/is-unicode-supported) - Detect whether the terminal supports Unicode
70
+ - [is-interactive](https://github.com/sindresorhus/is-interactive) - Check if stdout or stderr is interactive
59
71
 
60
72
  ## Maintainers
61
73