From 403e4b9c1c0f5bc2f9b4fd47650d5ad99678ffae Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sun, 29 Aug 2021 13:24:29 -0400 Subject: [PATCH] Remove dim style workaround for Windows (#331) See: https://github.com/chalk/chalk/pull/330/#issuecomment-471977551 The issue 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 it makes the output unpredictable. --- index.js | 23 +++++----------------- test/windows.js | 51 ------------------------------------------------- 2 files changed, 5 insertions(+), 69 deletions(-) delete mode 100644 test/windows.js diff --git a/index.js b/index.js index 2c95ea2..f8a3aec 100644 --- a/index.js +++ b/index.js @@ -5,8 +5,6 @@ const stdoutColor = require('supports-color').stdout; 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', 'ansi', 'ansi256', 'ansi16m']; @@ -53,14 +51,14 @@ for (const key of Object.keys(ansiStyles)) { styles[key] = { get() { const codes = ansiStyles[key]; - return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, key); + return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty); } }; } styles.visible = { get() { - return build.call(this, this._styles || [], true, 'visible'); + return build.call(this, this._styles || [], true); } }; @@ -80,7 +78,7 @@ for (const model of Object.keys(ansiStyles.color.ansi)) { close: ansiStyles.color.close, closeRe: ansiStyles.color.closeRe }; - return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, model); + return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty); }; } }; @@ -103,7 +101,7 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) { close: ansiStyles.bgColor.close, closeRe: ansiStyles.bgColor.closeRe }; - return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, model); + return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty); }; } }; @@ -111,7 +109,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 = function () { return applyStyle.apply(builder, arguments); }; @@ -141,9 +139,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 @@ -172,14 +167,6 @@ function applyStyle() { return this._empty ? '' : str; } - // 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 diff --git a/test/windows.js b/test/windows.js deleted file mode 100644 index fb21b3d..0000000 --- a/test/windows.js +++ /dev/null @@ -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 m = importFresh('..'); - t.is(m.gray.dim('foo'), '\u001B[90mfoo\u001B[22m\u001B[39m'); -}); - -test('apply dimmed styling on xterm compatible terminals', t => { - process.env.TERM = 'xterm'; - const m = importFresh('..'); - t.is(m.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 m = importFresh('..'); - t.is(m.blue.dim('foo'), '\u001B[34m\u001B[2mfoo\u001B[22m\u001B[39m'); -});