| 8 | 8 |
"github.com/vbauerster/mpb/v4/cwriter"
|
| 9 | 9 |
)
|
| 10 | 10 |
|
| 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)
|
| 14 | 14 |
|
| 15 | 15 |
// WithWaitGroup provides means to have a single joint point. If
|
| 16 | 16 |
// *sync.WaitGroup is provided, you can safely call just p.Wait()
|
| 17 | 17 |
// without calling Wait() on provided *sync.WaitGroup. Makes sense
|
| 18 | 18 |
// when there are more than one bar to render.
|
| 19 | |
func WithWaitGroup(wg *sync.WaitGroup) ProgressOption {
|
|
19 |
func WithWaitGroup(wg *sync.WaitGroup) ContainerOption {
|
| 20 | 20 |
return func(s *pState) {
|
| 21 | 21 |
s.uwg = wg
|
| 22 | 22 |
}
|
|
| 24 | 24 |
|
| 25 | 25 |
// WithWidth sets container width. Default is 80. Bars inherit this
|
| 26 | 26 |
// width, as long as no BarWidth is applied.
|
| 27 | |
func WithWidth(w int) ProgressOption {
|
|
27 |
func WithWidth(w int) ContainerOption {
|
| 28 | 28 |
return func(s *pState) {
|
| 29 | 29 |
if w >= 0 {
|
| 30 | 30 |
s.width = w
|
|
| 33 | 33 |
}
|
| 34 | 34 |
|
| 35 | 35 |
// WithRefreshRate overrides default 120ms refresh rate.
|
| 36 | |
func WithRefreshRate(d time.Duration) ProgressOption {
|
|
36 |
func WithRefreshRate(d time.Duration) ContainerOption {
|
| 37 | 37 |
return func(s *pState) {
|
| 38 | 38 |
if d < 10*time.Millisecond {
|
| 39 | 39 |
return
|
|
| 44 | 44 |
|
| 45 | 45 |
// WithManualRefresh disables internal auto refresh time.Ticker.
|
| 46 | 46 |
// 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 {
|
| 48 | 48 |
return func(s *pState) {
|
| 49 | 49 |
s.manualRefreshCh = ch
|
| 50 | 50 |
}
|
| 51 | 51 |
}
|
| 52 | 52 |
|
| 53 | 53 |
// WithContext provided context will be used for cancellation purposes.
|
| 54 | |
func WithContext(ctx context.Context) ProgressOption {
|
|
54 |
func WithContext(ctx context.Context) ContainerOption {
|
| 55 | 55 |
return func(s *pState) {
|
| 56 | 56 |
if ctx == nil {
|
| 57 | 57 |
return
|
|
| 62 | 62 |
|
| 63 | 63 |
// WithShutdownNotifier provided chanel will be closed, after all bars
|
| 64 | 64 |
// have been rendered.
|
| 65 | |
func WithShutdownNotifier(ch chan struct{}) ProgressOption {
|
|
65 |
func WithShutdownNotifier(ch chan struct{}) ContainerOption {
|
| 66 | 66 |
return func(s *pState) {
|
| 67 | 67 |
s.shutdownNotifier = ch
|
| 68 | 68 |
}
|
| 69 | 69 |
}
|
| 70 | 70 |
|
| 71 | 71 |
// WithOutput overrides default output os.Stdout.
|
| 72 | |
func WithOutput(w io.Writer) ProgressOption {
|
|
72 |
func WithOutput(w io.Writer) ContainerOption {
|
| 73 | 73 |
return func(s *pState) {
|
| 74 | 74 |
if w == nil {
|
| 75 | 75 |
return
|
|
| 79 | 79 |
}
|
| 80 | 80 |
|
| 81 | 81 |
// WithDebugOutput sets debug output.
|
| 82 | |
func WithDebugOutput(w io.Writer) ProgressOption {
|
|
82 |
func WithDebugOutput(w io.Writer) ContainerOption {
|
| 83 | 83 |
return func(s *pState) {
|
| 84 | 84 |
if w == nil {
|
| 85 | 85 |
return
|
|
| 87 | 87 |
s.debugOut = w
|
| 88 | 88 |
}
|
| 89 | 89 |
}
|
|
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 |
}
|