diff --git a/bar.go b/bar.go index 46ea5da..a61ddab 100644 --- a/bar.go +++ b/bar.go @@ -32,13 +32,13 @@ shutdown chan struct{} } -type filler interface { +type Filler interface { fill(w io.Writer, width int, s *decor.Statistics) } type ( bState struct { - filler filler + filler Filler id int width int alignment int @@ -75,12 +75,20 @@ } ) -func newBar(wg *sync.WaitGroup, id, width int, total int64, cancel <-chan struct{}, options ...BarOption) *Bar { +func newBar( + wg *sync.WaitGroup, + filler Filler, + id, width int, + total int64, + cancel <-chan struct{}, + options ...BarOption, +) *Bar { if total <= 0 { total = time.Now().Unix() } s := &bState{ + filler: filler, id: id, priority: id, width: width, diff --git a/progress.go b/progress.go index 2af6f69..79c0a9b 100644 --- a/progress.go +++ b/progress.go @@ -81,39 +81,27 @@ // AddBar creates a new progress bar and adds to the container. func (p *Progress) AddBar(total int64, options ...BarOption) *Bar { - // make sure filler is initialized first - args := []BarOption{ - func(s *bState) { - s.filler = &barFiller{ - format: defaultBarStyle, - } - }, - } - args = append(args, options...) - return p.add(total, args...) + filler := &barFiller{ + format: defaultBarStyle, + } + return p.Add(total, filler, options...) } // AddSpinner creates a new spinner bar and adds to the container. func (p *Progress) AddSpinner(total int64, alignment SpinnerAlignment, options ...BarOption) *Bar { - // make sure filler is initialized first - args := []BarOption{ - func(s *bState) { - s.filler = &spinnerFiller{ - frames: defaultSpinnerStyle, - alignment: alignment, - } - }, - } - args = append(args, options...) - return p.add(total, args...) -} - -func (p *Progress) add(total int64, options ...BarOption) *Bar { + filler := &spinnerFiller{ + frames: defaultSpinnerStyle, + alignment: alignment, + } + return p.Add(total, filler, options...) +} + +func (p *Progress) Add(total int64, filler Filler, options ...BarOption) *Bar { p.wg.Add(1) result := make(chan *Bar) select { case p.operateState <- func(s *pState) { - b := newBar(p.wg, s.idCounter, s.width, total, s.cancel, options...) + b := newBar(p.wg, filler, s.idCounter, s.width, total, s.cancel, options...) if b.runningBar != nil { s.waitBars[b.runningBar] = b } else {