forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlist.js
139 lines (122 loc) · 3.6 KB
/
list.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var _ = require('underscore');
var sprintf = require('sprintf-js').sprintf;
var h = require('../helper');
var chalk = require('../chalk');
var icon = require('../icon');
var log = require('../log');
var core = require('../core');
var session = require('../session');
var cmd = {
command: 'list [keyword]',
desc: 'list problems',
builder: {
keyword: {
type: 'string',
default: '',
describe: 'Filter problems by keyword'
},
query: {
alias: 'q',
type: 'string',
default: '',
describe: 'Filter problems by conditions:\n' +
'e(easy),m(medium),h(hard),d(done),l(locked),s(starred)\n' +
'Uppercase means negative, e.g. D(not done)'
},
stat: {
alias: 's',
type: 'boolean',
default: false,
describe: 'Show problems statistics'
},
tag: {
alias: 't',
type: 'string',
default: '',
describe: 'Filter problems by tags'
}
}
};
function byLevel(x, q) {
return x.level[0].toLowerCase() === q.toLowerCase();
}
function byStateAC(x, q) {
return x.state === 'ac';
}
function byLocked(x, q) {
return x.locked;
}
function byStarred(x, q) {
return x.starred;
}
var QUERY_HANDLERS = {
e: byLevel,
E: _.negate(byLevel),
m: byLevel,
M: _.negate(byLevel),
h: byLevel,
H: _.negate(byLevel),
l: byLocked,
L: _.negate(byLocked),
d: byStateAC,
D: _.negate(byStateAC),
s: byStarred,
S: _.negate(byStarred)
};
cmd.handler = function(argv) {
session.argv = argv;
core.getProblems(function(e, problems) {
if (e) return log.fail(e);
var all = problems.length;
if (argv.query) {
argv.query.split('').forEach(function(q) {
var f = QUERY_HANDLERS[q];
if (!f) return;
problems = _.filter(problems, _.partial(f, _, q));
});
}
if (argv.tag) {
var tag = argv.tag.toLowerCase();
// TODO: fill company/tags in problems
problems = _.filter(problems, function(x) {
return x.category === tag ||
(_.isArray(x.companies) && x.companies.indexOf(tag) !== -1) ||
(_.isArray(x.tags) && x.tags.indexOf(tag) !== -1);
});
}
var word = String(argv.keyword).toLowerCase();
if (word) {
if (word.endsWith(word.substr(-1).repeat(6))) {
log.warn('Hmmm...you might need a new keyboard?');
}
problems = _.filter(problems, function(x) {
return x.name.toLowerCase().indexOf(word) !== -1;
});
}
var stat = {locked: 0, starred: 0};
problems.forEach(function(problem) {
stat[problem.level] = (stat[problem.level] || 0) + 1;
stat[problem.state] = (stat[problem.state] || 0) + 1;
if (problem.locked) ++stat.locked;
if (problem.starred) ++stat.starred;
log.info(sprintf('%s %s %s [%3d] %-60s %-6s (%.2f %%)',
(problem.starred ? chalk.yellow(icon.like) : icon.none),
(problem.locked ? chalk.red(icon.lock) : icon.none),
h.prettyState(problem.state),
problem.id,
problem.name,
problem.level,
problem.percent));
});
if (argv.stat) {
log.info();
log.info(sprintf(' All: %-9d Listed: %-9d', all, problems.length));
log.info(sprintf(' Locked: %-9d Starred: %-9d', stat.locked, stat.starred));
log.info(sprintf(' Accept: %-9d Not-AC: %-9d New: %-9d',
(stat.ac || 0), (stat.notac || 0), (stat.None || 0)));
log.info(sprintf(' Easy: %-9d Medium: %-9d Hard: %-9d',
(stat.Easy || 0), (stat.Medium || 0), (stat.Hard || 0)));
}
});
};
module.exports = cmd;