export classy chalk object to create isolated ctx

This commit is contained in:
David Keijser 2015-01-23 15:45:40 +01:00
parent aa7e67741f
commit eeadb9f40d
2 changed files with 25 additions and 13 deletions

View file

@ -5,7 +5,13 @@ var stripAnsi = require('strip-ansi');
var hasAnsi = require('has-ansi'); var hasAnsi = require('has-ansi');
var supportsColor = require('supports-color'); var supportsColor = require('supports-color');
var defineProps = Object.defineProperties; var defineProps = Object.defineProperties;
var chalk = module.exports;
function Chalk(enabled) {
// detect mode if not set manually
if (enabled === undefined) {
this.enabled = this.supportsColor;
}
}
// use bright blue on Windows as the normal blue color is illegible // use bright blue on Windows as the normal blue color is illegible
if (process.platform === 'win32') { if (process.platform === 'win32') {
@ -17,6 +23,7 @@ function build(_styles) {
return applyStyle.apply(builder, arguments); return applyStyle.apply(builder, arguments);
}; };
builder._styles = _styles; builder._styles = _styles;
builder.enabled = this.enabled;
// __proto__ is used because we must return a function, but there is // __proto__ is used because we must return a function, but there is
// no way to create a function with a different prototype. // no way to create a function with a different prototype.
builder.__proto__ = proto; builder.__proto__ = proto;
@ -31,7 +38,7 @@ var styles = (function () {
ret[key] = { ret[key] = {
get: function () { get: function () {
return build(this._styles.concat(key)); return build.call(this, this._styles.concat(key));
} }
}; };
}); });
@ -53,7 +60,7 @@ function applyStyle() {
} }
} }
if (!chalk.enabled || !str) { if (!this.enabled || !str) {
return str; return str;
} }
@ -78,7 +85,7 @@ function init() {
Object.keys(styles).forEach(function (name) { Object.keys(styles).forEach(function (name) {
ret[name] = { ret[name] = {
get: function () { get: function () {
return build([name]); return build.call(this, [name]);
} }
}; };
}); });
@ -86,14 +93,11 @@ function init() {
return ret; return ret;
} }
defineProps(chalk, init()); defineProps(Chalk.prototype, init());
chalk.styles = ansiStyles; Chalk.prototype.styles = ansiStyles;
chalk.hasColor = hasAnsi; Chalk.prototype.hasColor = hasAnsi;
chalk.stripColor = stripAnsi; Chalk.prototype.stripColor = stripAnsi;
chalk.supportsColor = supportsColor; Chalk.prototype.supportsColor = supportsColor;
// detect mode if not set manually module.exports = new Chalk();
if (chalk.enabled === undefined) {
chalk.enabled = chalk.supportsColor;
}

View file

@ -68,6 +68,14 @@ describe('chalk.enabled', function () {
}); });
}); });
describe('chalk.constructor', function () {
it('should create a isolated context where colors can be disabled', function () {
var ctx = new chalk.constructor(false);
assert.equal(ctx.red('foo'), 'foo');
assert.equal(chalk.red('foo'), '\u001b[31mfoo\u001b[39m');
});
});
describe('chalk.styles', function () { describe('chalk.styles', function () {
it('should expose the styles as ANSI escape codes', function () { it('should expose the styles as ANSI escape codes', function () {
assert.equal(chalk.styles.red.open, '\u001b[31m'); assert.equal(chalk.styles.red.open, '\u001b[31m');