We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1073e54 commit 8cafd83Copy full SHA for 8cafd83
4 files changed
doc/api/tty.md
@@ -144,8 +144,9 @@ position.
144
added: v9.9.0
145
-->
146
147
-* `env` {Object} An object containing the environment variables to check.
148
- **Default:** `process.env`.
+* `env` {Object} An object containing the environment variables to check. This
+ enables simulating the usage of a specific terminal. **Default:**
149
+ `process.env`.
150
* Returns: {number}
151
152
Returns:
@@ -159,11 +160,18 @@ Use this to determine what colors the terminal supports. Due to the nature of
159
160
colors in terminals it is possible to either have false positives or false
161
negatives. It depends on process information and the environment variables that
162
may lie about what terminal is used.
-To enforce a specific behavior without relying on `process.env` it is possible
163
-to pass in an object with different settings.
+It is possible to pass in an `env` object to simulate the usage of a specific
164
+terminal. This can be useful to check how specific environment settings behave.
165
+
166
+To enforce a specific color support, use one of the below environment settings.
167
168
+* 2 colors: `FORCE_COLOR = 0` (Disables colors)
169
+* 16 colors: `FORCE_COLOR = 1`
170
+* 256 colors: `FORCE_COLOR = 2`
171
+* 16,777,216 colors: `FORCE_COLOR = 3`
172
-Use the `NODE_DISABLE_COLORS` environment variable to enforce this function to
-always return 1.
173
+Disabling color support is also possible by using the `NO_COLOR` and
174
+`NODE_DISABLE_COLORS` environment variables.
175
176
### writeStream.getWindowSize()
177
<!-- YAML
lib/internal/tty.js
@@ -72,11 +72,49 @@ const TERM_ENVS_REG_EXP = [
72
/^vt100/
73
];
74
75
+function warnOnDeactivatedColors(env) {
76
+ let name;
77
+ if (env.NODE_DISABLE_COLORS !== undefined)
78
+ name = 'NODE_DISABLE_COLORS';
79
+ if (env.NO_COLOR !== undefined)
80
+ name = 'NO_COLOR';
81
82
+ if (name !== undefined) {
83
+ process.emitWarning(
84
+ `The '${name}' env is ignored due to the 'FORCE_COLOR' env being set.`,
85
+ 'Warning'
86
+ );
87
+ }
88
+}
89
90
// The `getColorDepth` API got inspired by multiple sources such as
91
// https://github.com/chalk/supports-color,
92
// https://github.com/isaacs/color-support.
93
function getColorDepth(env = process.env) {
- if (env.NODE_DISABLE_COLORS || env.TERM === 'dumb') {
94
+ // Use level 0-3 to support the same levels as `chalk` does. This is done for
95
+ // consistency throughout the ecosystem.
96
+ if (env.FORCE_COLOR !== undefined) {
97
+ switch (env.FORCE_COLOR) {
98
+ case '':
99
+ case '1':
100
+ case 'true':
101
+ warnOnDeactivatedColors(env);
102
+ return COLORS_16;
103
+ case '2':
104
105
+ return COLORS_256;
106
+ case '3':
107
108
+ return COLORS_16m;
109
+ default:
110
+ return COLORS_2;
111
112
113
114
+ if (env.NODE_DISABLE_COLORS !== undefined ||
115
+ // See https://no-color.org/
116
+ env.NO_COLOR !== undefined ||
117
+ env.TERM === 'dumb') {
118
return COLORS_2;
119
}
120
test/pseudo-tty/test-tty-color-support.js
@@ -59,11 +59,18 @@ const writeStream = new WriteStream(fd);
59
[{ TERM: 'color' }, 4],
60
[{ TERM: 'linux' }, 4],
61
[{ TERM: 'fail' }, 1],
62
- [{ NODE_DISABLE_COLORS: '1' }, 1],
+ [{ TERM: 'color', NODE_DISABLE_COLORS: '1' }, 1],
63
[{ TERM: 'dumb' }, 1],
64
[{ TERM: 'dumb', COLORTERM: '1' }, 1],
65
[{ TERM: 'terminator' }, 24],
66
- [{ TERM: 'console' }, 4]
+ [{ TERM: 'console' }, 4],
67
+ [{ COLORTERM: '24bit', FORCE_COLOR: '' }, 4],
68
+ [{ NO_COLOR: '1', FORCE_COLOR: '2' }, 8],
69
+ [{ NODE_DISABLE_COLORS: '1', FORCE_COLOR: '3' }, 24],
70
+ [{ NO_COLOR: '1', COLORTERM: '24bit' }, 1],
71
+ [{ NO_COLOR: '', COLORTERM: '24bit' }, 1],
+ [{ TMUX: '1', FORCE_COLOR: 0 }, 1],
+ [{ NO_COLOR: 'true', FORCE_COLOR: 0, COLORTERM: 'truecolor' }, 1],
].forEach(([env, depth], i) => {
const actual = writeStream.getColorDepth(env);
assert.strictEqual(
test/pseudo-tty/test-tty-color-support.out
@@ -0,0 +1,8 @@
1
+(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
2
3
4
5
+(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
6
7
8
0 commit comments