forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathchalk.js
77 lines (67 loc) · 1.79 KB
/
chalk.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
var _ = require('underscore');
var style = require('ansi-styles');
var supportsColor = require('supports-color');
var chalk = {
enabled: supportsColor,
use256: supportsColor && supportsColor.has256,
themes: {},
theme: {}
};
var pres = [];
var posts = [];
var DEFAULT = {
black: "#000000",
blue: "#0000ff",
cyan: "#00ffff",
gray: "#999999",
green: "#00ff00",
magenta: "#ff00ff",
red: "#ff0000",
white: "#ffffff",
yellow: "#ffff00"
};
chalk.setTheme = function(name) {
var theme = this.themes[name] || this.themes.default || {};
this.theme = _.extendOwn(DEFAULT, theme);
};
chalk.print = function(s) {
s = this.enabled ? pres.join('') + s + posts.join('') : s;
pres.length = posts.length = 0;
return s;
};
chalk.wrap = function(pre, post) {
pres.push(pre);
posts.unshift(post);
var f = function(s) {
return chalk.print(s);
};
f.__proto__ = chalk;
return f;
};
chalk.init = function() {
var h = require('./helper');
_.each(h.getDirData(['colors']), function(f) {
chalk.themes[f.name] = _.mapObject(f.data, function(v, k) {
return chalk.use256 ? style.color.ansi256.hex(v) : style.color.ansi.hex(v);
});
});
_.chain(['black', 'blue', 'cyan', 'gray', 'green', 'magenta', 'red', 'white', 'yellow'])
.each(function(color) {
Object.defineProperty(chalk, color, {
get: function() {
return chalk.wrap(chalk.theme[color], style.color.close);
},
configurable: true
});
});
_.chain(['bold', 'dim', 'italic', 'inverse', 'strikethrough', 'underline'])
.each(function(modifier) {
Object.defineProperty(chalk, modifier, {
get: function() {
return chalk.wrap(style[modifier].open, style[modifier].close);
},
configurable: true
});
});
};
module.exports = chalk;