forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlist.js
159 lines (140 loc) · 4.52 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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]',
aliases: ['ls'],
desc: 'List questions',
builder: function(yargs) {
return yargs
.option('q', {
alias: 'query',
type: 'string',
default: '',
describe: [
'Filter questions by condition:',
'Uppercase means negative',
'e = easy E = m+h',
'm = medium M = e+h',
'h = hard H = e+m',
'd = done D = not done',
'l = locked L = non locked',
's = starred S = not starred'
].join('\n')
})
.option('s', {
alias: 'stat',
type: 'boolean',
default: false,
describe: 'Show statistics of listed questions'
})
.option('t', {
alias: 'tag',
type: 'array',
default: [],
describe: 'Filter questions by tag'
})
.positional('keyword', {
type: 'string',
default: '',
describe: 'Filter questions by keyword'
})
.example(chalk.yellow('leetcode list'), 'List all questions')
.example(chalk.yellow('leetcode list array'), 'List questions that has "array" in name')
.example(chalk.yellow('leetcode list -q eD'), 'List questions that with easy level and not done')
.example(chalk.yellow('leetcode list -t google'), 'List questions from Google company (require plugin)')
.example(chalk.yellow('leetcode list -t stack'), 'List questions realted to stack (require plugin)');
}
};
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)
};
function hasTag(o, tag) {
return _.isArray(o) &&
_.some(o, function(x) { return x.indexOf(tag.toLowerCase()) >= 0; });
}
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));
});
}
argv.tag.forEach(function(tag) {
// TODO: fill company/tags in problems
problems = _.filter(problems, function(p) {
return p.category === tag ||
hasTag(p.companies, tag) ||
hasTag(p.tags, tag);
});
});
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) >= 0;
});
}
var stat = {};
var KEYS = ['locked', 'starred', 'ac', 'notac', 'None', 'Easy', 'Medium', 'Hard'];
KEYS.forEach(function(x) { stat[x] = 0; });
problems = _.sortBy(problems, function(x) { return -x.id; });
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.printf('%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,
h.prettyLevel(sprintf('%-6s', problem.level)),
problem.percent);
});
if (argv.stat) {
log.info();
log.printf(' All: %-9d Listed: %-9d', all, problems.length);
log.printf(' Locked: %-9d Starred: %-9d', stat.locked, stat.starred);
log.printf(' Accept: %-9d Not-AC: %-9d New: %-9d', stat.ac, stat.notac, stat.None);
log.printf(' Easy: %-9d Medium: %-9d Hard: %-9d', stat.Easy, stat.Medium, stat.Hard);
}
});
};
module.exports = cmd;