From b16e9a4b9f13b3930e184e2f582b55091216fe47 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sun, 11 Feb 2018 04:50:04 -0800 Subject: [PATCH] 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 } ``` --- index.js | 43 +++++++++++++++++++++++-------------------- test.js | 10 ++++++++++ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index bbf9ef1..8880927 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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; } @@ -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 @@ -69,7 +77,7 @@ function supportsColor(stream) { return 1; } - return 0; + return min; } if ('TEAMCITY_VERSION' in env) { @@ -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); } diff --git a/test.js b/test.js index e99a022..88904ed 100644 --- a/test.js +++ b/test.js @@ -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); @@ -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); +});