1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | exports = module.exports = ProgressBar;
|
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 | function ProgressBar(fmt, options) {
|
46 | this.stream = options.stream || process.stderr;
|
47 |
|
48 | if (typeof(options) == 'number') {
|
49 | var total = options;
|
50 | options = {};
|
51 | options.total = total;
|
52 | } else {
|
53 | options = options || {};
|
54 | if ('string' != typeof fmt) throw new Error('format required');
|
55 | if ('number' != typeof options.total) throw new Error('total required');
|
56 | }
|
57 |
|
58 | this.fmt = fmt;
|
59 | this.curr = options.curr || 0;
|
60 | this.total = options.total;
|
61 | this.width = options.width || this.total;
|
62 | this.clear = options.clear
|
63 | this.chars = {
|
64 | complete : options.complete || '=',
|
65 | incomplete : options.incomplete || '-',
|
66 | head : options.head || (options.complete || '=')
|
67 | };
|
68 | this.renderThrottle = options.renderThrottle !== 0 ? (options.renderThrottle || 16) : 0;
|
69 | this.lastRender = -Infinity;
|
70 | this.callback = options.callback || function () {};
|
71 | this.tokens = {};
|
72 | this.lastDraw = '';
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | ProgressBar.prototype.tick = function(len, tokens){
|
84 | if (len !== 0)
|
85 | len = len || 1;
|
86 |
|
87 |
|
88 | if ('object' == typeof len) tokens = len, len = 1;
|
89 | if (tokens) this.tokens = tokens;
|
90 |
|
91 |
|
92 | if (0 == this.curr) this.start = new Date;
|
93 |
|
94 | this.curr += len
|
95 |
|
96 |
|
97 | this.render();
|
98 |
|
99 |
|
100 | if (this.curr >= this.total) {
|
101 | this.render(undefined, true);
|
102 | this.complete = true;
|
103 | this.terminate();
|
104 | this.callback(this);
|
105 | return;
|
106 | }
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | ProgressBar.prototype.render = function (tokens, force) {
|
118 | force = force !== undefined ? force : false;
|
119 | if (tokens) this.tokens = tokens;
|
120 |
|
121 | if (!this.stream.isTTY) return;
|
122 |
|
123 | var now = Date.now();
|
124 | var delta = now - this.lastRender;
|
125 | if (!force && (delta < this.renderThrottle)) {
|
126 | return;
|
127 | } else {
|
128 | this.lastRender = now;
|
129 | }
|
130 |
|
131 | var ratio = this.curr / this.total;
|
132 | ratio = Math.min(Math.max(ratio, 0), 1);
|
133 |
|
134 | var percent = Math.floor(ratio * 100);
|
135 | var incomplete, complete, completeLength;
|
136 | var elapsed = new Date - this.start;
|
137 | var eta = (percent == 100) ? 0 : elapsed * (this.total / this.curr - 1);
|
138 | var rate = this.curr / (elapsed / 1000);
|
139 |
|
140 |
|
141 | var str = this.fmt
|
142 | .replace(':current', this.curr)
|
143 | .replace(':total', this.total)
|
144 | .replace(':elapsed', isNaN(elapsed) ? '0.0' : (elapsed / 1000).toFixed(1))
|
145 | .replace(':eta', (isNaN(eta) || !isFinite(eta)) ? '0.0' : (eta / 1000)
|
146 | .toFixed(1))
|
147 | .replace(':percent', percent.toFixed(0) + '%')
|
148 | .replace(':rate', Math.round(rate));
|
149 |
|
150 |
|
151 | var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length);
|
152 | if(availableSpace && process.platform === 'win32'){
|
153 | availableSpace = availableSpace - 1;
|
154 | }
|
155 |
|
156 | var width = Math.min(this.width, availableSpace);
|
157 |
|
158 |
|
159 | completeLength = Math.round(width * ratio);
|
160 | complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);
|
161 | incomplete = Array(Math.max(0, width - completeLength + 1)).join(this.chars.incomplete);
|
162 |
|
163 |
|
164 | if(completeLength > 0)
|
165 | complete = complete.slice(0, -1) + this.chars.head;
|
166 |
|
167 |
|
168 | str = str.replace(':bar', complete + incomplete);
|
169 |
|
170 |
|
171 | if (this.tokens) for (var key in this.tokens) str = str.replace(':' + key, this.tokens[key]);
|
172 |
|
173 | if (this.lastDraw !== str) {
|
174 | this.stream.cursorTo(0);
|
175 | this.stream.write(str);
|
176 | this.stream.clearLine(1);
|
177 | this.lastDraw = str;
|
178 | }
|
179 | };
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | ProgressBar.prototype.update = function (ratio, tokens) {
|
196 | var goal = Math.floor(ratio * this.total);
|
197 | var delta = goal - this.curr;
|
198 |
|
199 | this.tick(delta, tokens);
|
200 | };
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | ProgressBar.prototype.interrupt = function (message) {
|
209 |
|
210 | this.stream.clearLine();
|
211 |
|
212 | this.stream.cursorTo(0);
|
213 |
|
214 | this.stream.write(message);
|
215 |
|
216 | this.stream.write('\n');
|
217 |
|
218 | this.stream.write(this.lastDraw);
|
219 | };
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | ProgressBar.prototype.terminate = function () {
|
228 | if (this.clear) {
|
229 | if (this.stream.clearLine) {
|
230 | this.stream.clearLine();
|
231 | this.stream.cursorTo(0);
|
232 | }
|
233 | } else {
|
234 | this.stream.write('\n');
|
235 | }
|
236 | };
|