From 922ac4b0aa67c42b7e0e79ca4a5a7c33beb3e858 Mon Sep 17 00:00:00 2001 From: Joshua Appelman Date: Wed, 1 Jul 2015 02:21:39 +0200 Subject: [PATCH] Makes dim when gray a noop on windows terminals. Fixes #58 --- index.js | 14 +++++++++++++- test.js | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 96f0ccb..cbe9288 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ var stripAnsi = require('strip-ansi'); var hasAnsi = require('has-ansi'); var supportsColor = require('supports-color'); var defineProps = Object.defineProperties; +var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM); function Chalk(options) { // detect mode if not set manually @@ -12,7 +13,7 @@ function Chalk(options) { } // use bright blue on Windows as the normal blue color is illegible -if (process.platform === 'win32' && !/^xterm/i.test(process.env.TERM)) { +if (isSimpleWindowsTerm) { ansiStyles.blue.open = '\u001b[94m'; } @@ -69,6 +70,14 @@ function applyStyle() { var nestedStyles = this._styles; var i = nestedStyles.length; + // 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. + var originalDim = ansiStyles.dim.open; + if (isSimpleWindowsTerm && (nestedStyles.indexOf('gray') !== -1 || nestedStyles.indexOf('grey') !== -1)) { + ansiStyles.dim.open = ''; + } + while (i--) { var code = ansiStyles[nestedStyles[i]]; @@ -78,6 +87,9 @@ function applyStyle() { str = code.open + str.replace(code.closeRe, code.open) + code.close; } + // Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue. + ansiStyles.dim.open = originalDim; + return str; } diff --git a/test.js b/test.js index b7359bf..bc39470 100644 --- a/test.js +++ b/test.js @@ -101,6 +101,25 @@ describe('chalk on windows', function () { var chalkCtx = requireUncached('./'); assert.equal(chalkCtx.blue('foo'), '\u001b[34mfoo\u001b[39m'); }); + + it('should not apply dimmed styling on gray strings, see https://github.com/chalk/chalk/issues/58', function () { + process.env.TERM = 'dumb'; + var chalkCtx = requireUncached('./'); + assert.equal(chalkCtx.gray.dim('foo'), '\u001b[90mfoo\u001b[22m\u001b[39m'); + }); + + it('should apply dimmed styling on xterm compatible terminals', function () { + process.env.TERM = 'xterm'; + var chalkCtx = requireUncached('./'); + assert.equal(chalkCtx.gray.dim('foo'), '\u001b[90m\u001b[2mfoo\u001b[22m\u001b[39m'); + }); + + it('should apply dimmed styling on strings of other colors', function () { + process.env.TERM = 'dumb'; + var chalkCtx = requireUncached('./'); + assert.equal(chalkCtx.blue.dim('foo'), '\u001b[94m\u001b[2mfoo\u001b[22m\u001b[39m'); + }); + }); describe('chalk.enabled', function () {