1 | const _ETA = require('./eta');
|
2 | const _Terminal = require('./terminal');
|
3 | const _formatter = require('./formatter');
|
4 | const _EventEmitter = require('events');
|
5 |
|
6 |
|
7 | module.exports = class GenericBar extends _EventEmitter{
|
8 |
|
9 | constructor(options){
|
10 | super();
|
11 |
|
12 |
|
13 | this.options = options;
|
14 |
|
15 |
|
16 | this.terminal = (this.options.terminal) ? this.options.terminal : new _Terminal(this.options.stream);
|
17 |
|
18 |
|
19 | this.value = 0;
|
20 |
|
21 |
|
22 | this.total = 100;
|
23 |
|
24 |
|
25 | this.lastDrawnString = null;
|
26 |
|
27 |
|
28 | this.startTime = null;
|
29 |
|
30 |
|
31 | this.lastRedraw = Date.now();
|
32 |
|
33 |
|
34 | this.eta = new _ETA(this.options.etaBufferLength, 0, 0);
|
35 |
|
36 |
|
37 | this.payload = {};
|
38 |
|
39 |
|
40 | this.isActive = false;
|
41 |
|
42 |
|
43 | this.formatter = (typeof this.options.format === 'function') ? this.options.format : _formatter;
|
44 | }
|
45 |
|
46 |
|
47 | render(){
|
48 |
|
49 | let progress = (this.value/this.total);
|
50 |
|
51 |
|
52 | if (isNaN(progress)){
|
53 | progress = (this.options && this.options.emptyOnZero) ? 0.0 : 1.0;
|
54 | }
|
55 |
|
56 |
|
57 | progress = Math.min(Math.max(progress, 0.0), 1.0);
|
58 |
|
59 |
|
60 | const params = {
|
61 | progress: progress,
|
62 | eta: this.eta.getTime(),
|
63 | startTime: this.startTime,
|
64 | total: this.total,
|
65 | value: this.value,
|
66 | maxWidth: this.terminal.getWidth()
|
67 | };
|
68 |
|
69 |
|
70 | const s = this.formatter(this.options, params, this.payload);
|
71 |
|
72 | const forceRedraw = this.options.forceRedraw
|
73 |
|
74 | || (this.options.noTTYOutput && !this.terminal.isTTY());
|
75 |
|
76 |
|
77 | if (forceRedraw || this.lastDrawnString != s){
|
78 |
|
79 | this.emit('redraw-pre');
|
80 |
|
81 |
|
82 | this.terminal.cursorTo(0, null);
|
83 |
|
84 |
|
85 | this.terminal.write(s);
|
86 |
|
87 |
|
88 | this.terminal.clearRight();
|
89 |
|
90 |
|
91 | this.lastDrawnString = s;
|
92 |
|
93 |
|
94 | this.lastRedraw = Date.now();
|
95 |
|
96 |
|
97 | this.emit('redraw-post');
|
98 | }
|
99 | }
|
100 |
|
101 |
|
102 | start(total, startValue, payload){
|
103 |
|
104 | this.value = startValue || 0;
|
105 | this.total = (typeof total !== 'undefined' && total >= 0) ? total : 100;
|
106 | this.payload = payload || {};
|
107 |
|
108 | this.startTime = Date.now();
|
109 | this.lastDrawnString = '';
|
110 |
|
111 |
|
112 | this.eta = new _ETA(this.options.etaBufferLength, this.startTime, this.value);
|
113 |
|
114 |
|
115 | this.isActive = true;
|
116 |
|
117 |
|
118 | this.emit('start', total, startValue);
|
119 | }
|
120 |
|
121 |
|
122 | stop(){
|
123 |
|
124 | this.isActive = false;
|
125 |
|
126 |
|
127 | this.emit('stop', this.total, this.value);
|
128 | }
|
129 |
|
130 |
|
131 | update(current, payload){
|
132 | if (typeof current !== 'undefined' && current !== null) {
|
133 |
|
134 | this.value = current;
|
135 |
|
136 |
|
137 | this.eta.update(Date.now(), current, this.total);
|
138 | }
|
139 |
|
140 |
|
141 | this.emit('update', this.total, this.value);
|
142 |
|
143 |
|
144 | const payloadData = payload || {};
|
145 | for (const key in payloadData){
|
146 | this.payload[key] = payloadData[key];
|
147 | }
|
148 |
|
149 |
|
150 | if (this.value >= this.getTotal() && this.options.stopOnComplete) {
|
151 | this.stop();
|
152 | }
|
153 | }
|
154 |
|
155 |
|
156 | increment(step, payload){
|
157 | step = step || 1;
|
158 | this.update(this.value + step, payload);
|
159 | }
|
160 |
|
161 |
|
162 | getTotal(){
|
163 | return this.total;
|
164 | }
|
165 |
|
166 |
|
167 | setTotal(total){
|
168 | if (typeof total !== 'undefined' && total >= 0){
|
169 | this.total = total;
|
170 | }
|
171 | }
|
172 | }
|