EnableTriggerComplete without total <= 0 check
Enables completing zero total bar without calling SetTotal(0, true).
Vladimir Bauer
1 year, 11 months ago
| 172 | 172 |
}
|
| 173 | 173 |
|
| 174 | 174 |
// EnableTriggerComplete enables triggering complete event. It's effective
|
| 175 | |
// only for bars which were constructed with `total <= 0` and after total
|
| 176 | |
// has been set with `(*Bar).SetTotal(int64, false)`. If `curren >= total`
|
|
175 |
// only for bars which were constructed with `total <= 0`. If `curren >= total`
|
| 177 | 176 |
// at the moment of call, complete event is triggered right away.
|
| 178 | 177 |
func (b *Bar) EnableTriggerComplete() {
|
| 179 | 178 |
select {
|
| 180 | 179 |
case b.operateState <- func(s *bState) {
|
| 181 | |
if s.triggerComplete || s.total <= 0 {
|
|
180 |
if s.triggerComplete {
|
| 182 | 181 |
return
|
| 183 | 182 |
}
|
| 184 | 183 |
if s.current >= s.total {
|
| 61 | 61 |
bar.SetTotal(0, true)
|
| 62 | 62 |
if !bar.Completed() {
|
| 63 | 63 |
t.Error("expected bar to complete")
|
|
64 |
}
|
|
65 |
|
|
66 |
p.Wait()
|
|
67 |
}
|
|
68 |
|
|
69 |
func TestBarEnableTriggerCompleteZeroBar(t *testing.T) {
|
|
70 |
p := mpb.New(mpb.WithWidth(80), mpb.WithOutput(io.Discard))
|
|
71 |
bar := p.AddBar(0) // never complete bar
|
|
72 |
|
|
73 |
if bar.Completed() {
|
|
74 |
t.Fail()
|
|
75 |
}
|
|
76 |
|
|
77 |
// Calling bar.SetTotal(0, true) has same effect
|
|
78 |
// but this one is more concise and intuitive
|
|
79 |
bar.EnableTriggerComplete()
|
|
80 |
|
|
81 |
if !bar.Completed() {
|
|
82 |
t.Fail()
|
| 64 | 83 |
}
|
| 65 | 84 |
|
| 66 | 85 |
p.Wait()
|