-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoolors.js
101 lines (94 loc) · 2.55 KB
/
coolors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var supportsColor = require('supports-color');
var ansi = require('ansi-styles');
// Use ansi-styles to build function that returns coloured strings
var plugins = {};
Object.keys(ansi).forEach(function(filter){
plugins[filter] = function(msg){
return ansi[filter].open + msg + ansi[filter].close;
}
});
// Map default styles
var styles = {
decorators: {
bold: plugins.bold,
dim: plugins.dim,
italic: plugins.italic,
underline: plugins.underline,
inverse: plugins.inverse,
hidden: plugins.hidden,
strikethrough: plugins.strikethrough
},
text: {
white: plugins.white,
gray: plugins.gray,
black: plugins.black,
red: plugins.red,
green: plugins.green,
yellow: plugins.yellow,
blue: plugins.blue,
magenta: plugins.magenta,
cyan: plugins.cyan
},
background: {
white: plugins.bgWhite,
black: plugins.bgBlack,
red: plugins.bgRed,
green: plugins.bgGreen,
yellow: plugins.bgYellow,
blue: plugins.bgBlue,
magenta: plugins.bgMagenta,
cyan: plugins.bgCyan
}
};
// Store name of styles for method availableStyles
var stylesKeys = {};
Object.keys(styles).forEach(function(style){
stylesKeys[style] = Object.keys(styles[style]);
});
/**
* CREATE A COOL LOG
*
* @param {String} msg
* @param {String|Object} config
*/
function coolors(msg, config){
if(supportsColor) {
switch (typeof config) {
case 'string':
if(plugins[config]){
msg = plugins[config](msg);
}
break;
case 'object':
var decorators = Object.keys(styles.decorators);
decorators.forEach(function (decorator) {
if (config[decorator]) {
msg = styles.decorators[decorator](msg);
}
});
['text', 'background'].forEach(function (option) {
if (config[option] && styles[option][config[option]]) {
msg = styles[option][config[option]](msg);
}
});
break;
}
}
return msg;
}
/**
* EXTEND COOLORS
*
* @param {String} name Plugin name
* @param {Function} fn Function for process
*/
coolors.addPlugin = function(name, fn){
plugins[name] = fn;
};
/**
* GET STYLES THAT WE CAN USE
*/
coolors.availableStyles = function(){
return stylesKeys;
};
module.exports = coolors;