Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate proper level when forcing color #72

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});