Skip to content

Commit d5190a1

Browse files
committed
Use 256 colors only if terminal supports it.
Signed-off-by: Eric Wang <[email protected]>
1 parent 3d637ca commit d5190a1

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

lib/chalk.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
var _ = require('underscore');
22
var style = require('ansi-styles');
3+
var supportsColor = require('supports-color');
34

45
var chalk = {
5-
enabled: true,
6+
enabled: supportsColor,
7+
use256: supportsColor && supportsColor.has256,
68
themes: {},
79
theme: {}
810
};
@@ -51,7 +53,7 @@ chalk.init = function() {
5153
_.each(fs.readdirSync(dir), function(f) {
5254
var theme = JSON.parse(fs.readFileSync(path.join(dir, f)));
5355
chalk.themes[path.basename(f, '.json')] = _.mapObject(theme, function(v, k) {
54-
return style.color.ansi256.hex(v);
56+
return chalk.use256 ? style.color.ansi256.hex(v) : style.color.ansi.hex(v);
5557
});
5658
});
5759

@@ -74,5 +76,4 @@ chalk.init = function() {
7476
});
7577
};
7678

77-
chalk.init();
7879
module.exports = chalk;

lib/cli.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ var config = require('./config');
1111
// global config < local config < cli params
1212
// Color is a tricky one so we manually handle it here.
1313
function setColorMode() {
14-
var useColor = config.USE_COLOR || false;
15-
if (process.argv.indexOf('--color') >= 0) useColor = true;
16-
if (process.argv.indexOf('--no-color') >= 0) useColor = false;
14+
// FIXME: delete this hack when supports-color handles it well.
15+
if (process.env.TERM_PROGRAM === 'iTerm.app') chalk.use256 = true;
1716

18-
chalk.enabled = useColor;
17+
chalk.enabled = config.USE_COLOR && chalk.enabled;
18+
chalk.init();
1919
chalk.setTheme(config.COLOR_THEME);
2020
}
2121

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"prompt": "^1.0.0",
3838
"request": "^2.74.0",
3939
"sprintf-js": "^1.0.3",
40+
"supports-color": "^3.2.3",
4041
"underscore": "^1.8.3",
4142
"wordwrap": "^1.0.0",
4243
"yargs": "^5.0.0"

test/test_helper.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var assert = require('chai').assert;
33
var chalk = require('../lib/chalk');
44
var h = require('../lib/helper');
55

6+
chalk.init();
7+
68
describe('helper', function() {
79
describe('#prettyState', function() {
810
it('should ok w/ color', function() {

0 commit comments

Comments
 (0)