Skip to content

Commit a992154

Browse files
committed
Support problem details in the source file.
Signed-off-by: Eric Wang <[email protected]>
1 parent a27674d commit a992154

File tree

6 files changed

+112
-15
lines changed

6 files changed

+112
-15
lines changed

lib/commands/show.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ var cmd = {
2525
type: 'string',
2626
default: config.LANG,
2727
describe: 'Program language to use'
28+
},
29+
extra: {
30+
alias: 'x',
31+
type: 'boolean',
32+
default: false,
33+
describe: 'Provide extra problem details in generated file'
2834
}
2935
}
3036
};
@@ -41,10 +47,11 @@ cmd.handler = function(argv) {
4147
if (!template)
4248
return log.fail('Failed to generate source file, ' +
4349
'unknown language "' + argv.lang + '"');
50+
problem.code = template.defaultCode;
4451

45-
var f = problem.key + h.langToExt(argv.lang);
46-
fs.writeFileSync(f, template.defaultCode);
47-
msg = sprintf('(File: %s)', chalk.yellow.underline(f));
52+
var filename = problem.key + h.langToExt(argv.lang);
53+
core.exportProblem(problem, filename, !argv.extra);
54+
msg = sprintf('(File: %s)', chalk.yellow.underline(filename));
4855
}
4956

5057
log.info(sprintf('[%d] %s %s\t%s\n',

lib/commands/submission.js

+31-11
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ var cmd = {
2424
type: 'string',
2525
describe: 'Where to save the submissions',
2626
default: '.'
27+
},
28+
extra: {
29+
alias: 'x',
30+
type: 'boolean',
31+
default: false,
32+
describe: 'Provide extra problem details in submission file'
2733
}
2834
}
2935
};
3036

