cli-progress icon indicating copy to clipboard operation
cli-progress copied to clipboard

Stop redraw when there is other logging

Open hootpy opened this issue 1 year ago • 7 comments

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!

hootpy avatar Jul 11 '23 20:07 hootpy

I'm facing this issue too!

Kaveh-ap avatar Aug 16 '23 16:08 Kaveh-ap

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 avatar Aug 16 '23 22:08 gregormaclaine

@gregormaclaine Thanks man, it works like a charm!

Kaveh-ap avatar Aug 19 '23 08:08 Kaveh-ap

@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 avatar Nov 24 '23 17:11 andreiled

@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 avatar Dec 18 '23 05:12 jcapcik

@jcapcik, thank you for the suggestion!
I will try it out.

andreiled avatar Dec 18 '23 16:12 andreiled

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.

pawelzwronek avatar Aug 25 '24 17:08 pawelzwronek