2013-08-03 02:16:26 +02:00
|
|
|
'use strict';
|
2014-06-24 17:13:41 +02:00
|
|
|
var escapeStringRegexp = require('escape-string-regexp');
|
2014-06-04 01:43:07 +02:00
|
|
|
var ansiStyles = require('ansi-styles');
|
2013-12-08 00:33:08 +01:00
|
|
|
var stripAnsi = require('strip-ansi');
|
2014-06-24 21:34:11 +02:00
|
|
|
var hasAnsi = require('has-ansi');
|
2014-06-14 03:49:42 +02:00
|
|
|
var supportsColor = require('supports-color');
|
2013-08-03 02:16:26 +02:00
|
|
|
var defineProps = Object.defineProperties;
|
2015-02-17 15:18:18 +07:00
|
|
|
|
|
|
|
|
function Chalk(options) {
|
|
|
|
|
// detect mode if not set manually
|
2015-02-17 15:24:13 +07:00
|
|
|
this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
|
2015-02-17 15:18:18 +07:00
|
|
|
}
|
2013-08-29 16:14:18 +02:00
|
|
|
|
2014-12-05 16:11:13 +07:00
|
|
|
// use bright blue on Windows as the normal blue color is illegible
|
2015-04-26 15:57:41 +07:00
|
|
|
if (process.platform === 'win32' && !/^xterm/i.test(process.env.TERM || '')) {
|
2014-12-05 16:11:13 +07:00
|
|
|
ansiStyles.blue.open = '\u001b[94m';
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-08 20:43:54 -07:00
|
|
|
function build(_styles) {
|
|
|
|
|
var builder = function builder() {
|
|
|
|
|
return applyStyle.apply(builder, arguments);
|
|
|
|
|
};
|
|
|
|
|
builder._styles = _styles;
|
2015-02-17 15:18:18 +07:00
|
|
|
builder.enabled = this.enabled;
|
2014-07-08 20:43:54 -07:00
|
|
|
// __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;
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-03 02:16:26 +02:00
|
|
|
var styles = (function () {
|
|
|
|
|
var ret = {};
|
|
|
|
|
|
2014-06-04 01:43:07 +02:00
|
|
|
Object.keys(ansiStyles).forEach(function (key) {
|
2014-06-24 17:13:41 +02:00
|
|
|
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
|
|
|
|
|
2013-08-03 02:16:26 +02:00
|
|
|
ret[key] = {
|
|
|
|
|
get: function () {
|
2015-02-17 15:18:18 +07:00
|
|
|
return build.call(this, this._styles.concat(key));
|
2013-08-03 02:16:26 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
})();
|
|
|
|
|
|
2014-07-08 20:43:54 -07:00
|
|
|
var proto = defineProps(function chalk() {}, styles);
|
|
|
|
|
|
2014-06-24 22:59:31 +02:00
|
|
|
function applyStyle() {
|
|
|
|
|
// support varags, but simply cast to string in case there's only one arg
|
2014-07-08 21:08:09 -07:00
|
|
|
var args = arguments;
|
|
|
|
|
var argsLen = args.length;
|
|
|
|
|
var str = argsLen !== 0 && String(arguments[0]);
|
|
|
|
|
if (argsLen > 1) {
|
|
|
|
|
// don't slice `arguments`, it prevents v8 optimizations
|
|
|
|
|
for (var a = 1; a < argsLen; a++) {
|
|
|
|
|
str += ' ' + args[a];
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-24 22:59:31 +02:00
|
|
|
|
2015-02-17 15:18:18 +07:00
|
|
|
if (!this.enabled || !str) {
|
2014-06-24 22:59:31 +02:00
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-23 18:56:00 +07:00
|
|
|
/*jshint validthis: true */
|
2014-07-08 20:43:54 -07:00
|
|
|
var nestedStyles = this._styles;
|
2014-06-26 00:15:15 +02:00
|
|
|
|
2014-08-22 21:28:38 +02:00
|
|
|
var i = nestedStyles.length;
|
|
|
|
|
while (i--) {
|
2014-06-26 00:15:15 +02:00
|
|
|
var code = ansiStyles[nestedStyles[i]];
|
2014-06-24 22:59:31 +02:00
|
|
|
// Replace any instances already present with a re-opening code
|
|
|
|
|
// otherwise only the part of the string until said closing code
|
|
|
|
|
// will be colored, and the rest will simply be 'plain'.
|
2014-06-26 00:15:15 +02:00
|
|
|
str = code.open + str.replace(code.closeRe, code.open) + code.close;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return str;
|
2014-06-24 22:59:31 +02:00
|
|
|
}
|
|
|
|
|
|
2013-08-03 02:16:26 +02:00
|
|
|
function init() {
|
|
|
|
|
var ret = {};
|
|
|
|
|
|
|
|
|
|
Object.keys(styles).forEach(function (name) {
|
|
|
|
|
ret[name] = {
|
|
|
|
|
get: function () {
|
2015-02-17 15:18:18 +07:00
|
|
|
return build.call(this, [name]);
|
2013-08-03 02:16:26 +02:00
|
|
|
}
|
2013-12-16 19:25:01 +01:00
|
|
|
};
|
2013-08-03 02:16:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 15:18:18 +07:00
|
|
|
defineProps(Chalk.prototype, init());
|
2013-12-13 19:36:43 +01:00
|
|
|
|
2015-02-17 15:18:18 +07:00
|
|
|
module.exports = new Chalk();
|
|
|
|
|
module.exports.styles = ansiStyles;
|
|
|
|
|
module.exports.hasColor = hasAnsi;
|
|
|
|
|
module.exports.stripColor = stripAnsi;
|
|
|
|
|
module.exports.supportsColor = supportsColor;
|