Comparing version 1.0.1 to 1.1.0
@@ -20,2 +20,3 @@ /*! | ||
* - `total` total number of ticks to complete | ||
* - `width` the displayed width of the progress bar defaulting to total | ||
* - `stream` the output stream defaulting to stdout | ||
@@ -56,2 +57,3 @@ * - `complete` completion character defaulting to "=" | ||
this.width = options.width || this.total; | ||
this.clear = options.clear | ||
this.chars = { | ||
@@ -82,16 +84,27 @@ complete: options.complete || '=' | ||
this.curr += len | ||
this.render(tokens); | ||
// progress complete | ||
if ((this.curr += len) >= this.total) { | ||
if (this.curr >= this.total) { | ||
this.complete = true; | ||
this.rl.clearLine(); | ||
this.rl.resume(); | ||
this.rl.close(); | ||
this.terminate(); | ||
return; | ||
} | ||
}; | ||
var percent = this.curr / this.total * 100 | ||
/** | ||
* Method to render the progress bar with optional `tokens` to | ||
* place in the progress bar's `fmt` field. | ||
* | ||
* @param {Object} tokens | ||
* @api public | ||
*/ | ||
ProgressBar.prototype.render = function(tokens){ | ||
var percent = (this.curr >= this.total) ? 100 : this.curr / this.total * 100 | ||
, complete = Math.round(this.width * (this.curr / this.total)) | ||
, incomplete | ||
, elapsed = new Date - this.start | ||
, eta = elapsed * (this.total / this.curr - 1) | ||
, eta = (percent == 100) ? 0 : elapsed * (this.total / this.curr - 1) | ||
complete = Array(complete).join(this.chars.complete); | ||
@@ -104,4 +117,4 @@ incomplete = Array(this.width - complete.length).join(this.chars.incomplete); | ||
.replace(':total', this.total) | ||
.replace(':elapsed', (elapsed / 1000).toFixed(1)) | ||
.replace(':eta', (eta / 1000).toFixed(1)) | ||
.replace(':elapsed', isNaN(elapsed) ? "0.0" : (elapsed / 1000).toFixed(1)) | ||
.replace(':eta', isNaN(eta) ? "0.0" : (eta / 1000).toFixed(1)) | ||
.replace(':percent', percent.toFixed(0) + '%'); | ||
@@ -118,1 +131,40 @@ | ||
}; | ||
/** | ||
* "update" the progress bar to represent an exact percentage. | ||
* The ratio (between 0 and 1) specified will be multiplied by `total` and | ||
* floored, representing the closest available "tick." For example, if a | ||
* progress bar has a length of 3 and `update(0.5)` is called, the progress | ||
* will be set to 1. | ||
* | ||
* A ratio of 0.5 will attempt to set the progress to halfway. | ||
* | ||
* @param {Number} ratio The ratio (between 0 and 1 inclusive) to set the | ||
* overall completion to. | ||
* @api public | ||
*/ | ||
ProgressBar.prototype.update = function(ratio) { | ||
var goal = Math.floor(ratio * this.total); | ||
var delta = goal - this.curr; | ||
this.tick(delta); | ||
}; | ||
/** | ||
* Terminates a progress bar. | ||
* | ||
* @api public | ||
*/ | ||
ProgressBar.prototype.terminate = function() { | ||
this.rl.resume(); | ||
if (this.clear) { | ||
this.rl.clearLine(); | ||
this.rl.close(); | ||
} else { | ||
this.rl.close(); | ||
console.log(); | ||
} | ||
}; |
{ | ||
"name": "progress" | ||
, "version": "1.0.1" | ||
, "version": "1.1.0" | ||
, "description": "Flexible ascii progress bar" | ||
@@ -10,2 +10,3 @@ , "keywords": ["cli", "progress"] | ||
, "engines": { "node": ">=0.4.0" } | ||
, "repository": "git://github.com/visionmedia/node-progress" | ||
} |
@@ -13,3 +13,3 @@ # node-progress | ||
as well as the `total`, telling the progress bar when it will | ||
be considered complete. After that all we need to do is `tick()` appropriately. | ||
be considered complete. After that all we need to do is `tick()` appropriately. | ||
@@ -21,3 +21,3 @@ ```javascript | ||
var timer = setInterval(function(){ | ||
bar.tick(); | ||
bar.tick(); | ||
if (bar.complete) { | ||
@@ -33,5 +33,7 @@ console.log('\ncomplete\n'); | ||
- `total` total number of ticks to complete | ||
- `width` the displayed width of the progress bar defaulting to total | ||
- `stream` the output stream defaulting to stdout | ||
- `complete` completion character defaulting to "=" | ||
- `incomplete` incomplete character defaulting to "-" | ||
- `clear` option to clear the bar on completion defaulting to false | ||
@@ -51,3 +53,3 @@ ## Tokens: | ||
In our download example each tick has a variable influence, so we pass the chunk length which adjusts the progress bar appropriately relative to the total length. | ||
In our download example each tick has a variable influence, so we pass the chunk length which adjusts the progress bar appropriately relative to the total length. | ||
@@ -94,3 +96,3 @@ ```javascript | ||
## License | ||
## License | ||
@@ -118,2 +120,2 @@ (The MIT License) | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
8688
143
116
1