Close #54 PR: export classy chalk object to create isolated ctx. Fixes #46, Fixes #46

This commit is contained in:
David Keijser 2015-02-17 15:18:18 +07:00 committed by Sindre Sorhus
parent 4d9c98c603
commit 72d1c11e43
3 changed files with 31 additions and 15 deletions

View file

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