Skip to content

Commit

Permalink
Calculate proper level when forcing color (#72)
Browse files Browse the repository at this point in the history
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 }
```
  • Loading branch information
TooTallNate authored and sindresorhus committed Feb 11, 2018
1 parent 58edd7d commit b16e9a4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
43 changes: 23 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ const hasFlag = require('has-flag');

const env = process.env;

let forceColor;
if (hasFlag('no-color') ||
hasFlag('no-colors') ||
hasFlag('color=false')) {
forceColor = false;
} else if (hasFlag('color') ||
hasFlag('colors') ||
hasFlag('color=true') ||
hasFlag('color=always')) {
forceColor = true;
}
if ('FORCE_COLOR' in env) {
forceColor = env.FORCE_COLOR.length === 0 || parseInt(env.FORCE_COLOR, 10) !== 0;
}

function translateLevel(level) {
if (level === 0) {
return false;
Expand All @@ -18,9 +33,7 @@ function translateLevel(level) {
}

function supportsColor(stream) {
if (hasFlag('no-color') ||
hasFlag('no-colors') ||
hasFlag('color=false')) {
if (forceColor === false) {
return 0;
}

Expand All @@ -34,17 +47,12 @@ function supportsColor(stream) {
return 2;
}

if (hasFlag('color') ||
hasFlag('colors') ||
hasFlag('color=true') ||
hasFlag('color=always')) {
return 1;
}

if (stream && !stream.isTTY) {
if (stream && !stream.isTTY && forceColor !== true) {
return 0;
}

const min = forceColor ? 1 : 0;

if (process.platform === 'win32') {
// Node.js 7.5.0 is the first version of Node.js to include a patch to
// libuv that enables 256 color output on Windows. Anything earlier and it
Expand All @@ -69,7 +77,7 @@ function supportsColor(stream) {
return 1;
}

return 0;
return min;
}

if ('TEAMCITY_VERSION' in env) {
Expand Down Expand Up @@ -103,19 +111,14 @@ function supportsColor(stream) {
}

if (env.TERM === 'dumb') {
return 0;
return min;
}

return 0;
return min;
}

function getSupportLevel(stream) {
let level = supportsColor(stream);

if ('FORCE_COLOR' in env) {
level = (env.FORCE_COLOR.length > 0 && parseInt(env.FORCE_COLOR, 10) === 0) ? 0 : (level || 1);
}

const level = supportsColor(stream);
return translateLevel(level);
}

Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test.beforeEach(() => {
});

test('return true if `FORCE_COLOR` is in env', t => {
process.stdout.isTTY = false;
process.env.FORCE_COLOR = true;
const result = importFresh('.');
t.truthy(result.stdout);
Expand Down Expand Up @@ -307,3 +308,12 @@ test('return level 3 if on Windows 10 build 14931 or later and Node version is >
const result = importFresh('.');
t.is(result.stdout.level, 3);
});

test('return level 2 when FORCE_COLOR is set when not TTY in xterm256', t => {
process.stdout.isTTY = false;
process.env.FORCE_COLOR = true;
process.env.TERM = 'xterm-256color';
const result = importFresh('.');
t.truthy(result.stdout);
t.is(result.stdout.level, 2);
});

0 comments on commit b16e9a4

Please sign in to comment.