2 var escapeStringRegexp = require('escape-string-regexp');
3 var ansiStyles = require('ansi-styles');
4 var stripAnsi = require('strip-ansi');
5 var hasAnsi = require('has-ansi');
6 var supportsColor = require('supports-color');
7 var defineProps = Object.defineProperties;
8 var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
10 function Chalk(options) {
11 // detect mode if not set manually
12 this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
15 // use bright blue on Windows as the normal blue color is illegible
16 if (isSimpleWindowsTerm) {
17 ansiStyles.blue.open = '\u001b[94m';
20 var styles = (function () {
23 Object.keys(ansiStyles).forEach(function (key) {
24 ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
28 return build.call(this, this._styles.concat(key));
36 var proto = defineProps(function chalk() {}, styles);
38 function build(_styles) {
39 var builder = function () {
40 return applyStyle.apply(builder, arguments);
43 builder._styles = _styles;
44 builder.enabled = this.enabled;
45 // __proto__ is used because we must return a function, but there is
46 // no way to create a function with a different prototype.
47 /* eslint-disable no-proto */
48 builder.__proto__ = proto;
53 function applyStyle() {
54 // support varags, but simply cast to string in case there's only one arg
56 var argsLen = args.length;
57 var str = argsLen !== 0 && String(arguments[0]);
60 // don't slice `arguments`, it prevents v8 optimizations
61 for (var a = 1; a < argsLen; a++) {
66 if (!this.enabled || !str) {
70 var nestedStyles = this._styles;
71 var i = nestedStyles.length;
73 // Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
74 // see https://github.com/chalk/chalk/issues/58
75 // If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
76 var originalDim = ansiStyles.dim.open;
77 if (isSimpleWindowsTerm && (nestedStyles.indexOf('gray') !== -1 || nestedStyles.indexOf('grey') !== -1)) {
78 ansiStyles.dim.open = '';
82 var code = ansiStyles[nestedStyles[i]];
84 // Replace any instances already present with a re-opening code
85 // otherwise only the part of the string until said closing code
86 // will be colored, and the rest will simply be 'plain'.
87 str = code.open + str.replace(code.closeRe, code.open) + code.close;
90 // Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue.
91 ansiStyles.dim.open = originalDim;
99 Object.keys(styles).forEach(function (name) {
102 return build.call(this, [name]);
110 defineProps(Chalk.prototype, init());
112 module.exports = new Chalk();
113 module.exports.styles = ansiStyles;
114 module.exports.hasColor = hasAnsi;
115 module.exports.stripColor = stripAnsi;
116 module.exports.supportsColor = supportsColor;