colors 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +25 -25
- package/README.md +184 -184
- package/examples/normal-usage.js +77 -77
- package/examples/safe-string.js +76 -76
- package/index.d.ts +134 -0
- package/lib/colors.js +179 -179
- package/lib/custom/trap.js +45 -45
- package/lib/custom/zalgo.js +104 -104
- package/lib/extendStringPrototype.js +113 -113
- package/lib/index.js +11 -11
- package/lib/maps/america.js +11 -11
- package/lib/maps/rainbow.js +13 -13
- package/lib/maps/random.js +7 -7
- package/lib/maps/zebra.js +4 -4
- package/lib/styles.js +76 -76
- package/lib/system/has-flag.js +23 -23
- package/lib/system/supports-colors.js +147 -147
- package/package.json +30 -28
- package/safe.d.ts +45 -0
- package/safe.js +9 -9
- package/themes/generic-logging.js +11 -11
package/examples/safe-string.js
CHANGED
@@ -1,76 +1,76 @@
|
|
1
|
-
var colors = require('../safe');
|
2
|
-
|
3
|
-
console.log(colors.yellow("First some yellow text"));
|
4
|
-
|
5
|
-
console.log(colors.yellow.underline("Underline that text"));
|
6
|
-
|
7
|
-
console.log(colors.red.bold("Make it bold and red"));
|
8
|
-
|
9
|
-
console.log(colors.rainbow("Double Raindows All Day Long"))
|
10
|
-
|
11
|
-
console.log(colors.trap("Drop the bass"))
|
12
|
-
|
13
|
-
console.log(colors.rainbow(colors.trap("DROP THE RAINBOW BASS")));
|
14
|
-
|
15
|
-
console.log(colors.bold.italic.underline.red('Chains are also cool.')); // styles not widely supported
|
16
|
-
|
17
|
-
|
18
|
-
console.log(colors.green('So ') + colors.underline('are') + ' ' + colors.inverse('inverse') + colors.yellow.bold(' styles! ')); // styles not widely supported
|
19
|
-
|
20
|
-
console.log(colors.zebra("Zebras are so fun!"));
|
21
|
-
|
22
|
-
console.log("This is " + colors.strikethrough("not") + " fun.");
|
23
|
-
|
24
|
-
|
25
|
-
console.log(colors.black.bgWhite('Background color attack!'));
|
26
|
-
console.log(colors.random('Use random styles on everything!'))
|
27
|
-
console.log(colors.america('America, Heck Yeah!'));
|
28
|
-
|
29
|
-
console.log('Setting themes is useful')
|
30
|
-
|
31
|
-
//
|
32
|
-
// Custom themes
|
33
|
-
//
|
34
|
-
//console.log('Generic logging theme as JSON'.green.bold.underline);
|
35
|
-
// Load theme with JSON literal
|
36
|
-
colors.setTheme({
|
37
|
-
silly: 'rainbow',
|
38
|
-
input: 'grey',
|
39
|
-
verbose: 'cyan',
|
40
|
-
prompt: 'grey',
|
41
|
-
info: 'green',
|
42
|
-
data: 'grey',
|
43
|
-
help: 'cyan',
|
44
|
-
warn: 'yellow',
|
45
|
-
debug: 'blue',
|
46
|
-
error: 'red'
|
47
|
-
});
|
48
|
-
|
49
|
-
// outputs red text
|
50
|
-
console.log(colors.error("this is an error"));
|
51
|
-
|
52
|
-
// outputs yellow text
|
53
|
-
console.log(colors.warn("this is a warning"));
|
54
|
-
|
55
|
-
// outputs grey text
|
56
|
-
console.log(colors.input("this is an input"));
|
57
|
-
|
58
|
-
|
59
|
-
// console.log('Generic logging theme as file'.green.bold.underline);
|
60
|
-
|
61
|
-
// Load a theme from file
|
62
|
-
colors.setTheme(__dirname + '/../themes/generic-logging.js');
|
63
|
-
|
64
|
-
// outputs red text
|
65
|
-
console.log(colors.error("this is an error"));
|
66
|
-
|
67
|
-
// outputs yellow text
|
68
|
-
console.log(colors.warn("this is a warning"));
|
69
|
-
|
70
|
-
// outputs grey text
|
71
|
-
console.log(colors.input("this is an input"));
|
72
|
-
|
73
|
-
// console.log(colors.zalgo("Don't summon him"))
|
74
|
-
|
75
|
-
|
76
|
-
|
1
|
+
var colors = require('../safe');
|
2
|
+
|
3
|
+
console.log(colors.yellow("First some yellow text"));
|
4
|
+
|
5
|
+
console.log(colors.yellow.underline("Underline that text"));
|
6
|
+
|
7
|
+
console.log(colors.red.bold("Make it bold and red"));
|
8
|
+
|
9
|
+
console.log(colors.rainbow("Double Raindows All Day Long"))
|
10
|
+
|
11
|
+
console.log(colors.trap("Drop the bass"))
|
12
|
+
|
13
|
+
console.log(colors.rainbow(colors.trap("DROP THE RAINBOW BASS")));
|
14
|
+
|
15
|
+
console.log(colors.bold.italic.underline.red('Chains are also cool.')); // styles not widely supported
|
16
|
+
|
17
|
+
|
18
|
+
console.log(colors.green('So ') + colors.underline('are') + ' ' + colors.inverse('inverse') + colors.yellow.bold(' styles! ')); // styles not widely supported
|
19
|
+
|
20
|
+
console.log(colors.zebra("Zebras are so fun!"));
|
21
|
+
|
22
|
+
console.log("This is " + colors.strikethrough("not") + " fun.");
|
23
|
+
|
24
|
+
|
25
|
+
console.log(colors.black.bgWhite('Background color attack!'));
|
26
|
+
console.log(colors.random('Use random styles on everything!'))
|
27
|
+
console.log(colors.america('America, Heck Yeah!'));
|
28
|
+
|
29
|
+
console.log('Setting themes is useful')
|
30
|
+
|
31
|
+
//
|
32
|
+
// Custom themes
|
33
|
+
//
|
34
|
+
//console.log('Generic logging theme as JSON'.green.bold.underline);
|
35
|
+
// Load theme with JSON literal
|
36
|
+
colors.setTheme({
|
37
|
+
silly: 'rainbow',
|
38
|
+
input: 'grey',
|
39
|
+
verbose: 'cyan',
|
40
|
+
prompt: 'grey',
|
41
|
+
info: 'green',
|
42
|
+
data: 'grey',
|
43
|
+
help: 'cyan',
|
44
|
+
warn: 'yellow',
|
45
|
+
debug: 'blue',
|
46
|
+
error: 'red'
|
47
|
+
});
|
48
|
+
|
49
|
+
// outputs red text
|
50
|
+
console.log(colors.error("this is an error"));
|
51
|
+
|
52
|
+
// outputs yellow text
|
53
|
+
console.log(colors.warn("this is a warning"));
|
54
|
+
|
55
|
+
// outputs grey text
|
56
|
+
console.log(colors.input("this is an input"));
|
57
|
+
|
58
|
+
|
59
|
+
// console.log('Generic logging theme as file'.green.bold.underline);
|
60
|
+
|
61
|
+
// Load a theme from file
|
62
|
+
colors.setTheme(__dirname + '/../themes/generic-logging.js');
|
63
|
+
|
64
|
+
// outputs red text
|
65
|
+
console.log(colors.error("this is an error"));
|
66
|
+
|
67
|
+
// outputs yellow text
|
68
|
+
console.log(colors.warn("this is a warning"));
|
69
|
+
|
70
|
+
// outputs grey text
|
71
|
+
console.log(colors.input("this is an input"));
|
72
|
+
|
73
|
+
// console.log(colors.zalgo("Don't summon him"))
|
74
|
+
|
75
|
+
|
76
|
+
|
package/index.d.ts
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
// Type definitions for Colors.js 1.2
|
2
|
+
// Project: https://github.com/Marak/colors.js
|
3
|
+
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>, Staffan Eketorp <https://github.com/staeke>
|
4
|
+
// Definitions: https://github.com/Marak/colors.js
|
5
|
+
|
6
|
+
export interface Color {
|
7
|
+
(text: string): string;
|
8
|
+
|
9
|
+
strip: Color;
|
10
|
+
stripColors: Color;
|
11
|
+
|
12
|
+
black: Color;
|
13
|
+
red: Color;
|
14
|
+
green: Color;
|
15
|
+
yellow: Color;
|
16
|
+
blue: Color;
|
17
|
+
magenta: Color;
|
18
|
+
cyan: Color;
|
19
|
+
white: Color;
|
20
|
+
gray: Color;
|
21
|
+
grey: Color;
|
22
|
+
|
23
|
+
bgBlack: Color;
|
24
|
+
bgRed: Color;
|
25
|
+
bgGreen: Color;
|
26
|
+
bgYellow: Color;
|
27
|
+
bgBlue: Color;
|
28
|
+
bgMagenta: Color;
|
29
|
+
bgCyan: Color;
|
30
|
+
bgWhite: Color;
|
31
|
+
|
32
|
+
reset: Color;
|
33
|
+
bold: Color;
|
34
|
+
dim: Color;
|
35
|
+
italic: Color;
|
36
|
+
underline: Color;
|
37
|
+
inverse: Color;
|
38
|
+
hidden: Color;
|
39
|
+
strikethrough: Color;
|
40
|
+
|
41
|
+
rainbow: Color;
|
42
|
+
zebra: Color;
|
43
|
+
america: Color;
|
44
|
+
trap: Color;
|
45
|
+
random: Color;
|
46
|
+
zalgo: Color;
|
47
|
+
}
|
48
|
+
|
49
|
+
export function setTheme(theme: any): void;
|
50
|
+
|
51
|
+
export let enabled: boolean;
|
52
|
+
|
53
|
+
export const strip: Color;
|
54
|
+
export const stripColors: Color;
|
55
|
+
|
56
|
+
export const black: Color;
|
57
|
+
export const red: Color;
|
58
|
+
export const green: Color;
|
59
|
+
export const yellow: Color;
|
60
|
+
export const blue: Color;
|
61
|
+
export const magenta: Color;
|
62
|
+
export const cyan: Color;
|
63
|
+
export const white: Color;
|
64
|
+
export const gray: Color;
|
65
|
+
export const grey: Color;
|
66
|
+
|
67
|
+
export const bgBlack: Color;
|
68
|
+
export const bgRed: Color;
|
69
|
+
export const bgGreen: Color;
|
70
|
+
export const bgYellow: Color;
|
71
|
+
export const bgBlue: Color;
|
72
|
+
export const bgMagenta: Color;
|
73
|
+
export const bgCyan: Color;
|
74
|
+
export const bgWhite: Color;
|
75
|
+
|
76
|
+
export const reset: Color;
|
77
|
+
export const bold: Color;
|
78
|
+
export const dim: Color;
|
79
|
+
export const italic: Color;
|
80
|
+
export const underline: Color;
|
81
|
+
export const inverse: Color;
|
82
|
+
export const hidden: Color;
|
83
|
+
export const strikethrough: Color;
|
84
|
+
|
85
|
+
export const rainbow: Color;
|
86
|
+
export const zebra: Color;
|
87
|
+
export const america: Color;
|
88
|
+
export const trap: Color;
|
89
|
+
export const random: Color;
|
90
|
+
export const zalgo: Color;
|
91
|
+
|
92
|
+
declare global {
|
93
|
+
interface String {
|
94
|
+
strip: string;
|
95
|
+
stripColors: string;
|
96
|
+
|
97
|
+
black: string;
|
98
|
+
red: string;
|
99
|
+
green: string;
|
100
|
+
yellow: string;
|
101
|
+
blue: string;
|
102
|
+
magenta: string;
|
103
|
+
cyan: string;
|
104
|
+
white: string;
|
105
|
+
gray: string;
|
106
|
+
grey: string;
|
107
|
+
|
108
|
+
bgBlack: string;
|
109
|
+
bgRed: string;
|
110
|
+
bgGreen: string;
|
111
|
+
bgYellow: string;
|
112
|
+
bgBlue: string;
|
113
|
+
bgMagenta: string;
|
114
|
+
bgCyan: string;
|
115
|
+
bgWhite: string;
|
116
|
+
|
117
|
+
reset: string;
|
118
|
+
// @ts-ignore
|
119
|
+
bold: string;
|
120
|
+
dim: string;
|
121
|
+
italic: string;
|
122
|
+
underline: string;
|
123
|
+
inverse: string;
|
124
|
+
hidden: string;
|
125
|
+
strikethrough: string;
|
126
|
+
|
127
|
+
rainbow: string;
|
128
|
+
zebra: string;
|
129
|
+
america: string;
|
130
|
+
trap: string;
|
131
|
+
random: string;
|
132
|
+
zalgo: string;
|
133
|
+
}
|
134
|
+
}
|
package/lib/colors.js
CHANGED
@@ -1,179 +1,179 @@
|
|
1
|
-
/*
|
2
|
-
|
3
|
-
The MIT License (MIT)
|
4
|
-
|
5
|
-
Original Library
|
6
|
-
- Copyright (c) Marak Squires
|
7
|
-
|
8
|
-
Additional functionality
|
9
|
-
- Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
10
|
-
|
11
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
12
|
-
of this software and associated documentation files (the "Software"), to deal
|
13
|
-
in the Software without restriction, including without limitation the rights
|
14
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
15
|
-
copies of the Software, and to permit persons to whom the Software is
|
16
|
-
furnished to do so, subject to the following conditions:
|
17
|
-
|
18
|
-
The above copyright notice and this permission notice shall be included in
|
19
|
-
all copies or substantial portions of the Software.
|
20
|
-
|
21
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
22
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
24
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
26
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
27
|
-
THE SOFTWARE.
|
28
|
-
|
29
|
-
*/
|
30
|
-
|
31
|
-
var colors = {};
|
32
|
-
module['exports'] = colors;
|
33
|
-
|
34
|
-
colors.themes = {};
|
35
|
-
|
36
|
-
var ansiStyles = colors.styles = require('./styles');
|
37
|
-
var defineProps = Object.defineProperties;
|
38
|
-
|
39
|
-
colors.supportsColor = require('./system/supports-colors').supportsColor;
|
40
|
-
|
41
|
-
if (typeof colors.enabled === "undefined") {
|
42
|
-
colors.enabled = colors.supportsColor() !== false;
|
43
|
-
}
|
44
|
-
|
45
|
-
colors.stripColors = colors.strip = function(str){
|
46
|
-
return ("" + str).replace(/\x1B\[\d+m/g, '');
|
47
|
-
};
|
48
|
-
|
49
|
-
|
50
|
-
var stylize = colors.stylize = function stylize (str, style) {
|
51
|
-
if (!colors.enabled) {
|
52
|
-
return str+'';
|
53
|
-
}
|
54
|
-
|
55
|
-
return ansiStyles[style].open + str + ansiStyles[style].close;
|
56
|
-
}
|
57
|
-
|
58
|
-
var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
|
59
|
-
var escapeStringRegexp = function (str) {
|
60
|
-
if (typeof str !== 'string') {
|
61
|
-
throw new TypeError('Expected a string');
|
62
|
-
}
|
63
|
-
return str.replace(matchOperatorsRe, '\\$&');
|
64
|
-
}
|
65
|
-
|
66
|
-
function build(_styles) {
|
67
|
-
var builder = function builder() {
|
68
|
-
return applyStyle.apply(builder, arguments);
|
69
|
-
};
|
70
|
-
builder._styles = _styles;
|
71
|
-
// __proto__ is used because we must return a function, but there is
|
72
|
-
// no way to create a function with a different prototype.
|
73
|
-
builder.__proto__ = proto;
|
74
|
-
return builder;
|
75
|
-
}
|
76
|
-
|
77
|
-
var styles = (function () {
|
78
|
-
var ret = {};
|
79
|
-
ansiStyles.grey = ansiStyles.gray;
|
80
|
-
Object.keys(ansiStyles).forEach(function (key) {
|
81
|
-
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
82
|
-
ret[key] = {
|
83
|
-
get: function () {
|
84
|
-
return build(this._styles.concat(key));
|
85
|
-
}
|
86
|
-
};
|
87
|
-
});
|
88
|
-
return ret;
|
89
|
-
})();
|
90
|
-
|
91
|
-
var proto = defineProps(function colors() {}, styles);
|
92
|
-
|
93
|
-
function applyStyle() {
|
94
|
-
var args = arguments;
|
95
|
-
var argsLen = args.length;
|
96
|
-
var str = argsLen !== 0 && String(arguments[0]);
|
97
|
-
if (argsLen > 1) {
|
98
|
-
for (var a = 1; a < argsLen; a++) {
|
99
|
-
str += ' ' + args[a];
|
100
|
-
}
|
101
|
-
}
|
102
|
-
|
103
|
-
if (!colors.enabled || !str) {
|
104
|
-
return str;
|
105
|
-
}
|
106
|
-
|
107
|
-
var nestedStyles = this._styles;
|
108
|
-
|
109
|
-
var i = nestedStyles.length;
|
110
|
-
while (i--) {
|
111
|
-
var code = ansiStyles[nestedStyles[i]];
|
112
|
-
str = code.open + str.replace(code.closeRe, code.open) + code.close;
|
113
|
-
}
|
114
|
-
|
115
|
-
return str;
|
116
|
-
}
|
117
|
-
|
118
|
-
colors.setTheme = function (theme) {
|
119
|
-
if (typeof theme === 'string') {
|
120
|
-
console.log('colors.setTheme now only accepts an object, not a string. ' +
|
121
|
-
'If you are trying to set a theme from a file, it is now your (the caller\'s) responsibility to require the file. ' +
|
122
|
-
'The old syntax looked like colors.setTheme(__dirname + \'/../themes/generic-logging.js\'); ' +
|
123
|
-
'The new syntax looks like colors.setTheme(require(__dirname + \'/../themes/generic-logging.js\'));');
|
124
|
-
return;
|
125
|
-
}
|
126
|
-
for (var style in theme) {
|
127
|
-
(function(style){
|
128
|
-
colors[style] = function(str){
|
129
|
-
if (typeof theme[style] === 'object'){
|
130
|
-
var out = str;
|
131
|
-
for (var i in theme[style]){
|
132
|
-
out = colors[theme[style][i]](out);
|
133
|
-
}
|
134
|
-
return out;
|
135
|
-
}
|
136
|
-
return colors[theme[style]](str);
|
137
|
-
};
|
138
|
-
})(style)
|
139
|
-
}
|
140
|
-
}
|
141
|
-
|
142
|
-
function init() {
|
143
|
-
var ret = {};
|
144
|
-
Object.keys(styles).forEach(function (name) {
|
145
|
-
ret[name] = {
|
146
|
-
get: function () {
|
147
|
-
return build([name]);
|
148
|
-
}
|
149
|
-
};
|
150
|
-
});
|
151
|
-
return ret;
|
152
|
-
}
|
153
|
-
|
154
|
-
var sequencer = function sequencer (map, str) {
|
155
|
-
var exploded = str.split(""), i = 0;
|
156
|
-
exploded = exploded.map(map);
|
157
|
-
return exploded.join("");
|
158
|
-
};
|
159
|
-
|
160
|
-
// custom formatter methods
|
161
|
-
colors.trap = require('./custom/trap');
|
162
|
-
colors.zalgo = require('./custom/zalgo');
|
163
|
-
|
164
|
-
// maps
|
165
|
-
colors.maps = {};
|
166
|
-
colors.maps.america = require('./maps/america');
|
167
|
-
colors.maps.zebra = require('./maps/zebra');
|
168
|
-
colors.maps.rainbow = require('./maps/rainbow');
|
169
|
-
colors.maps.random = require('./maps/random')
|
170
|
-
|
171
|
-
for (var map in colors.maps) {
|
172
|
-
(function(map){
|
173
|
-
colors[map] = function (str) {
|
174
|
-
return sequencer(colors.maps[map], str);
|
175
|
-
}
|
176
|
-
})(map)
|
177
|
-
}
|
178
|
-
|
179
|
-
defineProps(colors, init());
|
1
|
+
/*
|
2
|
+
|
3
|
+
The MIT License (MIT)
|
4
|
+
|
5
|
+
Original Library
|
6
|
+
- Copyright (c) Marak Squires
|
7
|
+
|
8
|
+
Additional functionality
|
9
|
+
- Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
10
|
+
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
13
|
+
in the Software without restriction, including without limitation the rights
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
16
|
+
furnished to do so, subject to the following conditions:
|
17
|
+
|
18
|
+
The above copyright notice and this permission notice shall be included in
|
19
|
+
all copies or substantial portions of the Software.
|
20
|
+
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
27
|
+
THE SOFTWARE.
|
28
|
+
|
29
|
+
*/
|
30
|
+
|
31
|
+
var colors = {};
|
32
|
+
module['exports'] = colors;
|
33
|
+
|
34
|
+
colors.themes = {};
|
35
|
+
|
36
|
+
var ansiStyles = colors.styles = require('./styles');
|
37
|
+
var defineProps = Object.defineProperties;
|
38
|
+
|
39
|
+
colors.supportsColor = require('./system/supports-colors').supportsColor;
|
40
|
+
|
41
|
+
if (typeof colors.enabled === "undefined") {
|
42
|
+
colors.enabled = colors.supportsColor() !== false;
|
43
|
+
}
|
44
|
+
|
45
|
+
colors.stripColors = colors.strip = function(str){
|
46
|
+
return ("" + str).replace(/\x1B\[\d+m/g, '');
|
47
|
+
};
|
48
|
+
|
49
|
+
|
50
|
+
var stylize = colors.stylize = function stylize (str, style) {
|
51
|
+
if (!colors.enabled) {
|
52
|
+
return str+'';
|
53
|
+
}
|
54
|
+
|
55
|
+
return ansiStyles[style].open + str + ansiStyles[style].close;
|
56
|
+
}
|
57
|
+
|
58
|
+
var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
|
59
|
+
var escapeStringRegexp = function (str) {
|
60
|
+
if (typeof str !== 'string') {
|
61
|
+
throw new TypeError('Expected a string');
|
62
|
+
}
|
63
|
+
return str.replace(matchOperatorsRe, '\\$&');
|
64
|
+
}
|
65
|
+
|
66
|
+
function build(_styles) {
|
67
|
+
var builder = function builder() {
|
68
|
+
return applyStyle.apply(builder, arguments);
|
69
|
+
};
|
70
|
+
builder._styles = _styles;
|
71
|
+
// __proto__ is used because we must return a function, but there is
|
72
|
+
// no way to create a function with a different prototype.
|
73
|
+
builder.__proto__ = proto;
|
74
|
+
return builder;
|
75
|
+
}
|
76
|
+
|
77
|
+
var styles = (function () {
|
78
|
+
var ret = {};
|
79
|
+
ansiStyles.grey = ansiStyles.gray;
|
80
|
+
Object.keys(ansiStyles).forEach(function (key) {
|
81
|
+
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
82
|
+
ret[key] = {
|
83
|
+
get: function () {
|
84
|
+
return build(this._styles.concat(key));
|
85
|
+
}
|
86
|
+
};
|
87
|
+
});
|
88
|
+
return ret;
|
89
|
+
})();
|
90
|
+
|
91
|
+
var proto = defineProps(function colors() {}, styles);
|
92
|
+
|
93
|
+
function applyStyle() {
|
94
|
+
var args = arguments;
|
95
|
+
var argsLen = args.length;
|
96
|
+
var str = argsLen !== 0 && String(arguments[0]);
|
97
|
+
if (argsLen > 1) {
|
98
|
+
for (var a = 1; a < argsLen; a++) {
|
99
|
+
str += ' ' + args[a];
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
if (!colors.enabled || !str) {
|
104
|
+
return str;
|
105
|
+
}
|
106
|
+
|
107
|
+
var nestedStyles = this._styles;
|
108
|
+
|
109
|
+
var i = nestedStyles.length;
|
110
|
+
while (i--) {
|
111
|
+
var code = ansiStyles[nestedStyles[i]];
|
112
|
+
str = code.open + str.replace(code.closeRe, code.open) + code.close;
|
113
|
+
}
|
114
|
+
|
115
|
+
return str;
|
116
|
+
}
|
117
|
+
|
118
|
+
colors.setTheme = function (theme) {
|
119
|
+
if (typeof theme === 'string') {
|
120
|
+
console.log('colors.setTheme now only accepts an object, not a string. ' +
|
121
|
+
'If you are trying to set a theme from a file, it is now your (the caller\'s) responsibility to require the file. ' +
|
122
|
+
'The old syntax looked like colors.setTheme(__dirname + \'/../themes/generic-logging.js\'); ' +
|
123
|
+
'The new syntax looks like colors.setTheme(require(__dirname + \'/../themes/generic-logging.js\'));');
|
124
|
+
return;
|
125
|
+
}
|
126
|
+
for (var style in theme) {
|
127
|
+
(function(style){
|
128
|
+
colors[style] = function(str){
|
129
|
+
if (typeof theme[style] === 'object'){
|
130
|
+
var out = str;
|
131
|
+
for (var i in theme[style]){
|
132
|
+
out = colors[theme[style][i]](out);
|
133
|
+
}
|
134
|
+
return out;
|
135
|
+
}
|
136
|
+
return colors[theme[style]](str);
|
137
|
+
};
|
138
|
+
})(style)
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
142
|
+
function init() {
|
143
|
+
var ret = {};
|
144
|
+
Object.keys(styles).forEach(function (name) {
|
145
|
+
ret[name] = {
|
146
|
+
get: function () {
|
147
|
+
return build([name]);
|
148
|
+
}
|
149
|
+
};
|
150
|
+
});
|
151
|
+
return ret;
|
152
|
+
}
|
153
|
+
|
154
|
+
var sequencer = function sequencer (map, str) {
|
155
|
+
var exploded = str.split(""), i = 0;
|
156
|
+
exploded = exploded.map(map);
|
157
|
+
return exploded.join("");
|
158
|
+
};
|
159
|
+
|
160
|
+
// custom formatter methods
|
161
|
+
colors.trap = require('./custom/trap');
|
162
|
+
colors.zalgo = require('./custom/zalgo');
|
163
|
+
|
164
|
+
// maps
|
165
|
+
colors.maps = {};
|
166
|
+
colors.maps.america = require('./maps/america');
|
167
|
+
colors.maps.zebra = require('./maps/zebra');
|
168
|
+
colors.maps.rainbow = require('./maps/rainbow');
|
169
|
+
colors.maps.random = require('./maps/random')
|
170
|
+
|
171
|
+
for (var map in colors.maps) {
|
172
|
+
(function(map){
|
173
|
+
colors[map] = function (str) {
|
174
|
+
return sequencer(colors.maps[map], str);
|
175
|
+
}
|
176
|
+
})(map)
|
177
|
+
}
|
178
|
+
|
179
|
+
defineProps(colors, init());
|