1111exports = module . exports = ProgressBar ;
1212
1313/**
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`.
1616 *
1717 * Options:
1818 *
@@ -22,6 +22,7 @@ exports = module.exports = ProgressBar;
2222 * - `complete` completion character defaulting to "="
2323 * - `incomplete` incomplete character defaulting to "-"
2424 * - `callback` optional function to call when the progress bar completes
25+ * - `clear` will clear the progress bar upon termination
2526 *
2627 * Tokens:
2728 *
@@ -32,21 +33,13 @@ exports = module.exports = ProgressBar;
3233 * - `:percent` completion percentage
3334 * - `:eta` eta in seconds
3435 *
35- * @param {String } fmt
36- * @param {Object|Number } options or total
36+ * @param {string } fmt
37+ * @param {object|number } options or total
3738 * @api public
3839 */
3940
4041function ProgressBar ( fmt , options ) {
4142 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- } ;
5043
5144 if ( typeof ( options ) == 'number' ) {
5245 var total = options ;
@@ -64,19 +57,18 @@ function ProgressBar(fmt, options) {
6457 this . width = options . width || this . total ;
6558 this . clear = options . clear
6659 this . chars = {
67- complete : options . complete || '='
68- , incomplete : options . incomplete || '-'
60+ complete : options . complete || '=' ,
61+ incomplete : options . incomplete || '-'
6962 } ;
7063 this . callback = options . callback || function ( ) { } ;
7164 this . lastDraw = '' ;
7265}
7366
7467/**
75- * "tick" the progress bar with optional `len` and
76- * optional `tokens`.
68+ * "tick" the progress bar with optional `len` and optional `tokens`.
7769 *
78- * @param {Number|Object } len or tokens
79- * @param {Object } tokens
70+ * @param {number|object } len or tokens
71+ * @param {object } tokens
8072 * @api public
8173 */
8274
@@ -103,26 +95,24 @@ ProgressBar.prototype.tick = function(len, tokens){
10395} ;
10496
10597/**
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.
108100 *
109- * @param {Object } tokens
101+ * @param {object } tokens
110102 * @api public
111103 */
112104
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 ;
117107
118108 var ratio = this . curr / this . total ;
119109 ratio = Math . min ( Math . max ( ratio , 0 ) , 1 ) ;
120110
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 ) ;
126116
127117 complete = Array ( complete ) . join ( this . chars . complete ) ;
128118 incomplete = Array ( this . width - complete . length ) . join ( this . chars . incomplete ) ;
@@ -131,19 +121,17 @@ ProgressBar.prototype.render = function(tokens){
131121 . replace ( ':bar' , complete + incomplete )
132122 . replace ( ':current' , this . curr )
133123 . 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 ) )
136127 . replace ( ':percent' , percent . toFixed ( 0 ) + '%' ) ;
137128
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 ] ) ;
143130
144131 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 ) ;
147135 this . lastDraw = str ;
148136 }
149137} ;
@@ -157,12 +145,12 @@ ProgressBar.prototype.render = function(tokens){
157145 *
158146 * A ratio of 0.5 will attempt to set the progress to halfway.
159147 *
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.
162150 * @api public
163151 */
164152
165- ProgressBar . prototype . update = function ( ratio , tokens ) {
153+ ProgressBar . prototype . update = function ( ratio , tokens ) {
166154 var goal = Math . floor ( ratio * this . total ) ;
167155 var delta = goal - this . curr ;
168156
@@ -175,14 +163,9 @@ ProgressBar.prototype.update = function(ratio, tokens) {
175163 * @api public
176164 */
177165
178- ProgressBar . prototype . terminate = function ( ) {
179- this . rl . resume ( ) ;
180-
166+ ProgressBar . prototype . terminate = function ( ) {
181167 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 ( ) ;
188171} ;
0 commit comments