Repository navigation
Expand file tree
/
Copy pathcontainer_option.go
More file actions
156 lines (138 loc) · 4.63 KB
/
Copy pathcontainer_option.go
File metadata and controls
156 lines (138 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package mpb
import (
"cmp"
"io"
"sync"
"time"
)
// ContainerOption is a func option to alter default behavior of a bar
// container. Container term refers to a Progress struct which can
// hold one or more Bars.
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.
//
// If a goroutine exits early (for example on error) before the bar
// reaches its total, call (*Bar).Abort on that bar so p.Wait()
// does not wait indefinitely for the bar to reach its total. The
// provided *sync.WaitGroup must still be balanced: call wg.Add(1)
// before starting each goroutine and defer wg.Done() inside it, even
// on the error path.
func WithWaitGroup(wg *sync.WaitGroup) ContainerOption {
return func(s *pState) {
s.uwg = wg
}
}
// WithWidth sets container width. If not set it defaults to terminal
// width. A bar added to the container will inherit its width, unless
// overridden by `func BarWidth(int) BarOption`.
func WithWidth(width int) ContainerOption {
return func(s *pState) {
s.reqWidth = width
}
}
// WithQueueLen sets buffer size of heap manager channel. Ideally it must be
// kept at MAX value, where MAX is number of bars to be rendered at the same
// time. Default queue len is 64.
func WithQueueLen(len int) ContainerOption {
return func(s *pState) {
s.hmQueueLen = len
}
}
// WithRefreshRate overrides default 150ms refresh rate.
func WithRefreshRate(d time.Duration) ContainerOption {
return func(s *pState) {
s.refreshRate = d
}
}
// WithManualRefresh disables internal auto refresh time.Ticker.
// Refresh will occur on value receive from provided ch, yet last bar
// will still trigger final refresh cycle on its completion or abortion,
// similar to last person switches tv off analogy here.
func WithManualRefresh(ch <-chan any) ContainerOption {
return func(s *pState) {
s.manualRC = ch
}
}
// WithRenderDelay delays rendering. By default rendering starts as
// soon as bar is added, with this option it's possible to delay
// rendering process by keeping provided chan unclosed. In other words
// rendering will start as soon as provided chan is closed.
func WithRenderDelay(ch <-chan any) ContainerOption {
return func(s *pState) {
s.delayRC = ch
}
}
// WithShutdownNotifier closes provided channel on shutdown event,
// i.e. after `(*Progress) Wait()` or `(*Progress) Shutdown()` call.
func WithShutdownNotifier(ch chan any) ContainerOption {
return func(s *pState) {
s.shutdownNotifier = ch
}
}
// WithOutput overrides default os.Stdout output. If underlying io.Writer
// is not a terminal then auto refresh is disabled unless WithAutoRefresh
// option is set.
func WithOutput(w io.Writer) ContainerOption {
return func(s *pState) {
s.output = cmp.Or(w, io.Discard)
}
}
// WithDebugOutput sets debug output. It's only used to write render error if any.
func WithDebugOutput(w io.Writer) ContainerOption {
return func(s *pState) {
s.debugOut = cmp.Or(w, io.Discard)
}
}
// WithConsoleWriter overrides default implementation of ConsoleWriter interface.
// This option makes following options ineffective:
// - WithOutput
// - ForceTTY
func WithConsoleWriter(cw ConsoleWriter) ContainerOption {
return func(s *pState) {
s.cwriter = cw
}
}
// WithAutoRefresh force auto refresh regardless of what output is set to.
// Applicable only if not WithManualRefresh set.
func WithAutoRefresh() ContainerOption {
return func(s *pState) {
s.autoRefresh = true
}
}
// ForceAutoRefresh is an alias of WithAutoRefresh.
func ForceAutoRefresh() ContainerOption {
return WithAutoRefresh()
}
// ForceTTY force treating output as tty.
// This one implicitly enables WithAutoRefresh unless WithManualRefresh specified.
// Can be handy if you need to wrap os.Stdout or os.Stderr for example like:
// mpb.WithOutput(io.MultiWriter(os.Stdout, &someTestBuf)).
func ForceTTY() ContainerOption {
return func(s *pState) {
s.forceTTY = true
}
}
// PopCompletedMode pop completed bars out of progress container.
// In this mode completed bars get moved to the top and stop
// participating in rendering cycle.
func PopCompletedMode() ContainerOption {
return func(s *pState) {
s.popCompleted = true
}
}
// ContainerOptional will return provided option only when cond is true.
func ContainerOptional(option ContainerOption, cond bool) ContainerOption {
if cond {
return option
}
return nil
}
// withDepleteHeap for test purposes only
func withDepleteHeap(ch chan<- *Bar) ContainerOption {
return func(s *pState) {
s.depleteHeap = ch
}
}