vbauerster / mpb

multi progress bar for Go cli applications
The Unlicense
2.29k stars 123 forks source link

The log will be overwritten while the progress bar is printed #105

Closed zhijian-pro closed 1 year ago

zhijian-pro commented 2 years ago

The log will be overwritten while the progress bar is printed. How to set not to overwrite the log, but to move the progress bar down one line?

package main

import (
    "time"

    "github.com/sirupsen/logrus"

    "github.com/vbauerster/mpb/v7"
    "github.com/vbauerster/mpb/v7/decor"
)

var logger *logrus.Logger

func main() {
    logger = logrus.New()
    total := 10000
    p := mpb.New(mpb.WithWidth(64), mpb.WithOutput(logger.Writer()))
    b := p.AddBar(0, // disable triggerComplete
        mpb.PrependDecorators(
            decor.Name("test count: ", decor.WCSyncWidth),
            decor.CountersNoUnit("%d / %d"),
        ),
        mpb.AppendDecorators(
            decor.OnComplete(decor.Percentage(decor.WC{W: 5}), "done"),
            decor.OnComplete(
                decor.AverageETA(decor.ET_STYLE_GO, decor.WC{W: 6}), "",
            ),
        ),
    )
    b.SetTotal(int64(total), false)

    for {
        logger.Infof(time.Now().String())
        time.Sleep(1 * time.Second)
        b.Increment()
    }
    b.SetTotal(0, true)
}
vbauerster commented 2 years ago

Mixing logs and progress bars at the same time is a bad idea IMHO. Log is supposed to be printed to hard media like paper in order to be read later, while progress bar can't be printed to a paper due to its interactive nature. This is the main reason I will not accept your PR #106 no matter how good it is.

zhijian-pro commented 2 years ago

What you say is also reasonable, but undeniably, progress bar and log record both have prompt function on terminal.And logs can express information that progress bars cannot replace. For example, an exception occurs in the ninth order. This exception only affects the ninth order, not the others, so the application should continue to run, But it should also be immediately obvious to the user that there is a problem with the ninth that needs attention. You can also see that many people have feedback on similar problems under many progress bar frameworks, which shows that this demand is common.

venth commented 2 years ago

I believe that my case is similar as @zhijian-pro. I'm looking for a multi progress bar which is capable of tracing a progress and present information about matching records. Such a solution exists in indicatif. indicatif exposes a function println which prints a message above a single or multi progress bar.

What do you think @vbauerster about extending current progress bar api by supporting of printing a message?

vbauerster commented 2 years ago

If you need to print something above a bar while it's running, please refer to this example.

Such a solution exists in indicatif. indicatif exposes a function println which prints a message above a single or multi progress bar.

You forgot to count unresolved issues related to that feature.

venth commented 2 years ago

Thanks for the answer and pointing out the example. I’ll take a look.

You forgot to count unresolved issues related to that feature.

That’s so true for both - forgetting and number of issues :) :) some of the issues I’ve omitted by creating a printing sequence. So it’s done in one thread.

For now, I’ve got tired of borrowing and moving in rust and went to golang. That’s why this library got my attention.

wohenbushuang commented 1 year ago

If you need to print something above a bar while it's running, please refer to this example.

It's complicated and seems the contents should be predefined. It will print everytime the progressbar refreshed, which may not the same as what we need (the behaviors like npm install).

vbauerster commented 1 year ago

Implemented in v8. https://pkg.go.dev/github.com/vbauerster/mpb/v8#Progress.Write https://github.com/vbauerster/mpb/blob/master/_examples/progressAsWriter/main.go