Skip to content

Commit 69b91aa

Browse files
committed
feat: show formatted problem description
1 parent fa6736a commit 69b91aa

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/commands/show.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ function genFileName(problem, opts) {
9696
}
9797

9898
function showProblem(problem, argv) {
99+
const path = require('path');
99100
const taglist = [problem.category]
100101
.concat(problem.companies || [])
101102
.concat(problem.tags || [])
102-
.map(x => h.badge(x, 'blue'))
103+
.map(x => h.badge(x, 'gray'))
103104
.join(' ');
104105
const langlist = problem.templates
105106
.map(x => h.badge(x.value, 'yellow'))
@@ -169,11 +170,13 @@ function showProblem(problem, argv) {
169170
if (problem.totalSubmit)
170171
log.printf('* Total Submissions: %s', problem.totalSubmit);
171172
if (problem.testable && problem.testcase)
173+
problem.testcase = problem.testcase.replace(/\n/g, ' ')
172174
log.printf('* Testcase Example: %s', chalk.yellow(util.inspect(problem.testcase)));
173175
if (filename)
174-
log.printf('* Source Code: %s', chalk.yellow.underline(filename));
176+
log.printf('* Source Code: %s', chalk.gray.underline(path.basename(filename)));
175177

176178
log.info();
179+
problem = core.formatProblem(problem, {lang: argv.lang});
177180
log.info(problem.desc);
178181
}
179182

lib/core.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ core.getProblem = function(keyword, needTranslation, cb) {
8989
this.getProblems(needTranslation, function(e, problems) {
9090
if (e) return cb(e);
9191

92-
keyword = Number(keyword) || keyword;
92+
keyword = Number(keyword) || keyword;
9393
const metaFid = file.exist(keyword) ? Number(file.meta(keyword).id) : NaN;
9494
const problem = problems.find(function(x) {
9595
return x.fid + '' === keyword + '' || x.fid + '' === metaFid + '' || x.name === keyword || x.slug === keyword;
@@ -135,4 +135,17 @@ core.exportProblem = function(problem, opts) {
135135
return file.render(opts.tpl, data);
136136
};
137137

138+
core.formatProblem = function(problem, opts) {
139+
const data = _.extend({}, problem);
140+
if (!data.lang) data.lang = opts.lang;
141+
data.comment = h.langToCommentStyle(data.lang);
142+
let desc = data.desc;
143+
desc = desc.replace(/<\/sup>/gm, '').replace(/<sup>/gm, '^');
144+
desc = he.decode(cheerio.load(desc).root().text());
145+
desc = desc.replace(/\r\n/g, '\n').replace(/^ /mg, '⁠').replace(/\n\n/g, '\n');
146+
const wrap = require('wordwrap')(79 - data.comment.line.length);
147+
data.desc = wrap(desc);
148+
return data;
149+
};
150+
138151
module.exports = core;

0 commit comments

Comments
 (0)