schollz / progressbar

A really basic thread-safe progress bar for Golang applications
https://pkg.go.dev/github.com/schollz/progressbar/v3?tab=doc
MIT License
4.08k stars 220 forks source link

Progress bar displays incorrectly if the operation is too quick. #155

Closed talwat closed 1 year ago

talwat commented 1 year ago

I'm trying to download some files, and some of them are small, and whenever a really small one is downloaded, the progressbar looks like this:

⠋ desc  [1s]

while it's supposed to be an actual bar, like this:

desc 100% [==============================================>] (17 MB/s)

Here's my config:

// Modified version of progressbar.DefaultBytes() to change the appearance.

bar := progressbar.NewOptions64(
    maxBytes,
    progressbar.OptionSetDescription(desc),
    progressbar.OptionSetWriter(os.Stderr),
    progressbar.OptionShowBytes(true),
    progressbar.OptionThrottle(65*time.Millisecond),
    progressbar.OptionOnCompletion(func() {
        log.RawLog("\n")
    }),
    progressbar.OptionSpinnerType(14),
    progressbar.OptionFullWidth(),
    progressbar.OptionSetRenderBlankState(true),
    progressbar.OptionSetPredictTime(false),

    progressbar.OptionEnableColorCodes(false),
    progressbar.OptionSetTheme(progressbar.Theme{
        Saucer:        "=",
        SaucerHead:    ">",
        AltSaucerHead: ">",
        SaucerPadding: " ",
        BarStart:      "[",
        BarEnd:        "]",
    }),
)

Any idea how I could fix this?

talwat commented 1 year ago

Also, here is a summary of my download function:

req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
resp, _ := http.DefaultClient.Do(req)

file, _ := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perms)

io.Copy(io.MultiWriter(file, bar), resp.Body)
talwat commented 1 year ago

Update: this turns out that resp.ContentLength was -1 for some reason, so progressbar used a spinner instead. I wish it would have thrown an error or something if I was expecting a progress bar but got a spinner.