Don't override windows blue when in xterm mode

This commit is contained in:
Wei Kin Huang 2015-04-17 13:27:48 -04:00
parent 04cb2d21ae
commit faa36fc134
3 changed files with 47 additions and 2 deletions

View file

@ -12,7 +12,7 @@ function Chalk(options) {
}
// use bright blue on Windows as the normal blue color is illegible
if (process.platform === 'win32') {
if (process.platform === 'win32' && !/^xterm/i.test(process.env.TERM)) {
ansiStyles.blue.open = '\u001b[94m';
}

5
package.json Normal file → Executable file
View file

@ -48,6 +48,9 @@
},
"devDependencies": {
"matcha": "^0.6.0",
"mocha": "*"
"mocha": "*",
"require-uncached": "^1.0.2",
"resolve-from": "^1.0.0",
"semver": "^4.3.3"
}
}

42
test.js Normal file → Executable file
View file

@ -1,5 +1,8 @@
'use strict';
var assert = require('assert');
var requireUncached = require('require-uncached');
var resolveFrom = require('resolve-from');
var semver = require('semver');
var chalk = require('./');
describe('chalk', function () {
@ -60,6 +63,45 @@ describe('chalk', function () {
});
});
describe('chalk on windows', function() {
var originalEnv, originalPlatform;
// in node versions older than 0.12.x process.platform cannot be overridden
if (semver.lt(process.version, '0.12.0')) {
return;
}
before(function() {
originalEnv = process.env;
originalPlatform = process.platform;
});
after(function() {
process.env = originalEnv;
Object.defineProperty(process, 'platform', {value: originalPlatform});
});
beforeEach(function() {
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')];
});
it('should replace blue foreground color in cmd.exe', function() {
process.env.TERM = 'dumb';
var chalkCtx = requireUncached('./');
assert.equal(chalkCtx.blue('foo'), '\u001b[94mfoo\u001b[39m');
});
it('shouldn\'t replace blue foreground color in xterm based terminals', function() {
process.env.TERM = 'xterm-256color';
var chalkCtx = requireUncached('./');
assert.equal(chalkCtx.blue('foo'), '\u001b[34mfoo\u001b[39m');
});
});
describe('chalk.enabled', function () {
it('should not output colors when manually disabled', function () {
chalk.enabled = false;