UNPKG

6.68 kBJavaScriptView Raw
1/*!
2 * node-progress
3 * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
4 * MIT Licensed
5 */
6
7/**
8 * Expose `ProgressBar`.
9 */
10
11exports = module.exports = ProgressBar;
12
13/**
14 * Initialize a `ProgressBar` with the given `fmt` string and `options` or
15 * `total`.
16 *
17 * Options:
18 *
19 * - `curr` current completed index
20 * - `total` total number of ticks to complete
21 * - `width` the displayed width of the progress bar defaulting to total
22 * - `stream` the output stream defaulting to stderr
23 * - `head` head character defaulting to complete character
24 * - `complete` completion character defaulting to "="
25 * - `incomplete` incomplete character defaulting to "-"
26 * - `renderThrottle` minimum time between updates in milliseconds defaulting to 16
27 * - `callback` optional function to call when the progress bar completes
28 * - `clear` will clear the progress bar upon termination
29 *
30 * Tokens:
31 *
32 * - `:bar` the progress bar itself
33 * - `:current` current tick number
34 * - `:total` total ticks
35 * - `:elapsed` time elapsed in seconds
36 * - `:percent` completion percentage
37 * - `:eta` eta in seconds
38 * - `:rate` rate of ticks per second
39 *
40 * @param {string} fmt
41 * @param {object|number} options or total
42 * @api public
43 */
44
45function 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 * "tick" the progress bar with optional `len` and optional `tokens`.
77 *
78 * @param {number|object} len or tokens
79 * @param {object} tokens
80 * @api public
81 */
82
83ProgressBar.prototype.tick = function(len, tokens){
84 if (len !== 0)
85 len = len || 1;
86
87 // swap tokens
88 if ('object' == typeof len) tokens = len, len = 1;
89 if (tokens) this.tokens = tokens;
90
91 // start time for eta
92 if (0 == this.curr) this.start = new Date;
93
94 this.curr += len
95
96 // try to render
97 this.render();
98
99 // progress complete
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 * Method to render the progress bar with optional `tokens` to place in the
111 * progress bar's `fmt` field.
112 *
113 * @param {object} tokens
114 * @api public
115 */
116
117ProgressBar.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 /* populate the bar template with percentages and timestamps */
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 /* compute the available space (non-zero) for the bar */
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 /* TODO: the following assumes the user has one ':bar' token */
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 /* add head to the complete string */
164 if(completeLength > 0)
165 complete = complete.slice(0, -1) + this.chars.head;
166
167 /* fill in the actual progress bar */
168 str = str.replace(':bar', complete + incomplete);
169
170 /* replace the extra tokens */
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 * "update" the progress bar to represent an exact percentage.
183 * The ratio (between 0 and 1) specified will be multiplied by `total` and
184 * floored, representing the closest available "tick." For example, if a
185 * progress bar has a length of 3 and `update(0.5)` is called, the progress
186 * will be set to 1.
187 *
188 * A ratio of 0.5 will attempt to set the progress to halfway.
189 *
190 * @param {number} ratio The ratio (between 0 and 1 inclusive) to set the
191 * overall completion to.
192 * @api public
193 */
194
195ProgressBar.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 * "interrupt" the progress bar and write a message above it.
204 * @param {string} message The message to write.
205 * @api public
206 */
207
208ProgressBar.prototype.interrupt = function (message) {
209 // clear the current line
210 this.stream.clearLine();
211 // move the cursor to the start of the line
212 this.stream.cursorTo(0);
213 // write the message text
214 this.stream.write(message);
215 // terminate the line after writing the message
216 this.stream.write('\n');
217 // re-display the progress bar with its lastDraw
218 this.stream.write(this.lastDraw);
219};
220
221/**
222 * Terminates a progress bar.
223 *
224 * @api public
225 */
226
227ProgressBar.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};