From eeadb9f40d9661be6494552225453f237029f169 Mon Sep 17 00:00:00 2001 From: David Keijser Date: Fri, 23 Jan 2015 15:45:40 +0100 Subject: [PATCH 1/4] export classy chalk object to create isolated ctx --- index.js | 30 +++++++++++++++++------------- test.js | 8 ++++++++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 6274189..a8a5e99 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,13 @@ 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(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 if (process.platform === 'win32') { @@ -17,6 +23,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 +38,7 @@ var styles = (function () { ret[key] = { 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; } @@ -78,7 +85,7 @@ function init() { Object.keys(styles).forEach(function (name) { ret[name] = { get: function () { - return build([name]); + return build.call(this, [name]); } }; }); @@ -86,14 +93,11 @@ function init() { return ret; } -defineProps(chalk, init()); +defineProps(Chalk.prototype, init()); -chalk.styles = ansiStyles; -chalk.hasColor = hasAnsi; -chalk.stripColor = stripAnsi; -chalk.supportsColor = supportsColor; +Chalk.prototype.styles = ansiStyles; +Chalk.prototype.hasColor = hasAnsi; +Chalk.prototype.stripColor = stripAnsi; +Chalk.prototype.supportsColor = supportsColor; -// detect mode if not set manually -if (chalk.enabled === undefined) { - chalk.enabled = chalk.supportsColor; -} +module.exports = new Chalk(); diff --git a/test.js b/test.js index ca5049e..d96fd40 100644 --- a/test.js +++ b/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(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'); From eedf19d4cadf3cebc5bb1da064c71bacc600cf4d Mon Sep 17 00:00:00 2001 From: David Keijser Date: Wed, 28 Jan 2015 14:58:29 +0100 Subject: [PATCH 2/4] updated readme with note about enabled property --- readme.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 0945581..ffb80ac 100644 --- a/readme.md +++ b/readme.md @@ -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 From cc4e4c429fac6d7715d23aacf2e5eec11834ed54 Mon Sep 17 00:00:00 2001 From: David Keijser Date: Thu, 29 Jan 2015 10:53:47 +0100 Subject: [PATCH 3/4] use options hash as argument to constructor --- index.js | 8 +++++--- test.js | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index a8a5e99..8cacd8c 100644 --- a/index.js +++ b/index.js @@ -6,10 +6,12 @@ var hasAnsi = require('has-ansi'); var supportsColor = require('supports-color'); var defineProps = Object.defineProperties; -function Chalk(enabled) { +function Chalk(options) { // detect mode if not set manually - if (enabled === undefined) { - this.enabled = this.supportsColor; + if (!options || options.enabled === undefined) { + this.enabled = supportsColor; + } else { + this.enabled = options.enabled; } } diff --git a/test.js b/test.js index d96fd40..5787e18 100644 --- a/test.js +++ b/test.js @@ -70,7 +70,7 @@ 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); + var ctx = new chalk.constructor({enabled: false}); assert.equal(ctx.red('foo'), 'foo'); assert.equal(chalk.red('foo'), '\u001b[31mfoo\u001b[39m'); }); From 95783b24e07d6581921e1b4f22532c80d7fd7fe5 Mon Sep 17 00:00:00 2001 From: David Keijser Date: Mon, 9 Feb 2015 11:24:13 +0100 Subject: [PATCH 4/4] don't use prototype to export module members --- index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 8cacd8c..511e2d7 100644 --- a/index.js +++ b/index.js @@ -97,9 +97,8 @@ function init() { defineProps(Chalk.prototype, init()); -Chalk.prototype.styles = ansiStyles; -Chalk.prototype.hasColor = hasAnsi; -Chalk.prototype.stripColor = stripAnsi; -Chalk.prototype.supportsColor = supportsColor; - module.exports = new Chalk(); +module.exports.styles = ansiStyles; +module.exports.hasColor = hasAnsi; +module.exports.stripColor = stripAnsi; +module.exports.supportsColor = supportsColor;