Skip to content

Commit

Permalink
console: adhere to environment variables enforcing colors
Browse files Browse the repository at this point in the history
Make sure the console color `auto` mode checks the environment
variables if any enforces (de)activation of colors.
  • Loading branch information
BridgeAR committed Mar 17, 2020
1 parent bb6125b commit 97dc3ef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
14 changes: 11 additions & 3 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 11 additions & 4 deletions lib/internal/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -225,5 +231,6 @@ function hasColors(count, env) {

module.exports = {
getColorDepth,
getEnforcedColorBits,
hasColors
};

0 comments on commit 97dc3ef

Please sign in to comment.