Skip to content

Commit

Permalink
Bugifx: synchronous update may cause unexpected behaviour on multibars
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDittrich committed Oct 2, 2019
1 parent f334a2d commit 98b0d2d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Branch 3.x ##

### 3.3.1 ###

* Bugifx: synchronous update may cause unexpected behaviour on multibars - limited to single bars
* Changed: renamed internal eta `push()` method to `update()`
* Changed: moved internal eta calculation call into `update()`

### 3.3.0 ###

* Added: option to pass custom formatters as callback via `options.format`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ The following options can be changed
- `hideCursor` (type:boolean) - hide the cursor during progress operation; restored on complete (default: false) - pass `null` to keep terminal settings
- `linewrap` (type:boolean) - disable line wrapping (default: false) - pass `null` to keep terminal settings; pass `true` to add linebreaks automatically (not recommended)
- `etaBuffer` (type:int) - number of updates with which to calculate the eta; higher numbers give a more stable eta (default: 10)
- `synchronousUpdate` (type:boolean) - trigger redraw during `update()` in case threshold time x2 is exceeded (default: true)
- `synchronousUpdate` (type:boolean) - trigger redraw during `update()` in case threshold time x2 is exceeded (default: true) - limited to single bar usage
- `noTTYOutput` (type:boolean) - enable scheduled output to notty streams - e.g. redirect to files (default: false)
- `notTTYSchedule` (type:int) - set the output schedule/interval for notty output in `ms` (default: 2000ms)
- `emptyOnZero` (type:boolean) - display progress bars with 'total' of zero(0) as empty, not full (default: false)
Expand Down
7 changes: 5 additions & 2 deletions lib/eta.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ETA{

constructor(length, initTime, initValue){
// size of eta buffer
this.etaBufferLength = length || 10;
this.etaBufferLength = length || 100;

// eta buffer with initial values
this.valueBuffer = [initValue];
Expand All @@ -15,9 +15,12 @@ class ETA{
}

// add new values to calculation buffer
push(time, value){
update(time, value, total){
this.valueBuffer.push(value);
this.timeBuffer.push(time);

// trigger recalculation
this.calculate(total-value);
}

// fetch estimated time
Expand Down
14 changes: 2 additions & 12 deletions lib/generic-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,15 @@ module.exports = class GenericBar{
// update value
this.value = current;

// add new value
this.eta.push(Date.now(), current);
// add new value; recalculate eta
this.eta.update(Date.now(), current, this.total);

// merge payload
const payloadData = payload || {};
for (const key in payloadData){
this.payload[key] = payloadData[key];
}

// number of remaining elements required for calculation
this.eta.calculate(this.total-this.value);

// trigger synchronous update ?
// check for throttel time
if (this.options.synchronousUpdate && (this.lastRedraw + this.options.throttleTime*2) < Date.now()){
// force update
this.render();
}

// limit reached ? autostop set ?
if (this.value >= this.getTotal() && this.options.stopOnComplete) {
this.stop();
Expand Down
7 changes: 7 additions & 0 deletions lib/single-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ module.exports = class SingleBar extends _GenericBar{
}

super.update(current, payload);

// trigger synchronous update ?
// check for throttel time
if (this.options.synchronousUpdate && (this.lastRedraw + this.options.throttleTime*2) < Date.now()){
// force update
this.render();
}
}

// start the progress bar
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cli-progress",
"version": "3.3.0",
"version": "3.3.1",
"description": "easy to use progress-bar for command-line/terminal applications",
"keywords": [
"cli",
Expand Down

0 comments on commit 98b0d2d

Please sign in to comment.