1 | const _Terminal = require('./terminal');
|
2 | const _BarElement = require('./generic-bar');
|
3 | const _options = require('./options');
|
4 | const _EventEmitter = require('events');
|
5 |
|
6 |
|
7 | module.exports = class MultiBar extends _EventEmitter{
|
8 |
|
9 | constructor(options, preset){
|
10 | super();
|
11 |
|
12 |
|
13 | this.bars = [];
|
14 |
|
15 |
|
16 | this.options = _options.parse(options, preset);
|
17 |
|
18 |
|
19 | this.options.synchronousUpdate = false;
|
20 |
|
21 |
|
22 | this.terminal = (this.options.terminal) ? this.options.terminal : new _Terminal(this.options.stream);
|
23 |
|
24 |
|
25 | this.timer = null;
|
26 |
|
27 |
|
28 | this.isActive = false;
|
29 |
|
30 |
|
31 | this.schedulingRate = (this.terminal.isTTY() ? this.options.throttleTime : this.options.notTTYSchedule);
|
32 | }
|
33 |
|
34 |
|
35 | create(total, startValue, payload){
|
36 |
|
37 | if (this.options.noTTYOutput === false && this.terminal.isTTY() === false){
|
38 | return;
|
39 | }
|
40 |
|
41 |
|
42 | const bar = new _BarElement(this.options);
|
43 |
|
44 |
|
45 | this.bars.push(bar);
|
46 |
|
47 |
|
48 | if (!this.isActive){
|
49 |
|
50 | if (this.options.hideCursor === true){
|
51 | this.terminal.cursor(false);
|
52 | }
|
53 |
|
54 |
|
55 | if (this.options.linewrap === false){
|
56 | this.terminal.lineWrapping(false);
|
57 | }
|
58 |
|
59 |
|
60 | this.timer = setTimeout(this.update.bind(this), this.schedulingRate);
|
61 | }
|
62 |
|
63 |
|
64 | this.isActive = true;
|
65 |
|
66 |
|
67 | bar.start(total, startValue, payload);
|
68 |
|
69 |
|
70 | this.emit('start');
|
71 |
|
72 |
|
73 | return bar;
|
74 | }
|
75 |
|
76 |
|
77 | remove(bar){
|
78 |
|
79 | const index = this.bars.indexOf(bar);
|
80 |
|
81 |
|
82 | if (index < 0){
|
83 | return false;
|
84 | }
|
85 |
|
86 |
|
87 | this.bars.splice(index, 1);
|
88 |
|
89 |
|
90 | this.update();
|
91 |
|
92 |
|
93 | this.terminal.newline();
|
94 | this.terminal.clearBottom();
|
95 |
|
96 | return true;
|
97 | }
|
98 |
|
99 |
|
100 | update(){
|
101 |
|
102 | if (this.timer){
|
103 | clearTimeout(this.timer);
|
104 | this.timer = null;
|
105 | }
|
106 |
|
107 |
|
108 | this.emit('update-pre');
|
109 |
|
110 |
|
111 | this.terminal.cursorRelativeReset();
|
112 |
|
113 |
|
114 | this.emit('redraw-pre');
|
115 |
|
116 |
|
117 | for (let i=0; i< this.bars.length; i++){
|
118 |
|
119 | if (i > 0){
|
120 | this.terminal.newline();
|
121 | }
|
122 |
|
123 |
|
124 | this.bars[i].render();
|
125 | }
|
126 |
|
127 |
|
128 | this.emit('redraw-post');
|
129 |
|
130 |
|
131 | if (this.options.noTTYOutput && this.terminal.isTTY() === false){
|
132 | this.terminal.newline();
|
133 | this.terminal.newline();
|
134 | }
|
135 |
|
136 |
|
137 | this.timer = setTimeout(this.update.bind(this), this.schedulingRate);
|
138 |
|
139 |
|
140 | this.emit('update-post');
|
141 |
|
142 |
|
143 | if (this.options.stopOnComplete && !this.bars.find(bar => bar.isActive)) {
|
144 | this.stop();
|
145 | }
|
146 | }
|
147 |
|
148 | stop(){
|
149 |
|
150 |
|
151 | clearTimeout(this.timer);
|
152 | this.timer = null;
|
153 |
|
154 |
|
155 | this.isActive = false;
|
156 |
|
157 |
|
158 | if (this.options.hideCursor === true){
|
159 | this.terminal.cursor(true);
|
160 | }
|
161 |
|
162 |
|
163 | if (this.options.linewrap === false){
|
164 | this.terminal.lineWrapping(true);
|
165 | }
|
166 |
|
167 |
|
168 | this.terminal.cursorRelativeReset();
|
169 |
|
170 |
|
171 | this.emit('stop-pre-clear');
|
172 |
|
173 |
|
174 | if (this.options.clearOnComplete){
|
175 |
|
176 | this.terminal.clearBottom();
|
177 |
|
178 |
|
179 | }else{
|
180 |
|
181 | for (let i=0; i< this.bars.length; i++){
|
182 |
|
183 | if (i > 0){
|
184 | this.terminal.newline();
|
185 | }
|
186 |
|
187 |
|
188 | this.bars[i].render();
|
189 |
|
190 |
|
191 | this.bars[i].stop();
|
192 | }
|
193 |
|
194 |
|
195 | this.terminal.newline();
|
196 | }
|
197 |
|
198 |
|
199 | this.emit('stop');
|
200 | }
|
201 | }
|