Skip to content

Commit b16e9a4

Browse files
TooTallNatesindresorhus
authored andcommitted
Calculate proper level when forcing color (#72)
Before, the level would be 1 when forcing color when the stream is not a TTY. ``` $ FORCE_COLOR=1 node -p "require('supports-color').stdout" | cat { level: 1, hasBasic: true, has256: false, has16m: false }` ``` Now, the proper level is calculated based on the environment, even when the stream is not a TTY. ``` $ FORCE_COLOR=1 node -p "require('supports-color').stdout" | cat { level: 3, hasBasic: true, has256: true, has16m: true } ```
1 parent 58edd7d commit b16e9a4

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

index.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ const hasFlag = require('has-flag');
44

55
const env = process.env;
66

7+
let forceColor;
8+
if (hasFlag('no-color') ||
9+
hasFlag('no-colors') ||
10+
hasFlag('color=false')) {
11+
forceColor = false;
12+
} else if (hasFlag('color') ||
13+
hasFlag('colors') ||
14+
hasFlag('color=true') ||
15+
hasFlag('color=always')) {
16+
forceColor = true;
17+
}
18+
if ('FORCE_COLOR' in env) {
19+
forceColor = env.FORCE_COLOR.length === 0 || parseInt(env.FORCE_COLOR, 10) !== 0;
20+
}
21+
722
function translateLevel(level) {
823
if (level === 0) {
924
return false;
@@ -18,9 +33,7 @@ function translateLevel(level) {
1833
}
1934

2035
function supportsColor(stream) {
21-
if (hasFlag('no-color') ||
22-
hasFlag('no-colors') ||
23-
hasFlag('color=false')) {
36+
if (forceColor === false) {
2437
return 0;
2538
}
2639

@@ -34,17 +47,12 @@ function supportsColor(stream) {
3447
return 2;
3548
}
3649

37-
if (hasFlag('color') ||
38-
hasFlag('colors') ||
39-
hasFlag('color=true') ||
40-
hasFlag('color=always')) {
41-
return 1;
42-
}
43-
44-
if (stream && !stream.isTTY) {
50+
if (stream && !stream.isTTY && forceColor !== true) {
4551
return 0;
4652
}
4753

54+
const min = forceColor ? 1 : 0;
55+
4856
if (process.platform === 'win32') {
4957
// Node.js 7.5.0 is the first version of Node.js to include a patch to
5058
// libuv that enables 256 color output on Windows. Anything earlier and it
@@ -69,7 +77,7 @@ function supportsColor(stream) {
6977
return 1;
7078
}
7179

72-
return 0;
80+
return min;
7381
}
7482

7583
if ('TEAMCITY_VERSION' in env) {
@@ -103,19 +111,14 @@ function supportsColor(stream) {
103111
}
104112

105113
if (env.TERM === 'dumb') {
106-
return 0;
114+
return min;
107115
}
108116

109-
return 0;
117+
return min;
110118
}
111119

112120
function getSupportLevel(stream) {
113-
let level = supportsColor(stream);
114-
115-
if ('FORCE_COLOR' in env) {
116-
level = (env.FORCE_COLOR.length > 0 && parseInt(env.FORCE_COLOR, 10) === 0) ? 0 : (level || 1);
117-
}
118-
121+
const level = supportsColor(stream);
119122
return translateLevel(level);
120123
}
121124

test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ test.beforeEach(() => {
1212
});
1313

1414
test('return true if `FORCE_COLOR` is in env', t => {
15+
process.stdout.isTTY = false;
1516
process.env.FORCE_COLOR = true;
1617
const result = importFresh('.');
1718
t.truthy(result.stdout);
@@ -307,3 +308,12 @@ test('return level 3 if on Windows 10 build 14931 or later and Node version is >
307308
const result = importFresh('.');
308309
t.is(result.stdout.level, 3);
309310
});
311+
312+
test('return level 2 when FORCE_COLOR is set when not TTY in xterm256', t => {
313+
process.stdout.isTTY = false;
314+
process.env.FORCE_COLOR = true;
315+
process.env.TERM = 'xterm-256color';
316+
const result = importFresh('.');
317+
t.truthy(result.stdout);
318+
t.is(result.stdout.level, 2);
319+
});

0 commit comments

Comments
 (0)