-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmulti-progress.js
98 lines (83 loc) · 1.88 KB
/
multi-progress.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// from https://gist.github.com/nuxlli/b425344b92ac1ff99c74
// with some modifications & additions
const Progress = require('progress');
const mockBar = {
tick() {},
terminate() {},
update() {},
render() {},
};
const mockInstance = {
newBar() {
return mockBar;
},
terminate() {},
move() {},
tick() {},
update() {},
isTTY: false,
};
module.exports = class MultiProgress {
constructor(stream) {
this.stream = stream || process.stderr;
this.isTTY = this.stream.isTTY;
if (!this.isTTY) {
return mockInstance;
}
this.cursor = 0;
this.bars = [];
this.terminates = 0;
return this;
}
newBar(schema, options) {
options.stream = this.stream;
var bar = new Progress(schema, options);
this.bars.push(bar);
var index = this.bars.length - 1;
// alloc line
this.move(index);
this.stream.write('\n');
this.cursor += 1;
// replace original
var self = this;
bar.otick = bar.tick;
bar.oterminate = bar.terminate;
bar.oupdate = bar.update;
bar.tick = function(value, options) {
self.tick(index, value, options);
};
bar.terminate = function() {
self.terminates += 1;
if (self.terminates === self.bars.length) {
self.terminate();
}
};
bar.update = function(value, options){
self.update(index, value, options);
};
return bar;
}
terminate() {
this.move(this.bars.length);
this.stream.clearLine();
this.stream.cursorTo(0);
}
move(index) {
this.stream.moveCursor(0, index - this.cursor);
this.cursor = index;
}
tick(index, value, options) {
const bar = this.bars[index];
if (bar) {
this.move(index);
bar.otick(value, options);
}
}
update(index, value, options) {
const bar = this.bars[index];
if (bar) {
this.move(index);
bar.oupdate(value, options);
}
}
}