Codebase list golang-github-vbauerster-mpb / d98ee94
refactoring: ContainerOption and ContainerOptionOnCondition Vladimir Bauer 7 years ago
5 changed file(s) with 25 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
2727 name := fmt.Sprintf("Bar#%d:", i)
2828 bar := p.AddBar(int64(total),
2929 // set BarWidth 40 for bar 1 and 2
30 mpb.OptionOnCondition(mpb.BarWidth(40), func() bool { return i > 0 }),
30 mpb.BarOptionOnCondition(mpb.BarWidth(40), func() bool { return i > 0 }),
3131 mpb.PrependDecorators(
3232 // simple name decorator
3333 decor.Name(name),
2323 for i := 0; i < numBars; i++ {
2424 name := fmt.Sprintf("Bar#%d:", i)
2525 b := p.AddBar(int64(total), mpb.BarID(i),
26 mpb.OptionOnCondition(mpb.BarRemoveOnComplete(), func() bool { return i == 0 }),
26 mpb.BarOptionOnCondition(mpb.BarRemoveOnComplete(), func() bool { return i == 0 }),
2727 mpb.PrependDecorators(
2828 decor.Name(name),
2929 decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace),
148148 }
149149 }
150150
151 // OptionOnCondition returns option when condition evaluates to true.
152 func OptionOnCondition(option BarOption, condition func() bool) BarOption {
151 // BarOptionOnCondition returns option when condition evaluates to true.
152 func BarOptionOnCondition(option BarOption, condition func() bool) BarOption {
153153 if condition() {
154154 return option
155155 }
88 "github.com/vbauerster/mpb/v4/cwriter"
99 )
1010
11 // ProgressOption is a function option which changes the default
12 // behavior of progress pool, if passed to mpb.New(...ProgressOption).
13 type ProgressOption func(*pState)
11 // ContainerOption is a function option which changes the default
12 // behavior of progress container, if passed to mpb.New(...ContainerOption).
13 type ContainerOption func(*pState)
1414
1515 // WithWaitGroup provides means to have a single joint point. If
1616 // *sync.WaitGroup is provided, you can safely call just p.Wait()
1717 // without calling Wait() on provided *sync.WaitGroup. Makes sense
1818 // when there are more than one bar to render.
19 func WithWaitGroup(wg *sync.WaitGroup) ProgressOption {
19 func WithWaitGroup(wg *sync.WaitGroup) ContainerOption {
2020 return func(s *pState) {
2121 s.uwg = wg
2222 }
2424
2525 // WithWidth sets container width. Default is 80. Bars inherit this
2626 // width, as long as no BarWidth is applied.
27 func WithWidth(w int) ProgressOption {
27 func WithWidth(w int) ContainerOption {
2828 return func(s *pState) {
2929 if w >= 0 {
3030 s.width = w
3333 }
3434
3535 // WithRefreshRate overrides default 120ms refresh rate.
36 func WithRefreshRate(d time.Duration) ProgressOption {
36 func WithRefreshRate(d time.Duration) ContainerOption {
3737 return func(s *pState) {
3838 if d < 10*time.Millisecond {
3939 return
4444
4545 // WithManualRefresh disables internal auto refresh time.Ticker.
4646 // Refresh will occur upon receive value from provided ch.
47 func WithManualRefresh(ch <-chan time.Time) ProgressOption {
47 func WithManualRefresh(ch <-chan time.Time) ContainerOption {
4848 return func(s *pState) {
4949 s.manualRefreshCh = ch
5050 }
5151 }
5252
5353 // WithContext provided context will be used for cancellation purposes.
54 func WithContext(ctx context.Context) ProgressOption {
54 func WithContext(ctx context.Context) ContainerOption {
5555 return func(s *pState) {
5656 if ctx == nil {
5757 return
6262
6363 // WithShutdownNotifier provided chanel will be closed, after all bars
6464 // have been rendered.
65 func WithShutdownNotifier(ch chan struct{}) ProgressOption {
65 func WithShutdownNotifier(ch chan struct{}) ContainerOption {
6666 return func(s *pState) {
6767 s.shutdownNotifier = ch
6868 }
6969 }
7070
7171 // WithOutput overrides default output os.Stdout.
72 func WithOutput(w io.Writer) ProgressOption {
72 func WithOutput(w io.Writer) ContainerOption {
7373 return func(s *pState) {
7474 if w == nil {
7575 return
7979 }
8080
8181 // WithDebugOutput sets debug output.
82 func WithDebugOutput(w io.Writer) ProgressOption {
82 func WithDebugOutput(w io.Writer) ContainerOption {
8383 return func(s *pState) {
8484 if w == nil {
8585 return
8787 s.debugOut = w
8888 }
8989 }
90
91 // ContainerOptionOnCondition returns option when condition evaluates to true.
92 func ContainerOptionOnCondition(option ContainerOption, condition func() bool) ContainerOption {
93 if condition() {
94 return option
95 }
96 return nil
97 }
5050 }
5151
5252 // New creates new Progress instance, which orchestrates bars rendering
53 // process. Accepts mpb.ProgressOption funcs for customization.
54 func New(options ...ProgressOption) *Progress {
53 // process. Accepts mpb.ContainerOption funcs for customization.
54 func New(options ...ContainerOption) *Progress {
5555 pq := make(priorityQueue, 0)
5656 heap.Init(&pq)
5757 s := &pState{