From e3272e8449989406c4abb0f171f1ae9a6e7cb103 Mon Sep 17 00:00:00 2001 From: Wei Kin Huang Date: Sun, 26 Apr 2015 15:57:41 +0700 Subject: [PATCH] Close #60 PR: Don't override windows blue when in xterm mode. Fixes #59 --- index.js | 2 +- package.json | 5 ++++- test.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4138a64..2bad21b 100644 --- a/index.js +++ b/index.js @@ -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'; } diff --git a/package.json b/package.json index f6d23b1..68c75f1 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test.js b/test.js index 5787e18..b7359bf 100644 --- a/test.js +++ b/test.js @@ -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 () { @@ -32,7 +35,7 @@ describe('chalk', function () { assert.equal(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\u001b[0m\u001b[31m\u001b[42m\u001b[4mfoo\u001b[24m\u001b[49m\u001b[39mfoo\u001b[0m'); }); - it('should be able to cache multiple styles', function() { + it('should be able to cache multiple styles', function () { var red = chalk.red; var green = chalk.green; var redBold = red.bold; @@ -60,6 +63,46 @@ describe('chalk', function () { }); }); +describe('chalk on windows', function () { + var originalEnv; + var 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;