31-
function getSubmissionDone(e, msg, problem, cb) {
37+
function onTaskDone(e, msg, problem, cb) {
3238
// NOTE: msg color means different purpose:
3339
// - red: error
3440
// - green: accepted, fresh download
@@ -42,12 +48,25 @@ function getSubmissionDone(e, msg, problem, cb) {
4248
if (cb) cb(e);
4349
}
4450

45-
function getSubmission(argv, problem, cb) {
46-
var done = _.partial(getSubmissionDone, _, _, problem, cb);
51+
function onTaskRun(argv, problem, cb) {
52+
var done = _.partial(onTaskDone, _, _, problem, cb);
53+
54+
if (argv.extra) {
55+
// have to get problem details, e.g. problem description.
56+
core.getProblem(problem.id, function(e, problem) {
57+
if (e) return done(e);
58+
59+
exportSubmission(argv, problem, done);
60+
});
61+
} else {
62+
exportSubmission(argv, problem, done);
63+
}
64+
}
4765

66+
function exportSubmission(argv, problem, cb) {
4867
core.getSubmissions(problem, function(e, submissions) {
49-
if (e) return done(e);
50-
if (submissions.length === 0) return done('no submissions?');
68+
if (e) return cb(e);
69+
if (submissions.length === 0) return cb('no submissions?');
5170

5271
// find the latest accepted one
5372
var submission = _.find(submissions, function(x) {
@@ -67,24 +86,25 @@ function getSubmission(argv, problem, cb) {
6786

6887
// skip the existing cached submissions
6988
if (fs.existsSync(filename)) {
70-
return done(null, chalk.underline(filename));
89+
return cb(null, chalk.underline(filename));
7190
}
7291

7392
core.getSubmission(submission, function(e, submission) {
74-
if (e) return done(e);
93+
if (e) return cb(e);
7594

76-
fs.writeFileSync(filename, submission.code);
95+
problem.code = submission.code;
96+
core.exportProblem(problem, filename, !argv.extra);
7797

7898
if (submission.state === 'Accepted')
79-
done(null, chalk.green.underline(filename));
99+
cb(null, chalk.green.underline(filename));
80100
else
81-
done(null, chalk.yellow.underline(filename));
101+
cb(null, chalk.yellow.underline(filename));
82102
});
83103
});
84104
}
85105

86106
cmd.handler = function(argv) {
87-
var doTask = _.partial(getSubmission, argv, _, _);
107+
var doTask = _.partial(onTaskRun, argv, _, _);
88108

89109
if (argv.all) {
90110
core.getProblems(function(e, problems) {

lib/core.js

+31
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var util = require('util');
4+
15
var _ = require('underscore');
26
var log = require('loglevel');
37

48
var cache = require('./cache');
59
var config = require('./config');
610
var client = require('./leetcode_client');
11+
var h = require('./helper');
712

813
var core = {};
914

@@ -91,6 +96,32 @@ core.starProblem = function(problem, starred, cb) {
9196
client.starProblem(problem, starred, cb);
9297
};
9398

99+
core.exportProblem = function(problem, f, codeOnly) {
100+
var output = '';
101+
102+
if (codeOnly) {
103+
output = problem.code;
104+
} else {
105+
var input = h.langToCommentStyle(h.extToLang(f));
106+
// copy problem attrs thus we can render it in template
107+
_.extend(input, problem);
108+
input.percent = input.percent.toFixed(2);
109+
input.testcase = util.inspect(input.testcase || '');
110+
111+
// NOTE: wordwrap internally uses '\n' as EOL, so here we have to
112+
// remove all '\r' in the raw string.
113+
// FIXME: while in template file we still use '\r\n' for the sake
114+
// of windows, is it really necessary?
115+
var wrap = require('wordwrap')(79 - input.commentLine.length);
116+
input.desc = wrap(input.desc.replace(/\r\n/g, '\n')).split('\n');
117+
118+
var tpl = fs.readFileSync(path.resolve(__dirname, '../source.tpl'), 'utf-8');
119+
output = _.template(tpl)(input);
120+
}
121+
122+
fs.writeFileSync(f, output);
123+
};
124+
94125
core.login = function(user, cb) {
95126
var self = this;
96127
client.login(user, function(e, user) {

lib/helper.js

+25
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ h.extToLang = function(fullpath) {
7575
}
7676
};
7777

78+
h.langToCommentStyle = function(lang) {
79+
switch (lang) {
80+
case 'c':
81+
case 'cpp':
82+
case 'csharp':
83+
case 'golang':
84+
case 'java':
85+
case 'javascript':
86+
case 'swift':
87+
default:
88+
return {
89+
commentHeader: '/*',
90+
commentLine: ' *',
91+
commentFooter: ' */'
92+
};
93+
case 'python':
94+
case 'ruby':
95+
return {
96+
commentHeader: '#',
97+
commentLine: '#',
98+
commentFooter: '#'
99+
};
100+
}
101+
};
102+
78103
h.getFileData = function(path) {
79104
return fs.existsSync(path) ? fs.readFileSync(path).toString() : null;
80105
};

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"mocha": "^3.0.2",
4747
"nock": "^8.0.0",
4848
"nyc": "^8.1.0",
49-
"rewire": "^2.5.2"
49+
"rewire": "^2.5.2",
50+
"wordwrap": "^1.0.0"
5051
}
5152
}

source.tpl

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%= commentHeader %>
2+
<%= commentLine %> [<%= id %>] <%= name %>
3+
<%= commentLine %>
4+
<%= commentLine %> <%= link %>
5+
<%= commentLine %>
6+
<%= commentLine %> <%= level %> (<%= percent %>%)
7+
<%= commentLine %> Total Accepted: <%= totalAC %>
8+
<%= commentLine %> Total Submissions: <%= totalSubmit %>
9+
<%= commentLine %> Testcase Example: <%= testcase %>
10+
<%= commentLine %>
11+
<% _.each(desc, function(x) { %><%= commentLine %> <%= x %>
12+
<% }) %><%= commentFooter %>
13+
<%= code %>

0 commit comments

Comments
 (0)