Skip to content

Commit

Permalink
added log() convenience method to multibar #111
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDittrich committed Apr 30, 2022
1 parent 17b9c64 commit c1b9375
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 3.11.0 ###

* Added: `log()` convenience method the multibar to enable custom logging output on top of the progress bars during operation
* Added: `gracefulExit` option (enabled by default) to stop the bars in case of `SIGINT` or `SIGTERM` - this restores most cursor settings before exiting

### 3.10.0 ###
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Features
* Works in Asynchronous and Synchronous tasks
* Preset/Theme support
* Custom bar formatters (via callback)
* Logging during multibar operation

Usage
------------
Expand Down Expand Up @@ -223,6 +224,18 @@ Stops the all progress bars
<instance>.stop();
```

### ::log() ###

Outputs (buffered) content on top of the multibars during operation.

**Notice: newline at the end is required**

Example: [example-logging.js](examples/example-logging.js)

```js
<instance>.log("Hello World\n");
```

Options
-----------------------------------

Expand Down
64 changes: 64 additions & 0 deletions examples/example-logging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const _progress = require('../cli-progress');

const files = {
'eta.js ': 187,
'generic-bar.js': 589,
'multi-bar.js ': 5342,
'options.js ': 42,
'single-bar.js ': 2123,
'terminal.js ': 4123
};
const bars = [];

// create new container
const multibar = new _progress.MultiBar({
format: ' {bar} | "{file}" | {value}/{total}',
hideCursor: true,
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',

// only the bars will be cleared, not the logged content
clearOnComplete: true,
stopOnComplete: true,

// important! redraw everything to avoid "empty" completed bars
forceRedraw: true
});

console.log("Downloading files..\n");

// add bars
for (const filename in files){
const size = files[filename];

bars.push(multibar.create(size, 0, {file: filename}));
}

const timer = setInterval(function(){

// increment
for (let i=0; i<bars.length;i++){
const bar = bars[i];

// download complete ?
if (bar.value < bar.total){
bar.increment();
}
}

// progress bar running ?
// check "isActive" property in case you've enabled "stopOnComplete" !
if (multibar.isActive === false){
clearInterval(timer);

//multibar.stop();
console.log('Download complete!')
}
}, 3);

let numMessages = 0;

const loggingTimer = setInterval(function(){
// don't forget to add a linebreak !!!
multibar.log(`[${(new Date()).toString()}] I'm logging message #${numMessages++}\n`);
}, 1500);
4 changes: 2 additions & 2 deletions lib/generic-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = class GenericBar extends _EventEmitter{
}

// internal render function
render(){
render(forceRendering=false){
// calculate the normalized current progress
let progress = (this.value/this.total);

Expand Down Expand Up @@ -78,7 +78,7 @@ module.exports = class GenericBar extends _EventEmitter{
// format string
const s = this.formatter(this.options, params, this.payload);

const forceRedraw = this.options.forceRedraw
const forceRedraw = forceRendering || this.options.forceRedraw
// force redraw in notty-mode!
|| (this.options.noTTYOutput && !this.terminal.isTTY());

Expand Down
18 changes: 18 additions & 0 deletions lib/multi-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports = class MultiBar extends _EventEmitter{
// update interval
this.schedulingRate = (this.terminal.isTTY() ? this.options.throttleTime : this.options.notTTYSchedule);

// logging output buffer
this.loggingBuffer = [];

// add handler to restore cursor settings (stop the bar) on SIGINT/SIGTERM ?
if (this.options.gracefulExit){
process.once('SIGINT', this.stop.bind(this));
Expand Down Expand Up @@ -119,6 +122,16 @@ module.exports = class MultiBar extends _EventEmitter{
// trigger event
this.emit('redraw-pre');

// content within logging buffer ?
if (this.loggingBuffer.length > 0){
this.terminal.clearLine();

// flush logging buffer and write content to terminal
while (this.loggingBuffer.length > 0){
this.terminal.write(this.loggingBuffer.shift(), true);
}
}

// update each bar
for (let i=0; i< this.bars.length; i++){
// add new line ?
Expand Down Expand Up @@ -204,4 +217,9 @@ module.exports = class MultiBar extends _EventEmitter{
// trigger event
this.emit('stop');
}

log(s){
// push content into logging buffer
this.loggingBuffer.push(s);
}
}
4 changes: 2 additions & 2 deletions lib/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ class Terminal{

// write content to output stream
// @TODO use string-width to strip length
write(s){
write(s, rawWrite=false){
// line wrapping enabled ? trim output
if (this.linewrap === true){
if (this.linewrap === true && rawWrite === false){
this.stream.write(s.substr(0, this.getWidth()));
}else{
this.stream.write(s);
Expand Down

0 comments on commit c1b9375

Please sign in to comment.