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

fix: aimed to fix the ChangeMax not working problem. #192

Closed chengxilo closed 1 month ago

chengxilo commented 1 month ago

Hi, I saw the issue https://github.com/schollz/progressbar/issues/147 and after I have a look at the source code, I guess I can solve the problem. The problem was caused by the method NewOptions64 in which we do these when the max was setted as -1.

      if b.config.max == -1 {
        b.config.ignoreLength = true
        b.config.max = int64(b.config.width)
        b.config.predictTime = false
    }

but didn't restore this when the ChangeMax method was called.

I hope it would be helpful. 🥰

chengxilo commented 1 month ago
func main() {
    bar := progressbar.Default(-1)
    bar.ChangeMax(100)
    for i := 0; i < 100; i++ {
        bar.Add(1)
        time.Sleep(100 * time.Millisecond)
    }
    fmt.Println()
    bar2 := progressbar.Default(100)
    for i := 0; i < 100; i++ {
        bar2.Add(1)
        time.Sleep(100 * time.Millisecond)
    }
}

Top code can be used to test it. And here I can show you the difference if you want. before change: image

after change: image

chengxilo commented 1 month ago

I am sorry about that I am not really good at using github. I wrongly used the main branch to submit the pr and when I try to figure it out , I changed the branch name accidently closed my pull request. As I only changed a little bit of code, I guess delete my repo and create a new one with an extra branch for this can be a better choice. Sorry for contaminate your pr list.😭