Skip to content

Commit

Permalink
Close #54 PR: export classy chalk object to create isolated ctx. Fixes
Browse files Browse the repository at this point in the history
…#46, Fixes #46
  • Loading branch information
David Keijser authored and sindresorhus committed Feb 17, 2015
1 parent 4d9c98c commit 72d1c11
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
33 changes: 19 additions & 14 deletions index.js
Expand Up @@ -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') {
Expand All @@ -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;
Expand All @@ -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));
}
};
});
Expand All @@ -53,7 +62,7 @@ function applyStyle() {
}
}

if (!chalk.enabled || !str) {
if (!this.enabled || !str) {
return str;
}

Expand All @@ -78,22 +87,18 @@ function init() {
Object.keys(styles).forEach(function (name) {
ret[name] = {
get: function () {
return build([name]);
return build.call(this, [name]);
}
};
});

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;
5 changes: 4 additions & 1 deletion readme.md
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions test.js
Expand Up @@ -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');
Expand Down

0 comments on commit 72d1c11

Please sign in to comment.