Codebase list golang-github-vbauerster-mpb / b4ecb5b
Progress: Add and MustAdd methods Nobody likes sudden panics, thus add opportunity to handle error. Vladimir Bauer 3 years ago
1 changed file(s) with 22 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
127127
128128 // New creates a bar by calling `Build` method on provided `BarFillerBuilder`.
129129 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) {
137150 if filler == nil {
138151 filler = NopStyle().Build()
139152 }
167180 bs.shutdownListeners = append(bs.shutdownListeners, d)
168181 }
169182 })
170 return bar
183 return bar, nil
171184 case <-p.done:
172 panic(DoneError)
185 return nil, DoneError
173186 }
174187 }
175188