Permalink
Browse files

Working with readline impl, even on Windows

  • Loading branch information...
1 parent 0a66e71 commit 0011de1af0cac998d428ccfede3a18a5e0dbc697 @davglass davglass committed Sep 19, 2012
Showing with 13 additions and 14 deletions.
  1. +13 −14 lib/node-progress.js
View
@@ -8,12 +8,6 @@
* Expose `ProgressBar`.
*/
-var rl = require('readline').createInterface({
- input: process.stdin,
- output: process.stdout
-});
-rl.setPrompt('', 0);
-
exports = module.exports = ProgressBar;
/**
@@ -48,10 +42,15 @@ exports.version = '0.0.5';
*/
function ProgressBar(fmt, options) {
+ this.rl = require('readline').createInterface({
+ input: process.stdin,
+ output: options.stream || process.stdout
+ });
+ this.rl.setPrompt('', 0);
+
options = options || {};
if ('string' != typeof fmt) throw new Error('format required');
if ('number' != typeof options.total) throw new Error('total required');
- this.stream = options.stream || process.stdout;
this.fmt = fmt;
this.curr = 0;
this.total = options.total;
@@ -84,19 +83,19 @@ ProgressBar.prototype.tick = function(len, tokens){
// progress complete
if ((this.curr += len) > this.total) {
this.complete = true;
- rl.write(null, {ctrl: true, name: 'u'});
- rl.close();
+ //this.rl.write(null, {ctrl: true, name: 'u'});
+ this.rl.resume();
+ this.rl.close();
return;
}
var percent = this.curr / this.total * 100
, complete = Math.round(this.width * (this.curr / this.total))
- , incomplete = this.width - complete
+ , incomplete
, elapsed = new Date - this.start
, eta = elapsed * (this.total / this.curr - 1)
-
complete = Array(complete).join(this.chars.complete);
- incomplete = Array(incomplete).join(this.chars.incomplete);
+ incomplete = Array(this.width - complete.length).join(this.chars.incomplete);
var str = this.fmt
.replace(':bar', complete + incomplete)
@@ -112,6 +111,6 @@ ProgressBar.prototype.tick = function(len, tokens){
}
}
- rl.write(null, {ctrl: true, name: 'u'});
- rl.write(str);
+ this.rl.write(null, {ctrl: true, name: 'u'});
+ this.rl.write(str);
};

0 comments on commit 0011de1

Please sign in to comment.