-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogress.js
More file actions
55 lines (50 loc) · 1.24 KB
/
progress.js
File metadata and controls
55 lines (50 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Gauge from "gauge";
export default class {
/**
* 构造方法,用于初始化进度条
*/
constructor() {
this.curr_index = 0;
this.taskName = "";
this.fullLength = 1;
// 进度条部分
this.gt = new Gauge(process.stderr, { // 进度条对象
updateInterval: 50,
cleanupOnExit: true
});
this.progress = 0; // 进度
this.pulse = setInterval(() => { // 说明信息更新器
this.gt.pulse(`正在测试服务器 ${this.taskName} 的延迟 ${this.curr_index}/${this.fullLength}`)
}, 110);
this.prog = setInterval(() => { // 进度条更新器
this.progress = this.curr_index / this.fullLength;
this.gt.show(Math.round(this.progress * 100)+"%", this.progress)
if (this.progress >= 1) {
clearInterval(this.prog)
clearInterval(this.pulse)
this.gt.disable()
}
}, 100);
}
/**
* 显示进度条
*/
show() {
this.gt.show();
}
/**
* 更新进度
* @param {String} taskName 当前任务名
*/
update(taskName) {
this.taskName = taskName;
this.curr_index++;
}
/**
* 设置总任务量
* @param {Number} length 总任务数
*/
setFullLength(length) {
this.fullLength = length;
}
}