-
Notifications
You must be signed in to change notification settings - Fork 84
/
multi-bar.js
250 lines (191 loc) · 6.41 KB
/
multi-bar.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const _Terminal = require('./terminal');
const _BarElement = require('./generic-bar');
const _options = require('./options');
const _EventEmitter = require('events');
// Progress-Bar constructor
module.exports = class MultiBar extends _EventEmitter{
constructor(options, preset){
super();
// list of bars
this.bars = [];
// parse+store options
this.options = _options.parse(options, preset);
// disable synchronous updates
this.options.synchronousUpdate = false;
// store terminal instance
this.terminal = (this.options.terminal) ? this.options.terminal : new _Terminal(this.options.stream);
// the update timer
this.timer = null;
// progress bar active ?
this.isActive = false;
// update interval
this.schedulingRate = (this.terminal.isTTY() ? this.options.throttleTime : this.options.notTTYSchedule);
// logging output buffer
this.loggingBuffer = [];
// callback used for gracefulExit
this.sigintCallback = null;
}
// add a new bar to the stack
create(total, startValue, payload, barOptions={}){
// create new bar element and merge global options + overrides
// use the same global terminal instance for all instances
const bar = new _BarElement(Object.assign(
{},
// global options
this.options,
// terminal instance
{
terminal: this.terminal
},
// overrides
barOptions,
));
// store bar
this.bars.push(bar);
// progress updates are only visible in TTY mode!
if (this.options.noTTYOutput === false && this.terminal.isTTY() === false){
return bar;
}
// add handler to restore cursor settings (stop the bar) on SIGINT/SIGTERM ?
if (this.sigintCallback === null && this.options.gracefulExit){
this.sigintCallback = this.stop.bind(this);
process.once('SIGINT', this.sigintCallback);
process.once('SIGTERM', this.sigintCallback);
}
// multiprogress already active ?
if (!this.isActive){
// hide the cursor ?
if (this.options.hideCursor === true){
this.terminal.cursor(false);
}
// disable line wrapping ?
if (this.options.linewrap === false){
this.terminal.lineWrapping(false);
}
// initialize update timer
this.timer = setTimeout(this.update.bind(this), this.schedulingRate);
}
// set flag
this.isActive = true;
// start progress bar
bar.start(total, startValue, payload);
// trigger event
this.emit('start');
// return new instance
return bar;
}
// remove a bar from the stack
remove(bar){
// find element
const index = this.bars.indexOf(bar);
// element found ?
if (index < 0){
return false;
}
// remove element
this.bars.splice(index, 1);
// force update
this.update();
// clear bottom
this.terminal.newline();
this.terminal.clearBottom();
return true;
}
// internal update routine
update(){
// stop timer
if (this.timer){
clearTimeout(this.timer);
this.timer = null;
}
// trigger event
this.emit('update-pre');
// reset cursor
this.terminal.cursorRelativeReset();
// trigger event
this.emit('redraw-pre');
// content within logging buffer ?
if (this.loggingBuffer.length > 0){
this.terminal.clearLine();
// flush logging buffer and write content to terminal
while (this.loggingBuffer.length > 0){
this.terminal.write(this.loggingBuffer.shift(), true);
}
}
// update each bar
for (let i=0; i< this.bars.length; i++){
// add new line ?
if (i > 0){
this.terminal.newline();
}
// render
this.bars[i].render();
}
// trigger event
this.emit('redraw-post');
// add new line in notty mode!
if (this.options.noTTYOutput && this.terminal.isTTY() === false){
this.terminal.newline();
this.terminal.newline();
}
// next update
this.timer = setTimeout(this.update.bind(this), this.schedulingRate);
// trigger event
this.emit('update-post');
// stop if stopOnComplete and all bars stopped
if (this.options.stopOnComplete && !this.bars.find(bar => bar.isActive)) {
this.stop();
}
}
stop(){
// stop timer
clearTimeout(this.timer);
this.timer = null;
// remove sigint listener
if (this.sigintCallback){
process.removeListener('SIGINT', this.sigintCallback);
process.removeListener('SIGTERM', this.sigintCallback);
this.sigintCallback = null;
}
// set flag
this.isActive = false;
// cursor hidden ?
if (this.options.hideCursor === true){
this.terminal.cursor(true);
}
// re-enable line wrpaping ?
if (this.options.linewrap === false){
this.terminal.lineWrapping(true);
}
// reset cursor
this.terminal.cursorRelativeReset();
// trigger event
this.emit('stop-pre-clear');
// clear line on complete ?
if (this.options.clearOnComplete){
// clear all bars
this.terminal.clearBottom();
// or show final progress ?
}else{
// update each bar
for (let i=0; i< this.bars.length; i++){
// add new line ?
if (i > 0){
this.terminal.newline();
}
// trigger final rendering
this.bars[i].render();
// stop
this.bars[i].stop();
}
// new line on complete
this.terminal.newline();
}
// trigger event
this.emit('stop');
}
log(s){
// push content into logging buffer
this.loggingBuffer.push(s);
}
}