| 59 | 59 |
conf pConf
|
| 60 | 60 |
}
|
| 61 | 61 |
|
| 62 | |
// New creates new Progress instance, which will orchestrate bars rendering
|
| 63 | |
// process. It acceepts context.Context, for cancellation.
|
| 64 | |
// If you don't plan to cancel, it is safe to feed with nil
|
| 65 | |
func New() *Progress {
|
| 66 | |
p := &Progress{
|
| 67 | |
wg: new(sync.WaitGroup),
|
| 68 | |
done: make(chan struct{}),
|
| 69 | |
ops: make(chan func(*pConf)),
|
| 70 | |
stopReqCh: make(chan struct{}),
|
| 71 | |
}
|
| 72 | |
go p.server(pConf{
|
|
62 |
// New creates new Progress instance, which orchestrates bars rendering process.
|
|
63 |
// Accepts mpb.ProgressOption funcs for customization.
|
|
64 |
func New(options ...ProgressOption) *Progress {
|
|
65 |
// defaults
|
|
66 |
conf := pConf{
|
| 73 | 67 |
bars: make([]*Bar, 0, 3),
|
| 74 | 68 |
width: pwidth,
|
| 75 | 69 |
format: pformat,
|
| 76 | 70 |
cw: cwriter.New(os.Stdout),
|
| 77 | 71 |
rr: prr,
|
| 78 | 72 |
ticker: time.NewTicker(prr),
|
| 79 | |
})
|
|
73 |
}
|
|
74 |
|
|
75 |
for _, opt := range options {
|
|
76 |
opt(&conf)
|
|
77 |
}
|
|
78 |
|
|
79 |
p := &Progress{
|
|
80 |
wg: new(sync.WaitGroup),
|
|
81 |
done: make(chan struct{}),
|
|
82 |
ops: make(chan func(*pConf)),
|
|
83 |
stopReqCh: make(chan struct{}),
|
|
84 |
}
|
|
85 |
go p.server(conf)
|
| 80 | 86 |
return p
|
| 81 | 87 |
}
|
| 82 | 88 |
|
| 83 | |
// WithCancel cancellation via channel.
|
| 84 | |
// You have to call p.Stop() anyway, after cancel.
|
| 85 | |
// Pancis, if nil channel is passed.
|
|
89 |
// WithCancel Deprecated, use mpb.WithCancel
|
| 86 | 90 |
func (p *Progress) WithCancel(ch <-chan struct{}) *Progress {
|
| 87 | 91 |
if ch == nil {
|
| 88 | 92 |
panic("nil cancel channel")
|
|
| 92 | 96 |
})
|
| 93 | 97 |
}
|
| 94 | 98 |
|
| 95 | |
// SetWidth overrides default (80) width of bar(s).
|
|
99 |
// SetWidth Deprecated, use mpb.WithWidth
|
| 96 | 100 |
func (p *Progress) SetWidth(width int) *Progress {
|
| 97 | 101 |
if width < 2 {
|
| 98 | 102 |
return p
|
|
| 102 | 106 |
})
|
| 103 | 107 |
}
|
| 104 | 108 |
|
| 105 | |
// SetOut sets underlying writer of progress. Default one is os.Stdout.
|
|
109 |
// SetOut Deprecated, use mpb.Output
|
| 106 | 110 |
func (p *Progress) SetOut(w io.Writer) *Progress {
|
| 107 | 111 |
if w == nil {
|
| 108 | 112 |
return p
|
|
| 113 | 117 |
})
|
| 114 | 118 |
}
|
| 115 | 119 |
|
| 116 | |
// RefreshRate overrides default (100ms) refresh rate value
|
|
120 |
// RefreshRate Deprecated, use mpb.WithRefreshRate
|
| 117 | 121 |
func (p *Progress) RefreshRate(d time.Duration) *Progress {
|
| 118 | 122 |
return updateConf(p, func(c *pConf) {
|
| 119 | 123 |
c.ticker.Stop()
|
| 120 | 124 |
c.ticker = time.NewTicker(d)
|
| 121 | 125 |
c.rr = d
|
| 122 | |
})
|
| 123 | |
}
|
| 124 | |
|
| 125 | |
// BeforeRenderFunc accepts a func, which gets called before render process.
|
| 126 | |
func (p *Progress) BeforeRenderFunc(f BeforeRender) *Progress {
|
| 127 | |
return updateConf(p, func(c *pConf) {
|
| 128 | |
c.beforeRender = f
|
| 129 | 126 |
})
|
| 130 | 127 |
}
|
| 131 | 128 |
|
|
| 188 | 185 |
}
|
| 189 | 186 |
}
|
| 190 | 187 |
|
| 191 | |
// ShutdownNotify means to be notified when main rendering goroutine quits, usualy after p.Stop() call.
|
| 192 | |
func (p *Progress) ShutdownNotify(ch chan struct{}) *Progress {
|
| 193 | |
return updateConf(p, func(c *pConf) {
|
| 194 | |
c.shutdownNotifier = ch
|
| 195 | |
})
|
| 196 | |
}
|
| 197 | |
|
| 198 | |
// Format sets custom format for underlying bar(s), default one is "[=>-]".
|
|
188 |
// Format Deprecated, use mpb.WithFormat
|
| 199 | 189 |
func (p *Progress) Format(format string) *Progress {
|
| 200 | 190 |
if utf8.RuneCountInString(format) != numFmtRunes {
|
| 201 | 191 |
return p
|