Fix FORCE_COLOR to set exact level instead of minimum

Previously, FORCE_COLOR was used as a floor value and terminal
detection could return a higher level. For example, FORCE_COLOR=1
on a truecolor terminal would still give level 3.

Now when FORCE_COLOR is set to a specific level (1, 2, or 3), that
exact level is returned without further terminal capability checks.

Fixes #624
This commit is contained in:
Varun Chawla 2026-02-13 00:54:25 -08:00
parent aa06bb5ac3
commit 8255f6a1a5
3 changed files with 58 additions and 1 deletions

View file

@ -91,7 +91,15 @@ function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
return 0;
}
const min = forceColor || 0;
// When `FORCE_COLOR` is set to a specific level, return that level
// directly instead of using it as a minimum. This ensures that
// e.g. FORCE_COLOR=1 gives exactly level 1, not a higher level
// based on terminal capabilities.
if (forceColor !== undefined) {
return forceColor;
}
const min = 0;
if (env.TERM === 'dumb') {
return min;

View file

@ -0,0 +1,4 @@
import {createSupportsColor} from '../source/vendor/supports-color/index.js';
const result = createSupportsColor({isTTY: true});
console.log(JSON.stringify(result));

45
test/force-color.js Normal file
View file

@ -0,0 +1,45 @@
import {fileURLToPath} from 'node:url';
import test from 'ava';
import {execaNode} from 'execa';
const fixturePath = fileURLToPath(new URL('_force-color-fixture.js', import.meta.url));
test('FORCE_COLOR=0 disables color support', async t => {
const {stdout} = await execaNode(fixturePath, {
env: {FORCE_COLOR: '0', COLORTERM: 'truecolor'},
});
t.is(stdout, 'false');
});
test('FORCE_COLOR=1 gives exactly level 1 even with truecolor terminal', async t => {
const {stdout} = await execaNode(fixturePath, {
env: {FORCE_COLOR: '1', COLORTERM: 'truecolor'},
});
const result = JSON.parse(stdout);
t.is(result.level, 1);
t.true(result.hasBasic);
t.false(result.has256);
t.false(result.has16m);
});
test('FORCE_COLOR=2 gives exactly level 2 even with truecolor terminal', async t => {
const {stdout} = await execaNode(fixturePath, {
env: {FORCE_COLOR: '2', COLORTERM: 'truecolor'},
});
const result = JSON.parse(stdout);
t.is(result.level, 2);
t.true(result.hasBasic);
t.true(result.has256);
t.false(result.has16m);
});
test('FORCE_COLOR=3 gives exactly level 3', async t => {
const {stdout} = await execaNode(fixturePath, {
env: {FORCE_COLOR: '3'},
});
const result = JSON.parse(stdout);
t.is(result.level, 3);
t.true(result.hasBasic);
t.true(result.has256);
t.true(result.has16m);
});