Skip to content

Commit 37c26bb

Browse files
committed
Show tag/lang badges in show -x.
Signed-off-by: Eric Wang <[email protected]>
1 parent 24009d6 commit 37c26bb

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

Diff for: lib/chalk.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,25 @@ chalk.wrap = function(pre, post) {
5050
return f;
5151
};
5252

53+
function bgName(s) { return 'bg' + s[0].toUpperCase() + s.substr(1); }
54+
5355
chalk.init = function() {
54-
var h = require('./helper');
55-
_.each(h.getCodeDirData('colors'), function(f) {
56-
chalk.themes[f.name] = _.mapObject(f.data, function(v, k) {
57-
return chalk.use256 ? style.color.ansi256.hex(v) : style.color.ansi.hex(v);
56+
_.each(require('./helper').getCodeDirData('colors'), function(f) {
57+
var o = {};
58+
_.pairs(f.data).forEach(function(x) {
59+
var k = x[0];
60+
var v = x[1];
61+
var bgK = bgName(k);
62+
63+
if (chalk.use256) {
64+
o[k] = style.color.ansi256.hex(v);
65+
o[bgK] = style.bgColor.ansi256.hex(v);
66+
} else {
67+
o[k] = style.color.ansi.hex(v);
68+
o[bgK] = style.bgColor.ansi.hex(v);
69+
}
5870
});
71+
chalk.themes[f.name] = o;
5972
});
6073

6174
_.chain(['black', 'blue', 'cyan', 'gray', 'green', 'magenta', 'red', 'white', 'yellow'])
@@ -66,6 +79,13 @@ chalk.init = function() {
6679
},
6780
configurable: true
6881
});
82+
var bgcolor = bgName(color);
83+
Object.defineProperty(chalk, bgcolor, {
84+
get: function() {
85+
return chalk.wrap(chalk.theme[bgcolor], style.bgColor.close);
86+
},
87+
configurable: true
88+
});
6989
});
7090

7191
_.chain(['bold', 'dim', 'italic', 'inverse', 'strikethrough', 'underline'])

Diff for: lib/commands/show.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ function genFileName(problem, lang) {
7575
}
7676

7777
function showProblem(problem, argv) {
78+
var taglist = [problem.category]
79+
.concat(problem.companies || [])
80+
.concat(problem.tags || [])
81+
.map(function(x) { return h.badge(x, 'blue'); })
82+
.join(' ');
7883
var langlist = problem.templates
79-
.map(function(x) { return x.value; })
84+
.map(function(x) { return h.badge(x.value, 'yellow'); })
8085
.sort()
81-
.join(', ');
86+
.join(' ');
8287

8388
var code;
8489
var needcode = argv.gen || argv.codeonly;
@@ -122,6 +127,12 @@ function showProblem(problem, argv) {
122127
(problem.starred ? chalk.yellow(icon.like) : icon.none));
123128
log.info();
124129
log.info(chalk.underline(problem.link));
130+
if (argv.extra) {
131+
log.info();
132+
log.info('Tags: ' + taglist);
133+
log.info();
134+
log.info('Langs: ' + langlist);
135+
}
125136

126137
log.info();
127138
log.printf('* %s', problem.category);
@@ -135,7 +146,6 @@ function showProblem(problem, argv) {
135146
log.printf('* Total Submissions: %s', problem.totalSubmit);
136147
if (problem.testable && problem.testcase)
137148
log.printf('* Testcase Example: %s', chalk.yellow(util.inspect(problem.testcase)));
138-
log.printf('* Avail Languages: %s', langlist);
139149

140150
log.info();
141151
log.info(problem.desc);

Diff for: lib/helper.js

+18
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,22 @@ h.spin = function(s) {
252252
return ora(require('./chalk').gray(s)).start();
253253
};
254254

255+
var COLORS = {
256+
blue: {fg: 'white', bg: 'bgBlue'},
257+
gray: {fg: 'white', bg: 'bgGray'},
258+
green: {fg: 'black', bg: 'bgGreen'},
259+
magenta: {fg: 'white', bg: 'bgMagenta'},
260+
red: {fg: 'white', bg: 'bgRed'},
261+
yellow: {fg: 'black', bg: 'bgYellow'}
262+
};
263+
h.badge = function(s, color) {
264+
s = ' ' + s + ' ';
265+
if (color === 'random')
266+
color = _.chain(COLORS).keys().sample().value();
267+
var c = COLORS[color || 'blue'];
268+
269+
var chalk = require('./chalk');
270+
return chalk[c.fg][c.bg](s);
271+
};
272+
255273
module.exports = h;

0 commit comments

Comments
 (0)