Skip to content

Commit e7ac6a6

Browse files
committedNov 18, 2018
refs skygragon#121: support customized filename.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 167fa23 commit e7ac6a6

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed
 

‎lib/commands/show.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,18 @@ const cmd = {
7676
function genFileName(problem, opts) {
7777
const path = require('path');
7878
const params = [
79-
problem.fid,
80-
problem.slug,
79+
file.fmt(config.file.show, problem),
8180
'',
8281
h.langToExt(opts.lang)
8382
];
8483

85-
// try to use a new filename to avoid overwrite by mistake
86-
let i = 0;
87-
let name;
88-
do {
89-
name = path.join(opts.outdir, params.join('.').replace(/\.+/g, '.'));
90-
params[2] = i++;
91-
} while (fs.existsSync(name));
92-
return name;
84+
// try new name to avoid overwrite by mistake
85+
for (let i = 0; ; ++i) {
86+
const name = path.join(opts.outdir, params.join('.').replace(/\.+/g, '.'));
87+
if (!fs.existsSync(name))
88+
return name;
89+
params[2] = i;
90+
}
9391
}
9492

9593
function showProblem(problem, argv) {
@@ -123,8 +121,8 @@ function showProblem(problem, argv) {
123121

124122
let filename;
125123
if (argv.gen) {
126-
filename = genFileName(problem, argv);
127124
file.mkdir(argv.outdir);
125+
filename = genFileName(problem, argv);
128126
fs.writeFileSync(filename, code);
129127

130128
if (argv.editor !== undefined) {

‎lib/commands/submission.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
22
var fs = require('fs');
3+
var path = require('path');
34

4-
var sprintf = require('sprintf-js').sprintf;
5+
var _ = require('underscore');
56

67
var h = require('../helper');
78
var file = require('../file');
89
var chalk = require('../chalk');
10+
var config = require('../config');
911
var log = require('../log');
1012
var Queue = require('../queue');
1113
var core = require('../core');
@@ -92,13 +94,11 @@ function exportSubmission(problem, argv, cb) {
9294
const submission = submissions.find(x => x.status_display === 'Accepted') || submissions[0];
9395
submission.ac = (submission.status_display === 'Accepted');
9496

95-
const f = sprintf('%s/%d.%s.%s.%s%s',
96-
argv.outdir,
97-
problem.fid,
98-
problem.slug,
99-
submission.id,
100-
submission.ac ? 'ac' : 'notac',
101-
h.langToExt(submission.lang));
97+
const data = _.extend({}, submission, problem);
98+
data.sid = submission.id;
99+
data.ac = submission.ac ? 'ac' : 'notac';
100+
const basename = file.fmt(config.file.submission, data);
101+
const f = path.join(argv.outdir, basename + h.langToExt(submission.lang));
102102

103103
file.mkdir(argv.outdir);
104104
// skip the existing cached submissions

‎lib/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ const DEFAULT_CONFIG = {
5555
editor: 'vim',
5656
lang: 'cpp'
5757
},
58+
file: {
59+
show: '${fid}.${slug}',
60+
submission: '${fid}.${slug}.${sid}.${ac}'
61+
},
5862
color: {
5963
enable: true,
6064
theme: 'default'

‎lib/file.js

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ file.render = function(tpl, data) {
100100
return result;
101101
};
102102

103+
file.fmt = function(format, data) {
104+
return _.template(format)(data);
105+
};
106+
103107
file.metaByName = function(filename) {
104108
const m = {};
105109

0 commit comments

Comments
 (0)
Please sign in to comment.