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.14k stars 224 forks source link

`ChangeMax(-1)` not setting length to unknown #206

Closed MegaRedHand closed 5 days ago

MegaRedHand commented 5 days ago

Minimum example:

import "github.com/schollz/progressbar/v3"

func main() {
    pb := progressbar.NewOptions(
        -1,
        progressbar.OptionSetMaxDetailRow(1),
    )
    pb.ChangeMax(-1)
    // this fails with 
    if err = pb.RenderBlank(); err != nil {
        return err
    }
}

It fails with the message:

panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/schollz/progressbar/v3.(*ProgressBar).render(0x1400011e508)
    /Users/mega/.asdf/installs/golang/1.22.4/packages/pkg/mod/github.com/schollz/progressbar/v3@v3.17.0/progressbar.go:904 +0x4ac
github.com/schollz/progressbar/v3.(*ProgressBar).RenderBlank(0x1400011e508?)
    /Users/mega/.asdf/installs/golang/1.22.4/packages/pkg/mod/github.com/schollz/progressbar/v3@v3.17.0/progressbar.go:597 +0xec
(rest omitted)

This is due to the call to ChangeMax(-1) calling lengthKnown(-1) instead of lengthUnknown().

MegaRedHand commented 5 days ago

I opened a PR with the fix. Just for context: I'm setting the length to -1 because reuse the same ProgressBar for multiple sequential stages.