11
11
exports = module . exports = ProgressBar ;
12
12
13
13
/**
14
- * Initialize a `ProgressBar` with the given
15
- * `fmt` string and `options` or ` total`.
14
+ * Initialize a `ProgressBar` with the given `fmt` string and `options` or
15
+ * `total`.
16
16
*
17
17
* Options:
18
18
*
@@ -22,6 +22,7 @@ exports = module.exports = ProgressBar;
22
22
* - `complete` completion character defaulting to "="
23
23
* - `incomplete` incomplete character defaulting to "-"
24
24
* - `callback` optional function to call when the progress bar completes
25
+ * - `clear` will clear the progress bar upon termination
25
26
*
26
27
* Tokens:
27
28
*
@@ -32,21 +33,13 @@ exports = module.exports = ProgressBar;
32
33
* - `:percent` completion percentage
33
34
* - `:eta` eta in seconds
34
35
*
35
- * @param {String } fmt
36
- * @param {Object|Number } options or total
36
+ * @param {string } fmt
37
+ * @param {object|number } options or total
37
38
* @api public
38
39
*/
39
40
40
41
function ProgressBar ( fmt , options ) {
41
42
this . stream = options . stream || process . stderr ;
42
- this . rl = require ( 'readline' ) . createInterface ( {
43
- input : process . stdin ,
44
- output : this . stream
45
- } ) ;
46
- this . rl . setPrompt ( '' , 0 ) ;
47
- this . rl . clearLine = function ( ) {
48
- this . write ( null , { ctrl : true , name : 'u' } ) ;
49
- } ;
50
43
51
44
if ( typeof ( options ) == 'number' ) {
52
45
var total = options ;
@@ -64,19 +57,18 @@ function ProgressBar(fmt, options) {
64
57
this . width = options . width || this . total ;
65
58
this . clear = options . clear
66
59
this . chars = {
67
- complete : options . complete || '='
68
- , incomplete : options . incomplete || '-'
60
+ complete : options . complete || '=' ,
61
+ incomplete : options . incomplete || '-'
69
62
} ;
70
63
this . callback = options . callback || function ( ) { } ;
71
64
this . lastDraw = '' ;
72
65
}
73
66
74
67
/**
75
- * "tick" the progress bar with optional `len` and
76
- * optional `tokens`.
68
+ * "tick" the progress bar with optional `len` and optional `tokens`.
77
69
*
78
- * @param {Number|Object } len or tokens
79
- * @param {Object } tokens
70
+ * @param {number|object } len or tokens
71
+ * @param {object } tokens
80
72
* @api public
81
73
*/
82
74
@@ -103,26 +95,24 @@ ProgressBar.prototype.tick = function(len, tokens){
103
95
} ;
104
96
105
97
/**
106
- * Method to render the progress bar with optional `tokens` to
107
- * place in the progress bar's `fmt` field.
98
+ * Method to render the progress bar with optional `tokens` to place in the
99
+ * progress bar's `fmt` field.
108
100
*
109
- * @param {Object } tokens
101
+ * @param {object } tokens
110
102
* @api public
111
103
*/
112
104
113
- ProgressBar . prototype . render = function ( tokens ) {
114
- if ( ! this . stream . isTTY ) {
115
- return ;
116
- }
105
+ ProgressBar . prototype . render = function ( tokens ) {
106
+ if ( ! this . stream . isTTY ) return ;
117
107
118
108
var ratio = this . curr / this . total ;
119
109
ratio = Math . min ( Math . max ( ratio , 0 ) , 1 ) ;
120
110
121
- var percent = ratio * 100
122
- , complete = Math . round ( this . width * ratio )
123
- , incomplete
124
- , elapsed = new Date - this . start
125
- , eta = ( percent == 100 ) ? 0 : elapsed * ( this . total / this . curr - 1 ) ;
111
+ var percent = ratio * 100 ;
112
+ var complete = Math . round ( this . width * ratio ) ;
113
+ var incomplete ;
114
+ var elapsed = new Date - this . start ;
115
+ var eta = ( percent == 100 ) ? 0 : elapsed * ( this . total / this . curr - 1 ) ;
126
116
127
117
complete = Array ( complete ) . join ( this . chars . complete ) ;
128
118
incomplete = Array ( this . width - complete . length ) . join ( this . chars . incomplete ) ;
@@ -131,19 +121,17 @@ ProgressBar.prototype.render = function(tokens){
131
121
. replace ( ':bar' , complete + incomplete )
132
122
. replace ( ':current' , this . curr )
133
123
. replace ( ':total' , this . total )
134
- . replace ( ':elapsed' , isNaN ( elapsed ) ? "0.0" : ( elapsed / 1000 ) . toFixed ( 1 ) )
135
- . replace ( ':eta' , ( isNaN ( eta ) || ! isFinite ( eta ) ) ? "0.0" : ( eta / 1000 ) . toFixed ( 1 ) )
124
+ . replace ( ':elapsed' , isNaN ( elapsed ) ? '0.0' : ( elapsed / 1000 ) . toFixed ( 1 ) )
125
+ . replace ( ':eta' , ( isNaN ( eta ) || ! isFinite ( eta ) ) ? '0.0' : ( eta / 1000 )
126
+ . toFixed ( 1 ) )
136
127
. replace ( ':percent' , percent . toFixed ( 0 ) + '%' ) ;
137
128
138
- if ( tokens ) {
139
- for ( var key in tokens ) {
140
- str = str . replace ( ':' + key , tokens [ key ] ) ;
141
- }
142
- }
129
+ if ( tokens ) for ( var key in tokens ) str = str . replace ( ':' + key , tokens [ key ] ) ;
143
130
144
131
if ( this . lastDraw !== str ) {
145
- this . rl . clearLine ( ) ;
146
- this . rl . write ( str ) ;
132
+ this . stream . clearLine ( ) ;
133
+ this . stream . cursorTo ( 0 ) ;
134
+ this . stream . write ( str ) ;
147
135
this . lastDraw = str ;
148
136
}
149
137
} ;
@@ -157,12 +145,12 @@ ProgressBar.prototype.render = function(tokens){
157
145
*
158
146
* A ratio of 0.5 will attempt to set the progress to halfway.
159
147
*
160
- * @param {Number } ratio The ratio (between 0 and 1 inclusive) to set the
161
- * overall completion to.
148
+ * @param {number } ratio The ratio (between 0 and 1 inclusive) to set the
149
+ * overall completion to.
162
150
* @api public
163
151
*/
164
152
165
- ProgressBar . prototype . update = function ( ratio , tokens ) {
153
+ ProgressBar . prototype . update = function ( ratio , tokens ) {
166
154
var goal = Math . floor ( ratio * this . total ) ;
167
155
var delta = goal - this . curr ;
168
156
@@ -175,14 +163,9 @@ ProgressBar.prototype.update = function(ratio, tokens) {
175
163
* @api public
176
164
*/
177
165
178
- ProgressBar . prototype . terminate = function ( ) {
179
- this . rl . resume ( ) ;
180
-
166
+ ProgressBar . prototype . terminate = function ( ) {
181
167
if ( this . clear ) {
182
- this . rl . clearLine ( ) ;
183
- this . rl . close ( ) ;
184
- } else {
185
- this . rl . close ( ) ;
186
- console . log ( ) ;
187
- }
168
+ this . stream . clearLine ( ) ;
169
+ this . stream . cursorTo ( 0 ) ;
170
+ } else console . log ( ) ;
188
171
} ;
0 commit comments