forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathlist.js
108 lines (97 loc) · 3.61 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
'use strict';
var _ = require('underscore');
var h = require('../helper');
var chalk = require('../chalk');
var icon = require('../icon');
var log = require('../log');
var core = require('../core');
var session = require('../session');
const cmd = {
command: 'list [keyword]',
aliases: ['ls'],
desc: 'List questions',
builder: function(yargs) {
return yargs
.option('q', core.filters.query)
.option('s', {
alias: 'stat',
type: 'boolean',
default: false,
describe: 'Show statistics of listed questions'
})
.option('t', core.filters.tag)
.option('x', {
alias: 'extra',
type: 'boolean',
default: false,
describe: 'Show extra details: category, companies, tags.'
})
.positional('keyword', {
type: 'string',
default: '',
describe: 'Filter questions by keyword'
})
.example(chalk.yellow('leetcode list'), 'List all questions')
.example(chalk.yellow('leetcode list -x'), 'Show extra info of questions, e.g. tags')
.example('', '')
.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)');
}
};
cmd.handler = function(argv) {
session.argv = argv;
core.filterProblems(argv, function(e, problems) {
if (e) return log.fail(e);
const word = argv.keyword.toLowerCase();
if (word) {
if (word.endsWith(word.substr(-1).repeat(6))) {
log.warn('Hmmm...you might need a new keyboard?');
}
problems = problems.filter(x => x.name.toLowerCase().includes(word));
}
const stat = {};
for (let x of ['locked', 'starred', 'ac', 'notac', 'None', 'Easy', 'Medium', 'Hard']) stat[x] = 0;
problems = _.sortBy(problems, x => -x.fid);
for (let problem of problems) {
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 [%=4s] %-60s %-6s (%s %%)',
(problem.starred ? chalk.yellow(icon.like) : icon.empty),
(problem.locked ? chalk.red(icon.lock) : icon.nolock),
h.prettyState(problem.state),
problem.fid,
problem.name,
h.prettyLevel(problem.level),
problem.percent.toFixed(2));
if (argv.extra) {
let badges = [problem.category];
badges = badges.concat(problem.companies || []);
badges = badges.concat(problem.tags || []);
let buf = [];
let len = 0;
for (let x of badges) {
if (len + x.length + 3 >= 60) {
log.printf('%12s%s', ' ', chalk.gray(buf.join(' | ')));
buf = [];
len = 0;
}
buf.push(x);
len += x.length + 3;
}
if (buf.length > 0)
log.printf('%12s%s', ' ', chalk.gray(buf.join(' | ')));
}
}
if (argv.stat) {
log.info();
log.printf(' Listed: %-9s Locked: %-9s Starred: %-9s', problems.length, stat.locked, stat.starred);
log.printf(' Accept: %-9s Not-AC: %-9s Remain: %-9s', stat.ac, stat.notac, stat.None);
log.printf(' Easy: %-9s Medium: %-9s Hard: %-9s', stat.Easy, stat.Medium, stat.Hard);
}
});
};
module.exports = cmd;