UNPKG

3 kBJavaScriptView Raw
1'use strict';
2var escapeStringRegexp = require('escape-string-regexp');
3var ansiStyles = require('ansi-styles');
4var supportsColor = require('supports-color');
5var defineProps = Object.defineProperties;
6var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
7
8function Chalk(options) {
9 // detect mode if not set manually
10 this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
11}
12
13// use bright blue on Windows as the normal blue color is illegible
14if (isSimpleWindowsTerm) {
15 ansiStyles.blue.open = '\u001b[94m';
16}
17
18var styles = {};
19
20Object.keys(ansiStyles).forEach(function (key) {
21 ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
22
23 styles[key] = {
24 get: function () {
25 return build.call(this, this._styles ? this._styles.concat(key) : [key]);
26 }
27 };
28});
29
30var proto = defineProps(function chalk() {}, styles);
31
32function build(_styles) {
33 var builder = function () {
34 return applyStyle.apply(builder, arguments);
35 };
36
37 builder._styles = _styles;
38 builder.enabled = this.enabled;
39 // __proto__ is used because we must return a function, but there is
40 // no way to create a function with a different prototype.
41 /* eslint-disable no-proto */
42 builder.__proto__ = proto;
43
44 return builder;
45}
46
47function applyStyle() {
48 // support varags, but simply cast to string in case there's only one arg
49 var args = arguments;
50 var argsLen = args.length;
51 var str = argsLen !== 0 && String(arguments[0]);
52
53 if (argsLen > 1) {
54 // don't slice `arguments`, it prevents v8 optimizations
55 for (var a = 1; a < argsLen; a++) {
56 str += ' ' + args[a];
57 }
58 }
59
60 if (!this.enabled || !str) {
61 return str;
62 }
63
64 var nestedStyles = this._styles;
65 var i = nestedStyles.length;
66
67 // Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
68 // see https://github.com/chalk/chalk/issues/58
69 // If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
70 var originalDim = ansiStyles.dim.open;
71 if (isSimpleWindowsTerm && (nestedStyles.indexOf('gray') !== -1 || nestedStyles.indexOf('grey') !== -1)) {
72 ansiStyles.dim.open = '';
73 }
74
75 while (i--) {
76 var code = ansiStyles[nestedStyles[i]];
77
78 // Replace any instances already present with a re-opening code
79 // otherwise only the part of the string until said closing code
80 // will be colored, and the rest will simply be 'plain'.
81 str = code.open + str.replace(code.closeRe, code.open) + code.close;
82
83 // Close the styling before a linebreak and reopen
84 // after next line to fix a bleed issue on OS X
85 // https://github.com/chalk/chalk/pull/92
86 str = str.replace(/\r?\n/g, code.close + '$&' + code.open);
87 }
88
89 // Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue.
90 ansiStyles.dim.open = originalDim;
91
92 return str;
93}
94
95defineProps(Chalk.prototype, styles);
96
97module.exports = new Chalk();
98module.exports.styles = ansiStyles;
99module.exports.supportsColor = supportsColor;