From f9bcae1cc2c22244994d6996e2b23e0cf0aebfd4 Mon Sep 17 00:00:00 2001 From: David Buchan-Swanson Date: Sat, 13 Oct 2018 14:55:35 +1000 Subject: [PATCH 1/5] Allow setting support level with FORCE_COLOR flag - All existing tests still pass, but I'm not certain this won't break external code - Closes #78 --- index.js | 14 +++++++++++++- test.js | 13 +++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1704131..aea5500 100644 --- a/index.js +++ b/index.js @@ -15,8 +15,16 @@ if (hasFlag('no-color') || hasFlag('color=always')) { forceColor = true; } +let forceLevel; if ('FORCE_COLOR' in env) { - forceColor = env.FORCE_COLOR.length === 0 || parseInt(env.FORCE_COLOR, 10) !== 0; + if (env.FORCE_COLOR === true) { + forceColor = true; + } else { + forceLevel = env.FORCE_COLOR.length === 0 ? + 1 : + parseInt(env.FORCE_COLOR, 10); + forceColor = forceLevel !== 0; + } } function translateLevel(level) { @@ -47,6 +55,10 @@ function supportsColor(stream) { return 2; } + if (forceLevel !== undefined) { + return forceLevel; + } + if (stream && !stream.isTTY && forceColor !== true) { return 0; } diff --git a/test.js b/test.js index 88904ed..5aa3b71 100644 --- a/test.js +++ b/test.js @@ -317,3 +317,16 @@ test('return level 2 when FORCE_COLOR is set when not TTY in xterm256', t => { t.truthy(result.stdout); t.is(result.stdout.level, 2); }); + +test('supports setting a color value using FORCE_COLOR', t => { + let result; + process.env.FORCE_COLOR = '2'; + result = importFresh('.'); + t.truthy(result.stdout); + t.is(result.stdout.level, 2); + + process.env.FORCE_COLOR = '3'; + result = importFresh('.'); + t.truthy(result.stdout); + t.is(result.stdout.level, 3); +}); From c61550435570b34c05830497daca469e5130e27f Mon Sep 17 00:00:00 2001 From: David Buchan-Swanson Date: Sat, 13 Oct 2018 15:02:42 +1000 Subject: [PATCH 2/5] ensure FORCE_COLOR flag supports ENV values true/false --- index.js | 4 +++- test.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index aea5500..f96a475 100644 --- a/index.js +++ b/index.js @@ -17,8 +17,10 @@ if (hasFlag('no-color') || } let forceLevel; if ('FORCE_COLOR' in env) { - if (env.FORCE_COLOR === true) { + if (env.FORCE_COLOR === true || env.FORCE_COLOR === 'true') { forceColor = true; + } else if (env.FORCE_COLOR === false || env.FORCE_COLOR === 'false') { + forceColor = false; } else { forceLevel = env.FORCE_COLOR.length === 0 ? 1 : diff --git a/test.js b/test.js index 5aa3b71..192b5e7 100644 --- a/test.js +++ b/test.js @@ -330,3 +330,22 @@ test('supports setting a color value using FORCE_COLOR', t => { t.truthy(result.stdout); t.is(result.stdout.level, 3); }); + +test('FORCE_COLOR works when set via command line (all values are strings)', t => { + let result; + process.env.FORCE_COLOR = 'true'; + result = importFresh('.'); + t.truthy(result.stdout); + t.is(result.stdout.level, 1); + + process.stdout.isTTY = false; + process.env.FORCE_COLOR = 'true'; + process.env.TERM = 'xterm-256color'; + result = importFresh('.'); + t.truthy(result.stdout); + t.is(result.stdout.level, 2); + + process.env.FORCE_COLOR = 'false'; + result = importFresh('.'); + t.false(result.stdout); +}); From bdf2cb2d6aac5b8b4d55b7842553ab62a560fbfe Mon Sep 17 00:00:00 2001 From: David Buchan-Swanson Date: Sun, 14 Oct 2018 10:13:42 +1000 Subject: [PATCH 3/5] simplify FORCE_COLOR handling to use just one variable --- index.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index f96a475..96444f6 100644 --- a/index.js +++ b/index.js @@ -8,24 +8,22 @@ let forceColor; if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false')) { - forceColor = false; + forceColor = 0; } else if (hasFlag('color') || hasFlag('colors') || hasFlag('color=true') || hasFlag('color=always')) { - forceColor = true; + forceColor = 1; } -let forceLevel; if ('FORCE_COLOR' in env) { if (env.FORCE_COLOR === true || env.FORCE_COLOR === 'true') { - forceColor = true; + forceColor = 1; } else if (env.FORCE_COLOR === false || env.FORCE_COLOR === 'false') { - forceColor = false; + forceColor = 0; } else { - forceLevel = env.FORCE_COLOR.length === 0 ? + forceColor = env.FORCE_COLOR.length === 0 ? 1 : parseInt(env.FORCE_COLOR, 10); - forceColor = forceLevel !== 0; } } @@ -43,7 +41,7 @@ function translateLevel(level) { } function supportsColor(stream) { - if (forceColor === false) { + if (forceColor === 0) { return 0; } @@ -57,15 +55,11 @@ function supportsColor(stream) { return 2; } - if (forceLevel !== undefined) { - return forceLevel; - } - - if (stream && !stream.isTTY && forceColor !== true) { + if (stream && !stream.isTTY && forceColor === undefined) { return 0; } - const min = forceColor ? 1 : 0; + const min = forceColor || 0; if (process.platform === 'win32') { // Node.js 7.5.0 is the first version of Node.js to include a patch to From 3f6b55f62499ae58f22146fafdf16ee6faafcb4f Mon Sep 17 00:00:00 2001 From: David Buchan-Swanson Date: Sun, 14 Oct 2018 10:19:27 +1000 Subject: [PATCH 4/5] ensure FORCE_COLOR maxes out at a value of three --- index.js | 2 +- test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 96444f6..0831bfc 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,7 @@ if ('FORCE_COLOR' in env) { } else { forceColor = env.FORCE_COLOR.length === 0 ? 1 : - parseInt(env.FORCE_COLOR, 10); + Math.min(parseInt(env.FORCE_COLOR, 10), 3); } } diff --git a/test.js b/test.js index 192b5e7..dce33ad 100644 --- a/test.js +++ b/test.js @@ -320,6 +320,11 @@ test('return level 2 when FORCE_COLOR is set when not TTY in xterm256', t => { test('supports setting a color value using FORCE_COLOR', t => { let result; + process.env.FORCE_COLOR = '1'; + result = importFresh('.'); + t.truthy(result.stdout); + t.is(result.stdout.level, 1); + process.env.FORCE_COLOR = '2'; result = importFresh('.'); t.truthy(result.stdout); @@ -329,6 +334,17 @@ test('supports setting a color value using FORCE_COLOR', t => { result = importFresh('.'); t.truthy(result.stdout); t.is(result.stdout.level, 3); + + process.env.FORCE_COLOR = '0'; + result = importFresh('.'); + t.false(result.stdout); +}); + +test('FORCE_COLOR maxes out at a value of 3', t => { + process.env.FORCE_COLOR = '4'; + const result = importFresh('.'); + t.truthy(result.stdout); + t.is(result.stdout.level, 3); }); test('FORCE_COLOR works when set via command line (all values are strings)', t => { From a888974bd3c1ffa64b71d3fc80a14a11a0a01b0a Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 21 Dec 2018 15:06:38 +0100 Subject: [PATCH 5/5] Update test.js --- test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.js b/test.js index d76670c..bbede42 100644 --- a/test.js +++ b/test.js @@ -325,7 +325,7 @@ test('return level 2 when FORCE_COLOR is set when not TTY in xterm256', t => { t.is(result.stdout.level, 2); }); -test('supports setting a color value using FORCE_COLOR', t => { +test('supports setting a color level using FORCE_COLOR', t => { let result; process.env.FORCE_COLOR = '1'; result = importFresh('.');