8000 remove sprintf-js · 521hellogithub/leetcode-cli@aaf6451 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aaf6451

Browse files
committed
remove sprintf-js
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent bd80b3a commit aaf6451

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< 10000 /td>
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?');

lib/log.js

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

54
var chalk = require('./chalk');
5+
var sprintf = require('./sprintf');
66

77
const log = {
88
output: _.bind(console.log, console),
@@ -27,7 +27,7 @@ log.isEnabled = function(name) {
2727
log.fail = function(e) {
2828
let msg = sprintf('%s', (e.msg || e));
2929
if (e.statusCode) {
30-
msg += sprintf(' [code=%d]', e.statusCode);
30+
msg += sprintf(' [code=%s]', e.statusCode);
3131
}
3232
log.error(msg);
3333
};

lib/sprintf.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict'
2+
3+
function len(s) {
4+
let s1 = s.replace(/\u001b\[[^m]*m/g, ''); // remove color controls
5+
s1 = s1.replace(/[^\x00-\xff]/g, ' '); // fix non-ascii
6+
return s1.length;
7+
}
8+
9+
function padLeft(s, n, c) {
10+
let k = Math.max(0, n - len(s));
11+
return c.repeat(k) + s;
12+
}
13+
14+
function padRight(s, n , c) {
15+
let k = Math.max(0, n - len(s));
16+
return s + c.repeat(k);
17+
}
18+
19+
function padCenter(s, n, c) {
20+
let k = Math.max(0, n - len(s));
21+
let r = (k - k % 2) / 2, l = k - r;
22+
return c.repeat(l) + s + c.repeat(r);
23+
}
24+
25+
const tsprintf = function() {
26+
const args = Array.from(arguments);
27+
let fmt = args.shift();
28+
return fmt.replace(/%[^s%]*[s%]/g, function(s) {
29+
if (s === '%%') return '%';
30+
31+
let x = '' + args.shift();
32+
let n = 0;
33+
34+
s = s.slice(1, s.length-1);
35+
if (s.length > 0) {
36+
switch (s[0]) {
37+
case '-':
38+
n = parseInt(s.slice(1)) || 0;
39+
x = padRight(x, n, ' ');
40+
break;
41+
case '=':
42+
n = parseInt(s.slice(1)) || 0;
43+
x = padCenter(x, n, ' ');
44+
break;
45+
default:
46+
n = parseInt(s) || 0;
47+
x = padLeft(x, n, ' ');
48+
break;
49+
}
50+
}
51+
52+
return x;
53+
});
54+
};
55+
56+
module.exports = tsprintf;

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"ora": "3.0.0",
6060
"prompt": "1.0.0",
6161
"request": "2.88.0",
62-
"sprintf-js": "1.1.1",
6362
"supports-color": "5.5.0",
6463
"underscore": "1.9.1",
6564
"wordwrap": "1.0.0",

test/test_log.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('log', function() {
102102

103103
describe('#printf', function() {
104104
it('should ok', function() {
105-
log.printf('%s and %d and %%', 'string', 100);
105+
log.printf('%s and %s and %%', 'string', 100);
106106
assert.equal(expected, 'string and 100 and %');
107107
});
108108
}); // #printf

test/test_sprintf.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
const assert = require('chai').assert;
3+
const rewire = require('rewire');
4+
5+
const sprintf = require('../lib/sprintf');
6+
7+
describe('sprintf', function() {
8+
it('should ok', function() {
9+
assert.equal(sprintf('%%'), '%');
10+
assert.equal(sprintf('%s', 123), '123');
11+
assert.equal(sprintf('%6s', 123), ' 123');
12+
assert.equal(sprintf('%-6s', 123), '123 ');
13+
assert.equal(sprintf('%=6s', 123), ' 123 ');
14+
15+
assert.equal(sprintf('%4s,%=4s,%-4s', 123, 'xy', 3.1), ' 123, xy ,3.1 ');
16+
});
17+
18+
it('should non-ascii ok', function() {
19+
assert.equal(sprintf('%4s', '中'), ' 中');
20+
assert.equal(sprintf('%-4s', '中'), '中 ');
21+
assert.equal(sprintf('%=4s', '中'), ' 中 ');
22+
23+
assert.equal(sprintf('%=14s', '12你好34世界'), ' 12你好34世界 ');
24+
});
25+
26+
it('should color ok', function() {
27+
const chalk = rewire('../lib/chalk');
28+
chalk.init();
29+
chalk.setTheme('default');
30+
31+
assert.equal(sprintf('%=3s', chalk.red('X')), ' \u001b[38;5;196mX\u001b[39m ');
32+
});
33+
});

0 commit comments

Comments
 (0)
0