Skip to content

Commit e92d6ea

Browse files
committed
Fix 'start' parameter issue.
1 parent f258c6d commit e92d6ea

File tree

1 file changed

+75
-75
lines changed

1 file changed

+75
-75
lines changed

index.js

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,118 +3,118 @@ var cursor = require('./lib/cursor');
33
var color = require('colors-cli')
44

55
function loading(options) {
6-
if (!(this instanceof loading)) {
7-
return new loading(options)
8-
}
9-
if (typeof options === 'string') {
10-
options = {
11-
text: options
12-
}
6+
if (!(this instanceof loading)) {
7+
return new loading(options)
8+
}
9+
if (typeof options === 'string') {
10+
options = {
11+
text: options
1312
}
13+
}
1414

15-
this.options = ut.extend(options, {
16-
text: '',
17-
color: 'cyan',
18-
stream: process.stderr,
19-
// stream: process.stdout
20-
// loading 样式
21-
frames: ["◜", "◠", "◝", "◞", "◡", "◟"]
22-
});
15+
this.options = ut.extend(options, {
16+
text: '',
17+
color: 'cyan',
18+
stream: process.stderr,
19+
// stream: process.stdout
20+
// loading 样式
21+
frames: ["◜", "◠", "◝", "◞", "◡", "◟"]
22+
});
2323

24-
// 文本显示
25-
this.text = this.options.text;
24+
// 文本显示
25+
this.text = this.options.text;
2626

27-
// 颜色显示
28-
this.color = this.options.color;
27+
// 颜色显示
28+
this.color = this.options.color;
2929

30-
// 动画间隔时间
31-
this.interval = this.options.interval || 60;
32-
this.stream = this.options.stream;
30+
// 动画间隔时间
31+
this.interval = this.options.interval || 60;
32+
this.stream = this.options.stream;
3333

34-
// loading 样式
35-
this.frames = this.options.frames;
34+
// loading 样式
35+
this.frames = this.options.frames;
3636

37-
// 不存在
38-
this.id = null;
37+
// 不存在
38+
this.id = null;
3939

40-
// 要检查 Node 是否正在运行一个 TTY上下文 中
41-
// linux 中没有运行在 tty 下的进程是 守护进程
42-
this.enabled = this.options.enabled || ((this.stream && this.stream.isTTY) && !process.env.CI);
43-
this.frameIndex = 0;
40+
// 要检查 Node 是否正在运行一个 TTY上下文 中
41+
// linux 中没有运行在 tty 下的进程是 守护进程
42+
this.enabled = this.options.enabled || ((this.stream && this.stream.isTTY) && !process.env.CI);
43+
this.frameIndex = 0;
4444
}
4545

4646
loading.prototype.frame = function () {
47-
var frames = this.frames;
48-
// var frames = ["◜", "◠", "◝", "◞", "◡", "◟"];
49-
// var frames = ["◰", "◳", "◲", "◱"]
50-
// var frames = ["◐", "◓", "◑", "◒"]
51-
// var frames = [".", "o", "O", "°", "O", "o", "."]
52-
// var frames = ["⊶", "⊷"]
53-
// var frames = ["ဝ", "၀"]
54-
// var frames = ["←", "↖", "↑", "↗", "→", "↘", "↓", "↙"]
55-
// var frames = ["🕐 ", "🕑 ", "🕒 ", "🕓 ", "🕔 ", "🕕 ", "🕖 ", "🕗 ", "🕘 ", "🕙 ", "🕚 "]
56-
var frame = frames[this.frameIndex];
57-
if (this.color) {
58-
frame = color[this.color](frame);
59-
}
60-
this.frameIndex = ++this.frameIndex % frames.length;
61-
return frame + ' ' + this.text;
47+
var frames = this.frames;
48+
// var frames = ["◜", "◠", "◝", "◞", "◡", "◟"];
49+
// var frames = ["◰", "◳", "◲", "◱"]
50+
// var frames = ["◐", "◓", "◑", "◒"]
51+
// var frames = [".", "o", "O", "°", "O", "o", "."]
52+
// var frames = ["⊶", "⊷"]
53+
// var frames = ["ဝ", "၀"]
54+
// var frames = ["←", "↖", "↑", "↗", "→", "↘", "↓", "↙"]
55+
// var frames = ["🕐 ", "🕑 ", "🕒 ", "🕓 ", "🕔 ", "🕕 ", "🕖 ", "🕗 ", "🕘 ", "🕙 ", "🕚 "]
56+
var frame = frames[this.frameIndex];
57+
if (this.color) {
58+
frame = color[this.color](frame);
59+
}
60+
this.frameIndex = ++this.frameIndex % frames.length;
61+
return frame + ' ' + this.text;
6262
}
6363

6464
loading.prototype.clear = function () {
6565

66-
if (!this.enabled) {
67-
return this;
68-
}
66+
if (!this.enabled) {
67+
return this;
68+
}
6969

70-
this.stream.clearLine();
71-
this.stream.cursorTo(0);
70+
this.stream.clearLine();
71+
this.stream.cursorTo(0);
7272

73-
return this;
73+
return this;
7474
}
7575

7676
loading.prototype.render = function () {
77-
this.clear();
78-
this.stream.write(this.frame());
79-
return this;
80-
77+
this.clear();
78+
this.stream.write(this.frame());
79+
return this;
8180
}
8281

83-
loading.prototype.start = function () {
84-
if (!this.enabled || this.id) return this;
85-
this.clear();
86-
cursor.hide(this.stream);
87-
this.id = setInterval(this.render.bind(this), this.interval);
88-
return this;
82+
loading.prototype.start = function (text) {
83+
if (text) this.text = text;
84+
if (!this.enabled || this.id) return this;
85+
this.clear();
86+
cursor.hide(this.stream);
87+
this.id = setInterval(this.render.bind(this), this.interval);
88+
return this;
8989
}
9090

9191
loading.prototype.stop = function () {
92-
if (!this.enabled) return this;
93-
clearInterval(this.id);
94-
this.id = null;
95-
this.clear();
96-
cursor.show(this.stream);
97-
return this;
92+
if (!this.enabled) return this;
93+
clearInterval(this.id);
94+
this.id = null;
95+
this.clear();
96+
cursor.show(this.stream);
97+
return this;
9898
}
9999

100100

101101
loading.prototype.succeed = function (text) {
102-
return this.stopAndPersist(color.green('✔'), text);
102+
return this.stopAndPersist(color.green('✔'), text);
103103
}
104104
loading.prototype.fail = function (text) {
105-
return this.stopAndPersist(color.red('✖'), text);
105+
return this.stopAndPersist(color.red('✖'), text);
106106
}
107107
loading.prototype.warn = function (text) {
108-
return this.stopAndPersist(color.yellow('⚠'), text);
108+
return this.stopAndPersist(color.yellow('⚠'), text);
109109
}
110110
loading.prototype.info = function (text) {
111-
return this.stopAndPersist(color.blue('ℹ'), text);
111+
return this.stopAndPersist(color.blue('ℹ'), text);
112112
}
113113
loading.prototype.stopAndPersist = function (symbol, text) {
114-
text = text || this.text
115-
this.stop();
116-
this.stream.write((symbol ? symbol + ' ' : ' ') + text + '\n');
117-
return this;
114+
text = text || this.text
115+
this.stop();
116+
this.stream.write((symbol ? symbol + ' ' : ' ') + text + '\n');
117+
return this;
118118
}
119119

120120
module.exports = loading

0 commit comments

Comments
 (0)