Setting output to nil, disables progress bar
Vladimir Bauer
7 years ago
| 2 | 2 | import ( |
| 3 | 3 | "context" |
| 4 | 4 | "io" |
| 5 | "io/ioutil" | |
| 5 | 6 | "sync" |
| 6 | 7 | "time" |
| 7 | 8 | ) |
| 24 | 25 | // width, as long as no BarWidth is applied. |
| 25 | 26 | func WithWidth(w int) ContainerOption { |
| 26 | 27 | return func(s *pState) { |
| 27 | if w >= 0 { | |
| 28 | s.width = w | |
| 28 | if w < 0 { | |
| 29 | return | |
| 29 | 30 | } |
| 31 | s.width = w | |
| 30 | 32 | } |
| 31 | 33 | } |
| 32 | 34 | |
| 33 | 35 | // WithRefreshRate overrides default 120ms refresh rate. |
| 34 | 36 | func WithRefreshRate(d time.Duration) ContainerOption { |
| 35 | 37 | return func(s *pState) { |
| 36 | if d < 10*time.Millisecond { | |
| 37 | return | |
| 38 | } | |
| 39 | 38 | s.rr = d |
| 40 | 39 | } |
| 41 | 40 | } |
| 66 | 65 | } |
| 67 | 66 | } |
| 68 | 67 | |
| 69 | // WithOutput overrides default output os.Stdout. | |
| 68 | // WithOutput overrides default os.Stdout output. Setting it to nil will | |
| 69 | // effectively disable auto refresh rate and discard any output, usefull | |
| 70 | // if you want to disable progress bars with little overhead. | |
| 70 | 71 | func WithOutput(w io.Writer) ContainerOption { |
| 71 | 72 | return func(s *pState) { |
| 72 | 73 | if w == nil { |
| 74 | s.manualRefreshCh = make(chan time.Time) | |
| 75 | s.output = ioutil.Discard | |
| 73 | 76 | return |
| 74 | 77 | } |
| 75 | 78 | s.output = w |