Skip to content

Commit

Permalink
Makes dim when gray a noop on windows terminals.
Browse files Browse the repository at this point in the history
Fixes #58
  • Loading branch information
Joshua Appelman committed Jul 1, 2015
1 parent dde7287 commit 922ac4b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
14 changes: 13 additions & 1 deletion index.js
Expand Up @@ -5,14 +5,15 @@ 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
this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
}

// 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';
}

Expand Down Expand Up @@ -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]];

Expand All @@ -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;
}

Expand Down
19 changes: 19 additions & 0 deletions test.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit 922ac4b

Please sign in to comment.