Progress: Add and MustAdd methods
Nobody likes sudden panics,
thus add opportunity to handle error.
Vladimir Bauer
3 years ago
| 127 | 127 | |
| 128 | 128 | // New creates a bar by calling `Build` method on provided `BarFillerBuilder`. |
| 129 | 129 | func (p *Progress) New(total int64, builder BarFillerBuilder, options ...BarOption) *Bar { |
| 130 | return p.AddFiller(total, builder.Build(), options...) | |
| 131 | } | |
| 132 | ||
| 133 | // AddFiller creates a bar which renders itself by provided filler. | |
| 134 | // If `total <= 0` triggering complete event by increment methods is disabled. | |
| 135 | // Panics if *Progress instance is done, i.e. called after (*Progress).Wait(). | |
| 136 | func (p *Progress) AddFiller(total int64, filler BarFiller, options ...BarOption) *Bar { | |
| 130 | return p.MustAdd(total, builder.Build(), options...) | |
| 131 | } | |
| 132 | ||
| 133 | // MustAdd creates a bar which renders itself by provided BarFiller. | |
| 134 | // If `total <= 0` triggering complete event by increment methods is | |
| 135 | // disabled. Panics if *Progress instance is done, i.e. called after | |
| 136 | // (*Progress).Wait(). | |
| 137 | func (p *Progress) MustAdd(total int64, filler BarFiller, options ...BarOption) *Bar { | |
| 138 | bar, err := p.Add(total, filler, options...) | |
| 139 | if err != nil { | |
| 140 | panic(err) | |
| 141 | } | |
| 142 | return bar | |
| 143 | } | |
| 144 | ||
| 145 | // Add creates a bar which renders itself by provided BarFiller. | |
| 146 | // If `total <= 0` triggering complete event by increment methods | |
| 147 | // is disabled. If *Progress instance is done, i.e. called after | |
| 148 | // (*Progress).Wait(), return err == DoneError. | |
| 149 | func (p *Progress) Add(total int64, filler BarFiller, options ...BarOption) (*Bar, error) { | |
| 137 | 150 | if filler == nil { |
| 138 | 151 | filler = NopStyle().Build() |
| 139 | 152 | } |
| 167 | 180 | bs.shutdownListeners = append(bs.shutdownListeners, d) |
| 168 | 181 | } |
| 169 | 182 | }) |
| 170 | return bar | |
| 183 | return bar, nil | |
| 171 | 184 | case <-p.done: |
| 172 | panic(DoneError) | |
| 185 | return nil, DoneError | |
| 173 | 186 | } |
| 174 | 187 | } |
| 175 | 188 | |