Skip to content

Commit ccd8b4d

Browse files
committed
Refactor: move themes to colors folder.
Signed-off-by: Eric Wang <[email protected]>
1 parent 9a0fa89 commit ccd8b4d

File tree

4 files changed

+61
-53
lines changed

4 files changed

+61
-53
lines changed

colors/dark.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"black": "#000000",
3+
"blue": "#000099",
4+
"cyan": "#009999",
5+
"green": "#009900",
6+
"magenta": "#990099",
7+
"red": "#990000",
8+
"white": "#ffffff",
9+
"yellow": "#999900"
10+
}

colors/default.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"black": "#000000",
3+
"blue": "#0000ff",
4+
"cyan": "#00ffff",
5+
"green": "#00ff00",
6+
"magenta": "#ff00ff",
7+
"red": "#ff0000",
8+
"white": "#ffffff",
9+
"yellow": "#ffff00"
10+
}

colors/pink.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"black": "#000000",
3+
"blue": "#0000ff",
4+
"cyan": "#00ffff",
5+
"green": "#ff1493",
6+
"magenta": "#ff00ff",
7+
"red": "#dc143c",
8+
"white": "#ffffff",
9+
"yellow": "#ff4500"
10+
}

lib/chalk.js

+31-53
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,17 @@
11
var _ = require('underscore');
22
var style = require('ansi-styles');
33

4-
function rgb(r, g, b) {
5-
return style.color.ansi256.rgb(r, g, b);
6-
}
7-
8-
var THEMES = {
9-
'default': {
10-
black: rgb(0, 0, 0),
11-
blue: rgb(0, 0, 255),
12-
cyan: rgb(0, 255, 255),
13-
green: rgb(0, 255, 0),
14-
magenta: rgb(255, 0, 255),
15-
red: rgb(255, 0, 0),
16-
white: rgb(255, 255, 255),
17-
yellow: rgb(255, 255, 0)
18-
},
19-
'dark': {
20-
black: rgb(0, 0, 0),
21-
blue: rgb(0, 0, 153),
22-
cyan: rgb(0, 153, 153),
23-
green: rgb(0, 153, 0),
24-
magenta: rgb(153, 0, 153),
25-
red: rgb(153, 0, 0),
26-
white: rgb(255, 255, 255),
27-
yellow: rgb(153, 153, 0)
28-
},
29-
'pink': {
30-
black: rgb(0, 0, 0),
31-
blue: rgb(0, 0, 153),
32-
cyan: rgb(0, 153, 153),
33-
green: rgb(255, 20, 147),
34-
magenta: rgb(153, 0, 153),
35-
red: rgb(220, 20, 60),
36-
white: rgb(255, 255, 255),
37-
yellow: rgb(255, 69, 0)
38-
}
39-
};
40-
414
var chalk = {
425
enabled: true,
43-
theme: THEMES.default
6+
themes: {},
7+
theme: {}
448
};
459

4610
var pres = [];
4711
var posts = [];
4812

4913
chalk.setTheme = function(name) {
50-
this.theme = THEMES[name] || THEMES.default;
14+
this.theme = this.themes[name] || this.themes.default || {};
5115
};
5216

5317
chalk.print = function(s) {
@@ -66,22 +30,36 @@ chalk.wrap = function(pre, post) {
6630
return f;
6731
};
6832

69-
_.chain(['black', 'blue', 'cyan', 'green', 'magenta', 'red', 'white', 'yellow'])
70-
.each(function(color) {
71-
Object.defineProperty(chalk, color, {
72-
get: function() {
73-
return chalk.wrap(chalk.theme[color], style.color.close);
74-
}
33+
chalk.init = function() {
34+
var fs = require('fs');
35+
var path = require('path');
36+
37+
var dir = path.join(__dirname, '..', 'colors');
38+
_.each(fs.readdirSync(dir), function(f) {
39+
var theme = JSON.parse(fs.readFileSync(path.join(dir, f)));
40+
chalk.themes[path.basename(f, '.json')] = _.mapObject(theme, function(v, k) {
41+
return style.color.ansi256.hex(v);
42+
});
7543
});
76-
});
7744

78-
_.chain(['bold', 'dim', 'italic', 'inverse', 'strikethrough', 'underline'])
79-
.each(function(modifier) {
80-
Object.defineProperty(chalk, modifier, {
81-
get: function() {
82-
return chalk.wrap(style[modifier].open, style[modifier].close);
83-
}
45+
_.chain(['black', 'blue', 'cyan', 'green', 'magenta', 'red', 'white', 'yellow'])
46+
.each(function(color) {
47+
Object.defineProperty(chalk, color, {
48+
get: function() {
49+
return chalk.wrap(chalk.theme[color], style.color.close);
50+
}
51+
});
8452
});
85-
});
8653

54+
_.chain(['bold', 'dim', 'italic', 'inverse', 'strikethrough', 'underline'])
55+
.each(function(modifier) {
56+
Object.defineProperty(chalk, modifier, {
57+
get: function() {
58+
return chalk.wrap(style[modifier].open, style[modifier].close);
59+
}
60+
});
61+
});
62+
};
63+
64+
chalk.init();
8765
module.exports = chalk;

0 commit comments

Comments
 (0)