Remove dim style workaround for Windows

See: https://github.com/chalk/chalk/pull/330/#issuecomment-471977551

It seems to have been fixed in newer Windows 10 builds. We're not interested in adding a conditional for older Windows versions as the fix severely complicates the codebase, and it also creates problems for consumers as the output is not predictable.
This commit is contained in:
Sindre Sorhus 2019-03-12 20:46:55 +07:00
parent 2ca015c4c5
commit 8504a0f05a
2 changed files with 5 additions and 72 deletions

View file

@ -4,8 +4,6 @@ const ansiStyles = require('ansi-styles');
const {stdout: stdoutColor} = require('supports-color');
const template = require('./templates.js');
const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm');
// `supportsColor.level` → `ansiStyles.color[name]` mapping
const levelMapping = [
'ansi',
@ -63,14 +61,14 @@ for (const [styleName, style] of Object.entries(ansiStyles)) {
styles[styleName] = {
get() {
return build.call(this, [...(this._styles || []), style], this._empty, styleName);
return build.call(this, [...(this._styles || []), style], this._empty);
}
};
}
styles.visible = {
get() {
return build.call(this, this._styles || [], true, 'visible');
return build.call(this, this._styles || [], true);
}
};
@ -90,7 +88,7 @@ for (const model of Object.keys(ansiStyles.color.ansi)) {
close: ansiStyles.color.close,
closeRe: ansiStyles.color.closeRe
};
return build.call(this, [...(this._styles || []), codes], this._empty, model);
return build.call(this, [...(this._styles || []), codes], this._empty);
};
}
};
@ -113,7 +111,7 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
close: ansiStyles.bgColor.close,
closeRe: ansiStyles.bgColor.closeRe
};
return build.call(this, [...(this._styles || []), codes], this._empty, model);
return build.call(this, [...(this._styles || []), codes], this._empty);
};
}
};
@ -121,7 +119,7 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
const proto = Object.defineProperties(() => {}, styles);
function build(_styles, _empty, key) {
function build(_styles, _empty) {
const builder = (...arguments_) => applyStyle.call(builder, ...arguments_);
builder._styles = _styles;
builder._empty = _empty;
@ -148,9 +146,6 @@ function build(_styles, _empty, key) {
}
});
// See below for fix regarding invisible grey/dim combination on Windows
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
// `__proto__` is used because we must return a function, but there is
// no way to create a function with a different prototype
builder.__proto__ = proto; // eslint-disable-line no-proto
@ -165,14 +160,6 @@ function applyStyle(...arguments_) {
return this._empty ? '' : string;
}
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
// see https://github.com/chalk/chalk/issues/58
// If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
const originalDim = ansiStyles.dim.open;
if (isSimpleWindowsTerm && this.hasGrey) {
ansiStyles.dim.open = '';
}
for (const code of this._styles.slice().reverse()) {
// Replace any instances already present with a re-opening code
// otherwise only the part of the string until said closing code
@ -185,9 +172,6 @@ function applyStyle(...arguments_) {
string = string.replace(/\r?\n/g, `${code.close}$&${code.open}`);
}
// Reset the original `dim` if we changed it to work around the Windows dimmed gray issue
ansiStyles.dim.open = originalDim;
return string;
}

View file

@ -1,51 +0,0 @@
import test from 'ava';
import importFresh from 'import-fresh';
import resolveFrom from 'resolve-from';
// Spoof supports-color
require('./_supports-color')(__dirname);
let originalEnv;
let originalPlatform;
test.before(() => {
originalEnv = process.env;
originalPlatform = process.platform;
});
test.after(() => {
process.env = originalEnv;
Object.defineProperty(process, 'platform', {value: originalPlatform});
});
test.beforeEach(() => {
process.env = {};
Object.defineProperty(process, 'platform', {value: 'win32'});
// Since chalk internally modifies `ansiStyles.blue.open`, `ansi-styles` needs
// to be removed from the require cache for `require-uncached` to work
delete require.cache[resolveFrom(__dirname, 'ansi-styles')];
});
test('detect a simple term if TERM isn\'t set', t => {
delete process.env.TERM;
const chalk = importFresh('..');
t.is(chalk.blue('foo'), '\u001B[34mfoo\u001B[39m');
});
test('don\'t apply dimmed styling on gray strings, see https://github.com/chalk/chalk/issues/58', t => {
process.env.TERM = 'dumb';
const chalk = importFresh('..');
t.is(chalk.gray.dim('foo'), '\u001B[90mfoo\u001B[22m\u001B[39m');
});
test('apply dimmed styling on xterm compatible terminals', t => {
process.env.TERM = 'xterm';
const chalk = importFresh('..');
t.is(chalk.gray.dim('foo'), '\u001B[90m\u001B[2mfoo\u001B[22m\u001B[39m');
});
test('apply dimmed styling on strings of other colors', t => {
process.env.TERM = 'dumb';
const chalk = importFresh('..');
t.is(chalk.blue.dim('foo'), '\u001B[34m\u001B[2mfoo\u001B[22m\u001B[39m');
});