Skip to content

Commit

Permalink
Progress: Add and MustAdd methods
Browse files Browse the repository at this point in the history
Nobody likes sudden panics,
thus add opportunity to handle error.
  • Loading branch information
vbauerster committed Mar 2, 2023
1 parent 6fb6daf commit b4ecb5b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,26 @@ func (p *Progress) AddSpinner(total int64, options ...BarOption) *Bar {

// New creates a bar by calling `Build` method on provided `BarFillerBuilder`.
func (p *Progress) New(total int64, builder BarFillerBuilder, options ...BarOption) *Bar {
return p.AddFiller(total, builder.Build(), options...)
return p.MustAdd(total, builder.Build(), options...)
}

// AddFiller creates a bar which renders itself by provided filler.
// If `total <= 0` triggering complete event by increment methods is disabled.
// Panics if *Progress instance is done, i.e. called after (*Progress).Wait().
func (p *Progress) AddFiller(total int64, filler BarFiller, options ...BarOption) *Bar {
// MustAdd creates a bar which renders itself by provided BarFiller.
// If `total <= 0` triggering complete event by increment methods is
// disabled. Panics if *Progress instance is done, i.e. called after
// (*Progress).Wait().
func (p *Progress) MustAdd(total int64, filler BarFiller, options ...BarOption) *Bar {
bar, err := p.Add(total, filler, options...)
if err != nil {
panic(err)
}
return bar
}

// Add creates a bar which renders itself by provided BarFiller.
// If `total <= 0` triggering complete event by increment methods
// is disabled. If *Progress instance is done, i.e. called after
// (*Progress).Wait(), return err == DoneError.
func (p *Progress) Add(total int64, filler BarFiller, options ...BarOption) (*Bar, error) {
if filler == nil {
filler = NopStyle().Build()
}
Expand Down Expand Up @@ -168,9 +181,9 @@ func (p *Progress) AddFiller(total int64, filler BarFiller, options ...BarOption
bs.shutdownListeners = append(bs.shutdownListeners, d)
}
})
return bar
return bar, nil
case <-p.done:
panic(DoneError)
return nil, DoneError
}
}

Expand Down

0 comments on commit b4ecb5b

Please sign in to comment.