Skip to content

Commit a72ca18

Browse files
committed
refs #35: refactor to support icon themes.
Signed-off-by: Eric Wang <[email protected]>
1 parent b0d8189 commit a72ca18

File tree

8 files changed

+55
-9
lines changed

8 files changed

+55
-9
lines changed

icons/default.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"yes": "",
3+
"no": "",
4+
"like": "",
5+
"unlike": "",
6+
"lock": "🔒",
7+
"none": " "
8+
}

lib/cli.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ var _ = require('underscore');
66

77
var chalk = require('./chalk');
88
var config = require('./config');
9+
var icon = require('./icon');
910

1011
// We are expecting a tier configuration like:
1112
// global config < local config < cli params
1213
// Color is a tricky one so we manually handle it here.
13-
function setColorMode() {
14+
function setColor() {
1415
// FIXME: delete this hack when supports-color handles it well.
1516
if (process.env.TERM_PROGRAM === 'iTerm.app') chalk.use256 = true;
1617

@@ -19,6 +20,11 @@ function setColorMode() {
1920
chalk.setTheme(config.COLOR_THEME);
2021
}
2122

23+
function setIcon() {
24+
icon.init();
25+
icon.setTheme(config.ICON_THEME);
26+
}
27+
2228
function setLogLevel() {
2329
var level = log.levels.INFO;
2430
if (process.argv.indexOf('-v') >= 0) level = log.levels.DEBUG;
@@ -71,7 +77,8 @@ cli.run = function() {
7177
config.init();
7278

7379
checkCache();
74-
setColorMode();
80+
setColor();
81+
setIcon();
7582
setLogLevel();
7683

7784
process.stdout.on('error', function(e) {

lib/commands/list.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var log = require('loglevel');
55
var chalk = require('../chalk');
66
var core = require('../core');
77
var h = require('../helper');
8+
var icon = require('../icon');
89

910
var cmd = {
1011
command: 'list [keyword]',
@@ -96,8 +97,8 @@ cmd.handler = function(argv) {
9697
if (problem.starred) ++stat.starred;
9798

9899
log.info(sprintf('%s %s %s [%3d] %-60s %-6s (%.2f %%)',
99-
(problem.starred ? chalk.yellow('★') : ' '),
100-
(problem.locked ? '🔒' : ' '),
100+
(problem.starred ? chalk.yellow(icon.like) : icon.none),
101+
(problem.locked ? chalk.red(icon.lock) : icon.none),
101102
h.prettyState(problem.state),
102103
problem.id,
103104
problem.name,

lib/commands/show.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var chalk = require('../chalk');
99
var config = require('../config');
1010
var core = require('../core');
1111
var h = require('../helper');
12+
var icon = require('../icon');
1213

1314
var cmd = {
1415
command: 'show <keyword>',
@@ -79,7 +80,7 @@ cmd.handler = function(argv) {
7980
log.info(sprintf('[%d] %s %s\t%s\n',
8081
problem.id,
8182
problem.name,
82-
(problem.starred ? chalk.yellow('★') : ' '),
83+
(problem.starred ? chalk.yellow(icon.like) : ' '),
8384
fileinfo));
8485
log.info(sprintf('%s\n', chalk.underline(problem.link)));
8586
log.info(sprintf('* %s (%.2f%%)', problem.level, problem.percent));

lib/commands/star.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var sprintf = require('sprintf-js').sprintf;
33

44
var chalk = require('../chalk');
55
var core = require('../core');
6+
var icon = require('../icon');
67

78
var cmd = {
89
command: 'star <keyword>',
@@ -27,7 +28,7 @@ cmd.handler = function(argv) {
2728
log.info(sprintf('[%d] %s %s',
2829
problem.id,
2930
problem.name,
30-
chalk.yellow(starred ? '★' : '☆')));
31+
chalk.yellow(starred ? icon.like : icon.unlike)));
3132

3233
core.updateProblem(problem, {starred: starred});
3334
});

lib/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ var DEFAULT_CONFIG = {
1818

1919
// but you will want change these
2020
LANG: 'cpp', // avail: [c,cpp,csharp,golang,java,javascript,python,ruby,swift]
21+
AUTO_LOGIN: false,
2122
USE_COLOR: true,
2223
COLOR_THEME: 'default',
23-
AUTO_LOGIN: false,
24+
ICON_THEME: 'default',
2425
MAX_WORKERS: 10
2526
};
2627

lib/helper.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ h.prettyState = function(state) {
1515

1616
h.prettyText = function(text, yesNo) {
1717
var chalk = require('./chalk');
18+
var icon = require('./icon');
1819
switch (yesNo) {
19-
case true: return chalk.green('✔' + text);
20-
case false: return chalk.red('✘' + text);
20+
case true: return chalk.green(icon.yes + text);
21+
case false: return chalk.red(icon.no + text);
2122
default: return text;
2223
}
2324
};

lib/icon.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var _ = require('underscore');
2+
3+
var icons = {
4+
yes: '✔',
5+
no: '✘',
6+
like: '★',
7+
unlike: '☆',
8+
lock: '🔒',
9+
none: ' ',
10+
11+
themes: []
12+
};
13+
14+
icons.setTheme = function(name) {
15+
var theme = this.themes[name] || this.themes.default || {};
16+
_.extendOwn(this, theme);
17+
};
18+
19+
icons.init = function() {
20+
var h = require('./helper');
21+
_.each(h.getDirData('icons'), function(f) {
22+
icons.themes[f.name] = f.data;
23+
});
24+
};
25+
26+
module.exports = icons;

0 commit comments

Comments
 (0)