123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- exports = module.exports = ProgressBar;
- function ProgressBar(fmt, options) {
- this.stream = options.stream || process.stderr;
- if (typeof(options) == 'number') {
- var total = options;
- options = {};
- options.total = total;
- } else {
- options = options || {};
- if ('string' != typeof fmt) throw new Error('format required');
- if ('number' != typeof options.total) throw new Error('total required');
- }
- this.fmt = fmt;
- this.curr = options.curr || 0;
- this.total = options.total;
- this.width = options.width || this.total;
- this.clear = options.clear
- this.chars = {
- complete : options.complete || '=',
- incomplete : options.incomplete || '-',
- head : options.head || (options.complete || '=')
- };
- this.renderThrottle = options.renderThrottle !== 0 ? (options.renderThrottle || 16) : 0;
- this.lastRender = -Infinity;
- this.callback = options.callback || function () {};
- this.tokens = {};
- this.lastDraw = '';
- }
- ProgressBar.prototype.tick = function(len, tokens){
- if (len !== 0)
- len = len || 1;
-
- if ('object' == typeof len) tokens = len, len = 1;
- if (tokens) this.tokens = tokens;
-
- if (0 == this.curr) this.start = new Date;
- this.curr += len
-
- this.render();
-
- if (this.curr >= this.total) {
- this.render(undefined, true);
- this.complete = true;
- this.terminate();
- this.callback(this);
- return;
- }
- };
- ProgressBar.prototype.render = function (tokens, force) {
- force = force !== undefined ? force : false;
- if (tokens) this.tokens = tokens;
- if (!this.stream.isTTY) return;
- var now = Date.now();
- var delta = now - this.lastRender;
- if (!force && (delta < this.renderThrottle)) {
- return;
- } else {
- this.lastRender = now;
- }
- var ratio = this.curr / this.total;
- ratio = Math.min(Math.max(ratio, 0), 1);
- var percent = Math.floor(ratio * 100);
- var incomplete, complete, completeLength;
- var elapsed = new Date - this.start;
- var eta = (percent == 100) ? 0 : elapsed * (this.total / this.curr - 1);
- var rate = this.curr / (elapsed / 1000);
-
- var str = this.fmt
- .replace(':current', this.curr)
- .replace(':total', this.total)
- .replace(':elapsed', isNaN(elapsed) ? '0.0' : (elapsed / 1000).toFixed(1))
- .replace(':eta', (isNaN(eta) || !isFinite(eta)) ? '0.0' : (eta / 1000)
- .toFixed(1))
- .replace(':percent', percent.toFixed(0) + '%')
- .replace(':rate', Math.round(rate));
-
- var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length);
- if(availableSpace && process.platform === 'win32'){
- availableSpace = availableSpace - 1;
- }
- var width = Math.min(this.width, availableSpace);
-
- completeLength = Math.round(width * ratio);
- complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);
- incomplete = Array(Math.max(0, width - completeLength + 1)).join(this.chars.incomplete);
-
- if(completeLength > 0)
- complete = complete.slice(0, -1) + this.chars.head;
-
- str = str.replace(':bar', complete + incomplete);
-
- if (this.tokens) for (var key in this.tokens) str = str.replace(':' + key, this.tokens[key]);
- if (this.lastDraw !== str) {
- this.stream.cursorTo(0);
- this.stream.write(str);
- this.stream.clearLine(1);
- this.lastDraw = str;
- }
- };
- ProgressBar.prototype.update = function (ratio, tokens) {
- var goal = Math.floor(ratio * this.total);
- var delta = goal - this.curr;
- this.tick(delta, tokens);
- };
- ProgressBar.prototype.interrupt = function (message) {
-
- this.stream.clearLine();
-
- this.stream.cursorTo(0);
-
- this.stream.write(message);
-
- this.stream.write('\n');
-
- this.stream.write(this.lastDraw);
- };
- ProgressBar.prototype.terminate = function () {
- if (this.clear) {
- if (this.stream.clearLine) {
- this.stream.clearLine();
- this.stream.cursorTo(0);
- }
- } else {
- this.stream.write('\n');
- }
- };
|