This commit is contained in:
parent
4d9c98c603
commit
72d1c11e43
3 changed files with 31 additions and 15 deletions
33
index.js
33
index.js
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -89,7 +89,10 @@ Multiple arguments will be separated by space.
|
|||
|
||||
### chalk.enabled
|
||||
|
||||
Color support is automatically detected, but you can override it.
|
||||
Color support is automatically detected, but you can override it by setting the
|
||||
`enabled` property or by creating a new instance just for your usage. Changing
|
||||
the property should only be done from end-user facing applications as it
|
||||
affects all consumers of the default chalk instance
|
||||
|
||||
### chalk.supportsColor
|
||||
|
||||
|
|
|
|||
8
test.js
8
test.js
|
|
@ -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({enabled: false});
|
||||
assert.equal(ctx.red('foo'), 'foo');
|
||||
assert.equal(chalk.red('foo'), '\u001b[31mfoo\u001b[39m');
|
||||
});
|
||||
});
|
||||
|
||||
describe('chalk.styles', function () {
|
||||
it('should expose the styles as ANSI escape codes', function () {
|
||||
assert.equal(chalk.styles.red.open, '\u001b[31m');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue