Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added beforeUpdateRendering and afterStop callbacks #58

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ The following options can be changed
- `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)
- `forceRedraw` (type:boolean) - trigger redraw on every frame even if progress remains the same; can be useful if progress bar gets overwritten by other concurrent writes to the terminal (default: false)
- `beforeUpdateRendering` (type:function) - if given, this callback is called for each update of MultiBar, just before bars are rendered (default: false)
- `afterStop` (type:function) - if given, this callback is called after stop of MultiBar (default: false)

Bar Formatting
-----------------------------------
Expand Down
10 changes: 10 additions & 0 deletions lib/multi-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ module.exports = class MultiBar{
// reset cursor
this.terminal.cursorRelativeReset();

// allow custom output before rendering all bars
if (this.options.beforeUpdateRendering !== false) {
this.options.beforeUpdateRendering(this.terminal);
}

// update each bar
for (let i=0; i< this.bars.length; i++){
// add new line ?
Expand Down Expand Up @@ -173,5 +178,10 @@ module.exports = class MultiBar{
// new line on complete
this.terminal.newline();
}

// allow custom output/cleanup after stop
if (this.options.afterStop !== false) {
this.options.afterStop(this.terminal);
}
}
}
6 changes: 6 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ module.exports = {
// force bar redraw even if progress did not change
_options.forceRedraw = mergeOption(opt.forceRedraw, false);

// callback before rendering bars in update method, gets terminal as first argument (terminal => {})
_options.beforeUpdateRendering = mergeOption(opt.beforeUpdateRendering, false);

// callback after stop, gets terminal as first argument (terminal => {})
_options.afterStop = mergeOption(opt.afterStop, false);

return _options;
},

Expand Down