diff --git a/_examples/differentWidth/main.go b/_examples/differentWidth/main.go index 9171a68..fc82dd6 100644 --- a/_examples/differentWidth/main.go +++ b/_examples/differentWidth/main.go @@ -28,7 +28,7 @@ name := fmt.Sprintf("Bar#%d:", i) bar := p.AddBar(int64(total), // set BarWidth 40 for bar 1 and 2 - mpb.OptionOnCondition(mpb.BarWidth(40), func() bool { return i > 0 }), + mpb.BarOptionOnCondition(mpb.BarWidth(40), func() bool { return i > 0 }), mpb.PrependDecorators( // simple name decorator decor.Name(name), diff --git a/_examples/remove/main.go b/_examples/remove/main.go index 38dd8af..f67ef3c 100644 --- a/_examples/remove/main.go +++ b/_examples/remove/main.go @@ -24,7 +24,7 @@ for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) b := p.AddBar(int64(total), mpb.BarID(i), - mpb.OptionOnCondition(mpb.BarRemoveOnComplete(), func() bool { return i == 0 }), + mpb.BarOptionOnCondition(mpb.BarRemoveOnComplete(), func() bool { return i == 0 }), mpb.PrependDecorators( decor.Name(name), decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace), diff --git a/bar_option.go b/bar_option.go index b85a99d..67f5bce 100644 --- a/bar_option.go +++ b/bar_option.go @@ -149,8 +149,8 @@ } } -// OptionOnCondition returns option when condition evaluates to true. -func OptionOnCondition(option BarOption, condition func() bool) BarOption { +// BarOptionOnCondition returns option when condition evaluates to true. +func BarOptionOnCondition(option BarOption, condition func() bool) BarOption { if condition() { return option } diff --git a/options.go b/options.go index d1e2182..d1f30c4 100644 --- a/options.go +++ b/options.go @@ -9,15 +9,15 @@ "github.com/vbauerster/mpb/v4/cwriter" ) -// ProgressOption is a function option which changes the default -// behavior of progress pool, if passed to mpb.New(...ProgressOption). -type ProgressOption func(*pState) +// ContainerOption is a function option which changes the default +// behavior of progress container, if passed to mpb.New(...ContainerOption). +type ContainerOption func(*pState) // WithWaitGroup provides means to have a single joint point. If // *sync.WaitGroup is provided, you can safely call just p.Wait() // without calling Wait() on provided *sync.WaitGroup. Makes sense // when there are more than one bar to render. -func WithWaitGroup(wg *sync.WaitGroup) ProgressOption { +func WithWaitGroup(wg *sync.WaitGroup) ContainerOption { return func(s *pState) { s.uwg = wg } @@ -25,7 +25,7 @@ // WithWidth sets container width. Default is 80. Bars inherit this // width, as long as no BarWidth is applied. -func WithWidth(w int) ProgressOption { +func WithWidth(w int) ContainerOption { return func(s *pState) { if w >= 0 { s.width = w @@ -34,7 +34,7 @@ } // WithRefreshRate overrides default 120ms refresh rate. -func WithRefreshRate(d time.Duration) ProgressOption { +func WithRefreshRate(d time.Duration) ContainerOption { return func(s *pState) { if d < 10*time.Millisecond { return @@ -45,14 +45,14 @@ // WithManualRefresh disables internal auto refresh time.Ticker. // Refresh will occur upon receive value from provided ch. -func WithManualRefresh(ch <-chan time.Time) ProgressOption { +func WithManualRefresh(ch <-chan time.Time) ContainerOption { return func(s *pState) { s.manualRefreshCh = ch } } // WithContext provided context will be used for cancellation purposes. -func WithContext(ctx context.Context) ProgressOption { +func WithContext(ctx context.Context) ContainerOption { return func(s *pState) { if ctx == nil { return @@ -63,14 +63,14 @@ // WithShutdownNotifier provided chanel will be closed, after all bars // have been rendered. -func WithShutdownNotifier(ch chan struct{}) ProgressOption { +func WithShutdownNotifier(ch chan struct{}) ContainerOption { return func(s *pState) { s.shutdownNotifier = ch } } // WithOutput overrides default output os.Stdout. -func WithOutput(w io.Writer) ProgressOption { +func WithOutput(w io.Writer) ContainerOption { return func(s *pState) { if w == nil { return @@ -80,7 +80,7 @@ } // WithDebugOutput sets debug output. -func WithDebugOutput(w io.Writer) ProgressOption { +func WithDebugOutput(w io.Writer) ContainerOption { return func(s *pState) { if w == nil { return @@ -88,3 +88,11 @@ s.debugOut = w } } + +// ContainerOptionOnCondition returns option when condition evaluates to true. +func ContainerOptionOnCondition(option ContainerOption, condition func() bool) ContainerOption { + if condition() { + return option + } + return nil +} diff --git a/progress.go b/progress.go index eee86ee..a5b0d83 100644 --- a/progress.go +++ b/progress.go @@ -51,8 +51,8 @@ } // New creates new Progress instance, which orchestrates bars rendering -// process. Accepts mpb.ProgressOption funcs for customization. -func New(options ...ProgressOption) *Progress { +// process. Accepts mpb.ContainerOption funcs for customization. +func New(options ...ContainerOption) *Progress { pq := make(priorityQueue, 0) heap.Init(&pq) s := &pState{