From 97dc3ef58591c0d83abb4c15b25c05eef30904d8 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 17 Mar 2020 01:16:03 +0100 Subject: [PATCH] console: adhere to environment variables enforcing colors Make sure the console color `auto` mode checks the environment variables if any enforces (de)activation of colors. --- lib/internal/console/constructor.js | 14 +++++++++++--- lib/internal/errors.js | 15 ++++++++++++--- lib/internal/tty.js | 15 +++++++++++---- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index f1ba8fbbd7c7e6..1b55a4ec9f3977 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -38,6 +38,9 @@ const { inspect, formatWithOptions } = require('internal/util/inspect'); +const { + getEnforcedColorBits +} = require('internal/tty'); const { isTypedArray, isSet, isMap, isSetIterator, isMapIterator, } = require('internal/util/types'); @@ -266,9 +269,14 @@ const kNoColorInspectOptions = {}; Console.prototype[kGetInspectOptions] = function(stream) { let color = this[kColorMode]; if (color === 'auto') { - color = stream.isTTY && ( - typeof stream.getColorDepth === 'function' ? - stream.getColorDepth() > 2 : true); + const colorBits = getEnforcedColorBits(process.env); + if (colorBits !== undefined) { + color = colorBits !== 1; + } else { + color = stream.isTTY && ( + typeof stream.hasColors === 'function' ? + stream.hasColors() : true); + } } const options = optionsMap.get(this); diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 9f9a0a66f2866e..547b646148fdbd 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -679,9 +679,18 @@ const fatalExceptionStackEnhancers = { colors: defaultColors } } = lazyInternalUtilInspect(); - const colors = (internalBinding('util').guessHandleType(2) === 'TTY' && - require('internal/tty').hasColors()) || - defaultColors; + const { + getEnforcedColorBits, + hasColors + } = require('internal/tty'); + let colors = false; + const colorBits = getEnforcedColorBits(process.env); + if (colorBits !== undefined) { + colors = colorBits !== 1; + } else { + const handleType = internalBinding('util').guessHandleType(2); + colors = (handleType === 'TTY' && hasColors()) || defaultColors; + } try { return inspect(error, { colors }); } catch { diff --git a/lib/internal/tty.js b/lib/internal/tty.js index 44d51737008eb0..821d9723fe8bec 100644 --- a/lib/internal/tty.js +++ b/lib/internal/tty.js @@ -96,10 +96,7 @@ function warnOnDeactivatedColors(env) { } } -// The `getColorDepth` API got inspired by multiple sources such as -// https://github.com/chalk/supports-color, -// https://github.com/isaacs/color-support. -function getColorDepth(env = process.env) { +function getEnforcedColorBits(env) { // Use level 0-3 to support the same levels as `chalk` does. This is done for // consistency throughout the ecosystem. if (env.FORCE_COLOR !== undefined) { @@ -129,6 +126,15 @@ function getColorDepth(env = process.env) { env.TERM === 'dumb') { return COLORS_2; } +} + +// The `getColorDepth` API got inspired by multiple sources such as +// https://github.com/chalk/supports-color, +// https://github.com/isaacs/color-support. +function getColorDepth(env = process.env) { + const colorBits = getEnforcedColorBits(env); + if (colorBits !== undefined) + return colorBits; if (process.platform === 'win32') { // Lazy load for startup performance. @@ -225,5 +231,6 @@ function hasColors(count, env) { module.exports = { getColorDepth, + getEnforcedColorBits, hasColors };