cli-progress
cli-progress copied to clipboard
Stop redraw when there is other logging
In the code Im running there is an exception so I log the messages. the problem is that it will break the bar and make it redraw. How to make the bar stick at the bottom of the terminal? Thanks in advanced!
I'm facing this issue too!
At the moment, the SingleBar does not allow additional logging messages during the progressbar's display. This is however available if you use the MultiBar which has a .log()
function which I believe is what you are looking for.
@gregormaclaine Thanks man, it works like a charm!
@gregormaclaine, thank you for the above suggestion!
For the most part, it worked for me as well.
However, I did notice a weird behavior: the very last message I log using MultiBar.log
is lost if I then call MultiBar.stop
right away.
After some experimentation, I noticed that adding a short sleep right before I call MultiBar.stop
helps that last logged message to appear, so this feels like some concurrency issue with how messages are flushed or something...
@andreiled I ran into this as well. It's because it buffers the logs, and only outputs them when the MultiBar.update()
is triggered, which is based on the throttleRate/FPS. I think this could be fixed by adding outputting of the log buffer in the stop()
routine here as that is meant to show final progress, but it seems simply forcing an update()
call on the MultiBar
before the stop()
seems to work. update()
is meant as an internal function (not a part of the API), but it's working for me.
@jcapcik, thank you for the suggestion!
I will try it out.
Here is my solution
class MyBar extends cliProgress.SingleBar {
constructor(total = 0, start = 0, suffix = '') {
super({
format: `{bar} {percentage}% | ETA: {eta_formatted} | {value}/{total}${suffix ? ' | ' + suffix : ''}`,
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591'
});
const consoleLoggers = allLoggers.filter(l => l.logToConsole);
this.on('redraw-post', () => consoleLoggers.forEach(l => l.insertNewLineOnce()));
this.start(total, start);
}
end() {
this.render();
this.removeAllListeners();
clearTimeout(this['timer']);
}
}
Crucial is calling insertNewLineOnce()
on('redraw-post')
, which tell a logger to print one new line symbol before next log message.