Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
'use strict';
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
9
function Chalk(options) {
10
// detect mode if not set manually
11
this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
12
}
13
14
// use bright blue on Windows as the normal blue color is illegible
15
if (process.platform === 'win32') {
16
ansiStyles.blue.open = '\u001b[94m';
17
}
18
19
function build(_styles) {
20
var builder = function builder() {
21
return applyStyle.apply(builder, arguments);
22
};
23
builder._styles = _styles;
24
builder.enabled = this.enabled;
25
// __proto__ is used because we must return a function, but there is
26
// no way to create a function with a different prototype.
27
builder.__proto__ = proto;
28
return builder;
29
}
30
31
var styles = (function () {
32
var ret = {};
33
34
Object.keys(ansiStyles).forEach(function (key) {
35
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
36
37
ret[key] = {
38
get: function () {
39
return build.call(this, this._styles.concat(key));
40
}
41
};
42
});
43
44
return ret;
45
})();
46
47
var proto = defineProps(function chalk() {}, styles);
48
49
function applyStyle() {
50
// support varags, but simply cast to string in case there's only one arg
51
var args = arguments;
52
var argsLen = args.length;
53
var str = argsLen !== 0 && String(arguments[0]);
54
if (argsLen > 1) {
55
// don't slice `arguments`, it prevents v8 optimizations
56
for (var a = 1; a < argsLen; a++) {
57
str += ' ' + args[a];
58
}
59
}
60
61
if (!this.enabled || !str) {
62
return str;
63
}
64
65
/*jshint validthis: true */
66
var nestedStyles = this._styles;
67
68
var i = nestedStyles.length;
69
while (i--) {
70
var code = ansiStyles[nestedStyles[i]];
71
// Replace any instances already present with a re-opening code
72
// otherwise only the part of the string until said closing code
73
// will be colored, and the rest will simply be 'plain'.
74
str = code.open + str.replace(code.closeRe, code.open) + code.close;
75
}
76
77
return str;
78
}
79
80
function init() {
81
var ret = {};
82
83
Object.keys(styles).forEach(function (name) {
84
ret[name] = {
85
get: function () {
86
return build.call(this, [name]);
87
}
88
};
89
});
90
91
return ret;
92
}
93
94
defineProps(Chalk.prototype, init());
95
96
module.exports = new Chalk();
97
module.exports.styles = ansiStyles;
98
module.exports.hasColor = hasAnsi;
99
module.exports.stripColor = stripAnsi;
100
module.exports.supportsColor = supportsColor;
101
102