Skip to content

Commit c6509cb

Browse files
committed
Implement 'show -g -l'
* generate source file with template Signed-off-by: Eric Wang <[email protected]>
1 parent 1f4a209 commit c6509cb

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

lib/commands/show.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,55 @@
1-
var sprintf = require('sprintf-js').sprintf;
1+
var sprintf = require('sprintf-js').sprintf,
2+
_ = require('underscore'),
3+
fs = require('fs');
24

3-
var core = require('../core');
5+
var core = require('../core'),
6+
config = require('../config'),
7+
h = require('../helper');
48

59
var cmd = {
6-
command: 'show <keyword>',
10+
command: 'show <keyword> [--gen|-g] [--lang|-l]',
711
desc: 'Show problem details.',
812
builder: {
913
keyword: {
1014
describe: 'Problem keyword, e.g. name, index, or URI path.'
15+
},
16+
gen: {
17+
alias: 'g',
18+
type: 'boolean',
19+
describe: 'Generate template source file.'
20+
},
21+
lang: {
22+
alias: 'l',
23+
type: 'string',
24+
default: config.LANG,
25+
describe: 'Language to use, used with -g.'
1126
}
1227
}
1328
};
1429

1530
cmd.handler = function(argv) {
31+
1632
core.getProblem(argv.keyword, function(e, problem){
1733
if (e) return console.log('ERROR:', e);
1834

19-
console.log(sprintf('[%d] %s\n', problem.id, problem.name));
35+
var msg = '';
36+
if (argv.gen) {
37+
var template = _.find(problem.templates, function(x){
38+
return x.value == argv.lang;
39+
});
40+
if (!template)
41+
return console.log('Failed to generate source file: unknown language', argv.lang);
42+
43+
var f = problem.key + h.fileExt(argv.lang);
44+
fs.writeFileSync(f, template.defaultCode);
45+
msg = sprintf('(File: %s)', f);
46+
}
47+
48+
console.log(sprintf('[%d] %s\t%s\n', problem.id, problem.name, msg));
2049
console.log(sprintf('%s\n', problem.link));
2150
console.log(sprintf('* %s (%s)', problem.level, problem.percent));
2251
console.log(sprintf('* Total Accepted: %d', problem.total_ac));
23-
console.log(sprintf('* Total Submissions: %d', problem.total_submit));
24-
console.log();
52+
console.log(sprintf('* Total Submissions: %d\n', problem.total_submit));
2553
console.log(problem.desc);
2654
});
2755
}

lib/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
BASE_URL: 'https://leetcode.com',
33
LOGIN_URL: 'https://leetcode.com/accounts/login/',
4-
PROBLEMS_URL: 'https://leetcode.com/problems/'
4+
PROBLEMS_URL: 'https://leetcode.com/problems/',
5+
LANG: 'cpp' // others: c,csharp,golang,java,javascript,python,ruby,swift
56
};

lib/helper.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@ h.prettyState = function(state) {
66
case 'notac': return '✘'; break;
77
default: return ' '; break;
88
};
9-
}
9+
};
10+
11+
h.fileExt = function(lang) {
12+
switch(lang) {
13+
case 'c': return '.c'; break;
14+
case 'cpp': return '.cpp'; break;
15+
case 'csharp': return '.cs'; break;
16+
case 'golang': return '.go'; break;
17+
case 'java': return '.java'; break;
18+
case 'javascript': return '.js'; break;
19+
case 'python': return '.py'; break;
20+
case 'ruby': return '.rb'; break;
21+
case 'swift': return '.swift'; break;
22+
default: return '.raw'; break;
23+
}
24+
};
1025

1126
h.getSetCookieValue = function(resp, key) {
1227
var cookies = resp.headers['set-cookie'];
@@ -18,6 +33,6 @@ h.getSetCookieValue = function(resp, key) {
1833
if (kv[0] == key) return kv[1];
1934
}
2035
}
21-
}
36+
};
2237

2338
module.exports = h;

lib/leetcode_client.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ leetcode_client.getProblems = function(cb) {
4545
});
4646
};
4747

48+
// hacking ;P
49+
var aceCtrl = { init: function(){ return Array.prototype.slice.call(arguments); } };
50+
4851
leetcode_client.getProblem = function(problem, cb) {
4952
request(problem.link, function(e, resp, body){
5053
if (e) return cb(e);
@@ -53,9 +56,14 @@ leetcode_client.getProblem = function(problem, cb) {
5356
var $ = cheerio.load(body);
5457
var info = $('div[class="question-info text-info"] ul li strong');
5558

56-
problem.desc = $('meta[property="og:description"]').attr('content');
5759
problem.total_ac = $(info[0]).text();
5860
problem.total_submit = $(info[1]).text();
61+
problem.desc = $('meta[property="og:description"]').attr('content');
62+
63+
var raw = $('div[ng-controller="AceCtrl as aceCtrl"]').attr('ng-init');
64+
raw = raw.replace(/\n/g,''); // FIXME: might break test cases!
65+
var args = eval(raw);
66+
problem.templates = args[0];
5967

6068
return cb(null, problem);
6169
});

0 commit comments

Comments
 (0)