Skip to content

Commit aaf6451

Browse files
committed
remove sprintf-js
Signed-off-by: Eric Wang <[email protected]>
1 parent bd80b3a commit aaf6451

File tree

15 files changed

+122
-35
lines changed

15 files changed

+122
-35
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
"curly": 0,
1818
"key-spacing": [2, {align: "value"}],
1919
"max-len": [1, 120],
20+
"no-control-regex": 0,
2021
"no-console": 1,
2122
"no-empty": [2, { "allowEmptyCatch": true }],
2223
"no-eval": 1, // we use it on purpose

lib/commands/cache.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22
var _ = require('underscore');
3-
var sprintf = require('sprintf-js').sprintf;
43

54
var h = require('../helper');
65
var chalk = require('../chalk');
76
var log = require('../log');
87
var cache = require('../cache');
98
var session = require('../session');
9+
var sprintf = require('../sprintf');
1010

1111
const cmd = {
1212
command: 'cache [keyword]',
@@ -56,8 +56,8 @@ cmd.handler = function(argv) {
5656
return x;
5757
})
5858
.forEach(function(f) {
59-
log.printf(' %s %8s %s ago',
60-
chalk.green(sprintf('%-60s', f.name)),
59+
log.printf(' %-60s %8s %s ago',
60+
chalk.green(f.name),
6161
h.prettySize(f.size),
6262
h.prettyTime((Date.now() - f.mtime) / 1000));
6363
});

lib/commands/list.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
var _ = require('underscore');
3-
var sprintf = require('sprintf-js').sprintf;
43

54
var h = require('../helper');
65
var chalk = require('../chalk');
@@ -67,14 +66,14 @@ cmd.handler = function(argv) {
6766
if (problem.locked) ++stat.locked;
6867
if (problem.starred) ++stat.starred;
6968

70-
log.printf('%s %s %s [%4d] %-60s %-6s (%.2f %%)',
69+
log.printf('%s %s %s [%=4s] %-60s %-6s (%s %%)',
7170
(problem.starred ? chalk.yellow(icon.like) : icon.empty),
7271
(problem.locked ? chalk.red(icon.lock) : icon.nolock),
7372
h.prettyState(problem.state),
7473
problem.fid,
7574
problem.name,
76-
h.prettyLevel(sprintf('%-6s', problem.level)),
77-
problem.percent);
75+
h.prettyLevel(problem.level),
76+
problem.percent.toFixed(2));
7877

7978
if (argv.extra) {
8079
let badges = [problem.category];
@@ -99,9 +98,9 @@ cmd.handler = function(argv) {
9998

10099
if (argv.stat) {
101100
log.info();
102-
log.printf(' Listed: %-9d Locked: %-9d Starred: %-9d', problems.length, stat.locked, stat.starred);
103-
log.printf(' Accept: %-9d Not-AC: %-9d Remain: %-9d', stat.ac, stat.notac, stat.None);
104-
log.printf(' Easy: %-9d Medium: %-9d Hard: %-9d', stat.Easy, stat.Medium, stat.Hard);
101+
log.printf(' Listed: %-9s Locked: %-9s Starred: %-9s', problems.length, stat.locked, stat.starred);
102+
log.printf(' Accept: %-9s Not-AC: %-9s Remain: %-9s', stat.ac, stat.notac, stat.None);
103+
log.printf(' Easy: %-9s Medium: %-9s Hard: %-9s', stat.Easy, stat.Medium, stat.Hard);
105104
}
106105
});
107106
};

lib/commands/plugin.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
2-
var sprintf = require('sprintf-js').sprintf;
3-
42
var h = require('../helper');
53
var chalk = require('../chalk');
64
var config = require('../config');
75
var log = require('../log');
86
var Plugin = require('../plugin');
97
var session = require('../session');
8+
var sprintf = require('../sprintf');
109

1110
const cmd = {
1211
command: 'plugin [name]',

lib/commands/session.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22
var prompt = require('prompt');
3-
var sprintf = require('sprintf-js').sprintf;
43

54
var h = require('../helper');
65
var chalk = require('../chalk');
76
var log = require('../log');
87
var core = require('../core');
98
var session = require('../session');
9+
var sprintf = require('../sprintf');
1010

1111
const cmd = {
1212
command: 'session [keyword]',
@@ -61,14 +61,14 @@ function printSessions(e, sessions) {
6161
if (s.total_submitted > 0)
6262
submissionRate = s.total_acs * 100 / s.total_submitted;
6363

64-
log.printf(' %s %8d %-26s %s (%6s %%) %s (%6s %%)',
64+
log.printf(' %s %8s %-26s %6s (%6s %%) %6s (%6s %%)',
6565
s.is_active ? h.prettyState('ac') : ' ',
6666
s.id,
6767
s.name || 'Anonymous Session',
68-
chalk.green(sprintf('%6s', s.ac_questions)),
69-
sprintf('%.2f', questionRate),
70-
chalk.green(sprintf('%6s', s.total_acs)),
71-
sprintf('%.2f', submissionRate));
68+
chalk.green(s.ac_questions),
69+
questionRate.toFixed(2),
70+
chalk.green(s.total_acs),
71+
submissionRate.toFixed(2));
7272
}
7373
}
7474

lib/commands/show.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function showProblem(problem, argv) {
137137
}
138138
}
139139

140-
log.printf('[%d] %s %s', problem.fid, problem.name,
140+
log.printf('[%s] %s %s', problem.fid, problem.name,
141141
(problem.starred ? chalk.yellow(icon.like) : icon.empty));
142142
log.info();
143143
log.info(chalk.underline(problem.link));
@@ -150,7 +150,7 @@ function showProblem(problem, argv) {
150150

151151
log.info();
152152
log.printf('* %s', problem.category);
153-
log.printf('* %s (%.2f%%)', h.prettyLevel(problem.level), problem.percent);
153+
log.printf('* %s (%s%%)', h.prettyLevel(problem.level), problem.percent.toFixed(2));
154154

155155
if (filename)
156156
log.printf('* Source Code: %s', chalk.yellow.underline(filename));

lib/commands/star.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ cmd.handler = function(argv) {
3535
core.starProblem(problem, !argv.delete, function(e, starred) {
3636
if (e) return log.fail(e);
3737

38-
log.printf('[%d] %s %s', problem.fid, problem.name,
38+
log.printf('[%s] %s %s', problem.fid, problem.name,
3939
chalk.yellow(starred ? icon.like : icon.unlike));
4040

4141
core.updateProblem(problem, {starred: starred});

lib/commands/stat.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22
var moment = require('moment');
3-
var sprintf = require('sprintf-js').sprintf;
43
var _ = require('underscore');
54

65
var chalk = require('../chalk');
76
var icon = require('../icon');
87
var log = require('../log');
98
var core = require('../core');
109
var session = require('../session');
10+
var sprintf = require('../sprintf');
1111
var h = require('../helper');
1212

1313
const cmd = {
@@ -50,9 +50,9 @@ function printLine(key, done, all) {
5050
const n = 30;
5151
const percent = (all > 0) ? done / all : 0;
5252
const x = Math.ceil(n * percent);
53-
log.printf(' %s\t%3d/%-3d (%6s %%) %s%s',
53+
log.printf(' %s\t%3s/%-3s (%6s %%) %s%s',
5454
h.prettyLevel(key), done, all,
55-
sprintf('%.2f', 100 * percent),
55+
(100 * percent).toFixed(2),
5656
chalk.green('█'.repeat(x)),
5757
chalk.red('░'.repeat(n - x)));
5858
}
@@ -96,15 +96,15 @@ function showGraph(problems) {
9696
if (groups > 5) groups = 5;
9797

9898
const header = _.range(groups)
99-
.map(x => sprintf('%4d%18d', x * 10 + 1, x * 10 + 10))
99+
.map(x => sprintf('%4s%18s', x * 10 + 1, x * 10 + 10))
100100
.join('');
101101
log.info(' ' + header);
102102

103103
const graph = [];
104104
for (let problem of problems)
105105
graph[problem.fid] = ICONS[problem.state] || ICONS.none;
106106

107-
let line = [sprintf(' %03d', 0)];
107+
let line = [sprintf('%04s', 0)];
108108
for (let i = 1, n = graph.length; i <= n; ++i) {
109109
// padding before group
110110
if (i % 10 === 1) line.push(' ');
@@ -114,7 +114,7 @@ function showGraph(problems) {
114114
// time to start new row
115115
if (i % (10 * groups) === 0 || i === n) {
116116
log.info(line.join(' '));
117-
line = [sprintf(' %03d', i)];
117+
line = [sprintf('%04s', i)];
118118
}
119119
}
120120

lib/commands/submission.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function doTask(problem, queue, cb) {
6262
// - green: accepted, fresh download
6363
// - yellow: not ac-ed, fresh download
6464
// - white: existed already, skip download
65-
log.printf('[%4d] %-60s %s', problem.fid, problem.name,
65+
log.printf('[%=4s] %-60s %s', problem.fid, problem.name,
6666
(e ? chalk.red('ERROR: ' + (e.msg || e)) : msg));
6767
if (cb) cb(e);
6868
}

lib/commands/user.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22
var prompt = require('prompt');
3-
var sprintf = require('sprintf-js').sprintf;
43

54
var h = require('../helper');
65
var config = require('../config');
76
var chalk = require('../chalk');
87
var log = require('../log');
98
var core = require('../core');
109
var session = require('../session');
10+
var sprintf = require('../sprintf');
1111

1212
const cmd = {
1313
command: 'user',
@@ -65,9 +65,9 @@ cmd.handler = function(argv) {
6565
if (user) {
6666
log.info(chalk.gray(sprintf(' %-9s %-20s %s', 'Premium', 'User', 'Host')));
6767
log.info(chalk.gray('-'.repeat(60)));
68-
log.printf(' %s %s %s',
68+
log.printf(' %s %-20s %s',
6969
h.prettyText('', user.paid || false),
70-
chalk.yellow(sprintf('%-20s', user.name)),
70+
chalk.yellow(user.name),
7171
config.sys.urls.base);
7272
} else
7373
return log.fail('You are not login yet?');

0 commit comments

Comments
 (0)