Skip to content

Commit

Permalink
allowed 0 (zero) value for total/progress initialization #11
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDittrich committed Aug 2, 2017
1 parent ccce482 commit 7605d2c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -6,4 +6,5 @@ gfx/*
PublicHtml/*
node_modules*
.tmp*
intern_*
intern_*
dev.js
3 changes: 3 additions & 0 deletions CHANGES.md
@@ -1,3 +1,6 @@
### 1.5.0 ###
* Added: **0** values for total/progress initialization are allowed - feature requested by [jfmmm on GitHub](https://github.com/AndiDittrich/Node.CLI-Progress/issues/11) #11

### 1.4.0 ###
* Added: **Preset/Theme support**. Different bar-styles can be loaded from internal library (in addition to full customization)
* Added: Dependency **colors** for colorized progress bars
Expand Down
6 changes: 4 additions & 2 deletions lib/Bar.js
Expand Up @@ -71,7 +71,8 @@ Bar.prototype.render = function(){
var s = this.format;

// calculate the normalized current progress
var progress = this.value/this.total;
// handle NaN Errors
var progress = (this.value/this.total) || 1.0;

This comment has been minimized.

Copy link
@erikkallen

erikkallen Sep 4, 2017

Contributor

This does not work as expected since 0/1 || 1 = 1, so the progressbar can never be 0 onless you use a value like 0.000001 Ithink we need an if statement here

This comment has been minimized.

Copy link
@AndiDittrich

AndiDittrich Sep 4, 2017

Author Member

hi erik,
thanks for your report. you're right, this edge case doesn't work at all...i've used it as quick&dirty solution to avoid the NaN Error in case total=0 was set...

could you please open a new issue ?

br, Andi


// limiter
progress = Math.min(Math.max(progress, 0.0), 1.0);
Expand Down Expand Up @@ -150,7 +151,8 @@ Bar.prototype.formatTime = function (t, roundTo) {
Bar.prototype.start = function(total, startValue){
// set initial values
this.value = startValue || 0;
this.total = total || 100;
this.total = (typeof total !== 'undefined' && total >= 0) ? total : 100;

this.startTime = Date.now();
this.lastDrawnString = '';

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "cli-progress",
"version": "1.4.1",
"version": "1.5.0",
"description": "Easy to use Progress-Bar for Command-Line/Terminal Applications",
"keywords": [
"cli",
Expand Down

0 comments on commit 7605d2c

Please sign in to comment.