Makes dim when gray a noop on windows terminals.

Fixes #58
This commit is contained in:
Joshua Appelman 2015-07-01 02:21:39 +02:00
parent dde7287192
commit 922ac4b0aa
2 changed files with 32 additions and 1 deletions

View file

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

19
test.js
View file

@ -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 () {