forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlist.js
192 lines (182 loc) · 6.69 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'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 file = require('../file');
var session = require('../session');
var path = require('path');
var fs = require('fs');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
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('c', {
alias: 'csv',
type: 'boolean',
default: false,
describe: 'Output csv file of problem list'
})
.option('o', {
alias: 'output',
type: 'string',
default: path.join(file.homeDir(), 'out.csv'),
describe: 'Target directory for output file'
})
.option('z', {
alias: 'zh',
type: 'boolean',
default: false,
describe: 'Set output csv as chinese column'
})
.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'
})
.option('p', {
alias: 'time',
type: 'string',
default: "0",
describe: 'Filter questions by selected time period of company questions'
})
.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));
}
//prepare for csvWriter
const csvWriter = createCsvWriter({
path: argv.output,
header: [
{id: 'id', title: argv.z ? "题号" : 'id'},
{id: 'name', title: argv.z ? "标题" : 'name'},
{id: 'level', title: argv.z ? "难度" : 'level'},
{id: 'tags', title: argv.z ? "分类" : 'tags'},
{id: 'companies', title: argv.z ? "公司" :'companies'},
{id: 'companies06', title: argv.z ? "6个月内出现" :'companies 6 months'},
{id: 'companies612', title: argv.z ? "一年内出现" :'companies 6-12 months'},
{id: 'companies1224', title: argv.z ? "两年内出现" :'companies 12-24 months'},
{id: 'percent', title: argv.z ? "接受率" : 'percent'},
{id: 'category', title: argv.z ? "类别" : 'category'},
]
});
const stat = {};
for (let x of ['locked', 'starred', 'ac', 'notac', 'None', 'Easy', 'Medium', 'Hard']) stat[x] = 0;
problems = _.sortBy(problems, x => -x.fid);
var data = [];
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;
var categoryTrans = {
"algorithms":"算法",
"database":"数据库",
"shell":"命令行"
}
//build data for csvWriter
if(argv.csv){
if(problem.companyStats!=null){
data.push( {id:problem.fid,
name: '=HYPERLINK("'+problem.link+'","'+problem.name+'")',
level: problem.level,
tags: problem.tags,
companies: problem.companies,
companies06: problem.companyStats["1"].join(','),
companies612: problem.companyStats["2"].join(','),
companies1224: problem.companyStats["3"].join(','),
percent: problem.percent.toFixed(2),
category: argv.z ? categoryTrans[problem.category] : problem.category
});
}
else{
data.push( {id:problem.fid,
name: '=HYPERLINK("'+problem.link+'","'+problem.name+'")',
level: problem.level,
tags: problem.tags,
companies: problem.companies,
percent: problem.percent.toFixed(2),
category: argv.z ? categoryTrans[problem.category] : problem.category
});
}
}
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(' | ')));
}
}
//write data to csv
if(argv.csv){
csvWriter
.writeRecords(data)
.then(()=> {
fs.writeFileSync(argv.output, '\ufeff' + fs.readFileSync(argv.output));
console.log('The CSV file was written successfully, output path: '+argv.output);
});
}
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;