Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitaaf6451

Browse files
committed
remove sprintf-js
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parentbd80b3a commitaaf6451

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-
varsprintf=require('sprintf-js').sprintf;
43

54
varh=require('../helper');
65
varchalk=require('../chalk');
76
varlog=require('../log');
87
varcache=require('../cache');
98
varsession=require('../session');
9+
varsprintf=require('../sprintf');
1010

1111
constcmd={
1212
command:'cache [keyword]',
@@ -56,8 +56,8 @@ cmd.handler = function(argv) {
5656
returnx;
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-
varsprintf=require('sprintf-js').sprintf;
43

54
varh=require('../helper');
65
varchalk=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
letbadges=[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-
varsprintf=require('sprintf-js').sprintf;
3-
42
varh=require('../helper');
53
varchalk=require('../chalk');
64
varconfig=require('../config');
75
varlog=require('../log');
86
varPlugin=require('../plugin');
97
varsession=require('../session');
8+
varsprintf=require('../sprintf');
109

1110
constcmd={
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
varprompt=require('prompt');
3-
varsprintf=require('sprintf-js').sprintf;
43

54
varh=require('../helper');
65
varchalk=require('../chalk');
76
varlog=require('../log');
87
varcore=require('../core');
98
varsession=require('../session');
9+
varsprintf=require('../sprintf');
1010

1111
constcmd={
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)returnlog.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
varmoment=require('moment');
3-
varsprintf=require('sprintf-js').sprintf;
43
var_=require('underscore');
54

65
varchalk=require('../chalk');
76
varicon=require('../icon');
87
varlog=require('../log');
98
varcore=require('../core');
109
varsession=require('../session');
10+
varsprintf=require('../sprintf');
1111
varh=require('../helper');
1212

1313
constcmd={
@@ -50,9 +50,9 @@ function printLine(key, done, all) {
5050
constn=30;
5151
constpercent=(all>0) ?done/all :0;
5252
constx=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
constheader=_.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
constgraph=[];
104104
for(letproblemofproblems)
105105
graph[problem.fid]=ICONS[problem.state]||ICONS.none;
106106

107-
letline=[sprintf(' %03d',0)];
107+
letline=[sprintf('%04s',0)];
108108
for(leti=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
varprompt=require('prompt');
3-
varsprintf=require('sprintf-js').sprintf;
43

54
varh=require('../helper');
65
varconfig=require('../config');
76
varchalk=require('../chalk');
87
varlog=require('../log');
98
varcore=require('../core');
109
varsession=require('../session');
10+
varsprintf=require('../sprintf');
1111

1212
constcmd={
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
returnlog.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-
varsprintf=require('sprintf-js').sprintf;
43

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

77
constlog={
88
output:_.bind(console.log,console),
@@ -27,7 +27,7 @@ log.isEnabled = function(name) {
2727
log.fail=function(e){
2828
letmsg=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+
functionlen(s){
4+
lets1=s.replace(/\u001b\[[^m]*m/g,'');// remove color controls
5+
s1=s1.replace(/[^\x00-\xff]/g,' ');// fix non-ascii
6+
returns1.length;
7+
}
8+
9+
functionpadLeft(s,n,c){
10+
letk=Math.max(0,n-len(s));
11+
returnc.repeat(k)+s;
12+
}
13+
14+
functionpadRight(s,n,c){
15+
letk=Math.max(0,n-len(s));
16+
returns+c.repeat(k);
17+
}
18+
19+
functionpadCenter(s,n,c){
20+
letk=Math.max(0,n-len(s));
21+
letr=(k-k%2)/2,l=k-r;
22+
returnc.repeat(l)+s+c.repeat(r);
23+
}
24+
25+
consttsprintf=function(){
26+
constargs=Array.from(arguments);
27+
letfmt=args.shift();
28+
returnfmt.replace(/%[^s%]*[s%]/g,function(s){
29+
if(s==='%%')return'%';
30+
31+
letx=''+args.shift();
32+
letn=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+
returnx;
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+
constassert=require('chai').assert;
3+
constrewire=require('rewire');
4+
5+
constsprintf=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+
constchalk=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)

[8]ページ先頭

©2009-2025 Movatter.jp