vbauerster / mpb

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

Panic with different number of columns #14

Closed elbaro closed 6 years ago

elbaro commented 6 years ago

mpb panics when multiple bars have different number of prependers / appenders.

from progress_posix.go

b0 := s.bars[0]
if numP == -1 {
    numP = b0.NumOfPrependers()
}

This assumes all bars use the same number of prependers / appenders. It can be fixed in a naive (slow) way that calculates the maxmium number of columns every tick:

// if(numP==-1) <-- numP can be changed if you add/remove a bar.

for _, bar := range s.bars {
    num := bar.NumOfPrependers()
    if num > numP {
        numP = num
    }
}
vbauerster commented 6 years ago

What is your use case for having bars with different number of prependers / appenders ?

vbauerster commented 6 years ago

Should be fixed now. Can you please check?

elbaro commented 6 years ago

In my case I have a million urls to download. There is a global bar to show the total progress (# of urls completed) with the remaining time appended, and 10 bars for 10 concurrent downloads with urls prepended and speeds (MB/s) appended.

For example,

(1)
Total Download:   |=====     | 113686 / 1000000 (xx %)
https://...1.zip |=========  | (2.3 MB/s)
https://...2.zip |=========  | (152 KB/s)
..
https://...10.zip |=========  | (30 KB/s)

The 10 bars should be aligned but the global bar is not.

If 2.zip is completed first, other bars' position remain and 2.zip is replaced with new progress for 11.zip. (#13)

(2)
Total Download:   |=====     | 113687 / 1000000 (xx %)
https://...1.zip |=========  | (2.3 MB/s)
https://...11.zip |===       | (200 KB/s)
..
https://...10.zip |=========  | (30 KB/s)

If requesting 11.zip returns an error, it is replaced by a retry bar.

(3)
Total Download:   |=====     | 113687 / 1000000 (xx %)
https://...1.zip |=========  | (2.3 MB/s)
https://...11.zip | Retrying in 3 seconds.. |  (2/5)
..
https://...10.zip |=========  | (30 KB/s)

This requires putting the text within the bar. Asking for (3) may be too much, but I think scenarios like this (having different kinds of bars and multiple alignment groups) are common.

elbaro commented 6 years ago

I wrote a simple test and It works now. https://asciinema.org/a/f0DzxVe6pojSSRmchgGAVumKO