| 20 | 20 |
// are called after (*Progress).Stop() has been called
|
| 21 | 21 |
var ErrCallAfterStop = errors.New("method call on stopped Progress instance")
|
| 22 | 22 |
|
|
23 |
// default RefreshRate
|
|
24 |
var rr = 100 * time.Millisecond
|
|
25 |
|
| 23 | 26 |
type (
|
| 24 | 27 |
// BeforeRender is a func, which gets called before render process
|
| 25 | 28 |
BeforeRender func([]*Bar)
|
|
| 35 | 38 |
listen []chan int
|
| 36 | 39 |
result []chan int
|
| 37 | 40 |
}
|
|
41 |
|
|
42 |
// config changeable by user
|
|
43 |
userConf struct {
|
|
44 |
width int
|
|
45 |
format string
|
|
46 |
beforeRender BeforeRender
|
|
47 |
cw *cwriter.Writer
|
|
48 |
ticker *time.Ticker
|
|
49 |
|
|
50 |
shutdownNotifier chan struct{}
|
|
51 |
cancel <-chan struct{}
|
|
52 |
}
|
| 38 | 53 |
)
|
| 39 | 54 |
|
| 40 | 55 |
const (
|
|
| 43 | 58 |
)
|
| 44 | 59 |
|
| 45 | 60 |
const (
|
| 46 | |
// default RefreshRate
|
| 47 | |
rr = 100
|
| 48 | 61 |
// default width
|
| 49 | |
pwidth = 70
|
|
62 |
pwidth = 80
|
|
63 |
// default format
|
|
64 |
pformat = "[=>-]"
|
| 50 | 65 |
// number of format runes for bar
|
| 51 | 66 |
numFmtRunes = 5
|
| 52 | 67 |
)
|
| 53 | 68 |
|
| 54 | 69 |
// Progress represents the container that renders Progress bars
|
| 55 | 70 |
type Progress struct {
|
| 56 | |
// Context for canceling bars rendering
|
| 57 | |
// ctx context.Context
|
| 58 | 71 |
// WaitGroup for internal rendering sync
|
| 59 | 72 |
wg *sync.WaitGroup
|
| 60 | 73 |
|
| 61 | |
width int
|
| 62 | |
format string
|
| 63 | |
|
| 64 | |
bCommandCh chan *bCommandData
|
| 65 | |
rrChangeReqCh chan time.Duration
|
| 66 | |
outChangeReqCh chan io.Writer
|
| 67 | |
barCountReqCh chan chan int
|
| 68 | |
brCh chan BeforeRender
|
| 69 | |
done chan struct{}
|
| 70 | |
beforeStop chan struct{}
|
| 71 | |
cancel <-chan struct{}
|
|
74 |
done chan struct{}
|
|
75 |
userConf chan userConf
|
|
76 |
bCommandCh chan *bCommandData
|
|
77 |
barCountReqCh chan chan int
|
|
78 |
beforeStop chan struct{}
|
| 72 | 79 |
}
|
| 73 | 80 |
|
| 74 | 81 |
// New creates new Progress instance, which will orchestrate bars rendering
|
|
| 76 | 83 |
// If you don't plan to cancel, it is safe to feed with nil
|
| 77 | 84 |
func New() *Progress {
|
| 78 | 85 |
p := &Progress{
|
| 79 | |
width: pwidth,
|
| 80 | |
bCommandCh: make(chan *bCommandData),
|
| 81 | |
rrChangeReqCh: make(chan time.Duration),
|
| 82 | |
outChangeReqCh: make(chan io.Writer),
|
| 83 | |
barCountReqCh: make(chan chan int),
|
| 84 | |
brCh: make(chan BeforeRender),
|
| 85 | |
done: make(chan struct{}),
|
| 86 | |
beforeStop: make(chan struct{}),
|
| 87 | |
wg: new(sync.WaitGroup),
|
| 88 | |
}
|
| 89 | |
go p.server()
|
| 90 | |
return p
|
| 91 | |
}
|
| 92 | |
|
| 93 | |
// WithCancel cancellation via channel
|
|
86 |
wg: new(sync.WaitGroup),
|
|
87 |
done: make(chan struct{}),
|
|
88 |
userConf: make(chan userConf),
|
|
89 |
bCommandCh: make(chan *bCommandData),
|
|
90 |
barCountReqCh: make(chan chan int),
|
|
91 |
beforeStop: make(chan struct{}),
|
|
92 |
}
|
|
93 |
go p.server(userConf{
|
|
94 |
width: pwidth,
|
|
95 |
format: pformat,
|
|
96 |
cw: cwriter.New(os.Stdout),
|
|
97 |
ticker: time.NewTicker(rr),
|
|
98 |
})
|
|
99 |
return p
|
|
100 |
}
|
|
101 |
|
|
102 |
// WithCancel cancellation via channel.
|
|
103 |
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
|
|
104 |
// or nil channel passed
|
| 94 | 105 |
func (p *Progress) WithCancel(ch <-chan struct{}) *Progress {
|
|
106 |
if isClosed(p.done) {
|
|
107 |
panic(ErrCallAfterStop)
|
|
108 |
}
|
| 95 | 109 |
if ch == nil {
|
| 96 | 110 |
panic("nil cancel channel")
|
| 97 | 111 |
}
|
| 98 | |
p2 := new(Progress)
|
| 99 | |
*p2 = *p
|
| 100 | |
p2.cancel = ch
|
| 101 | |
return p2
|
| 102 | |
}
|
| 103 | |
|
| 104 | |
// SetWidth overrides default (70) width of bar(s)
|
| 105 | |
func (p *Progress) SetWidth(n int) *Progress {
|
| 106 | |
if n < 0 {
|
| 107 | |
panic("negative width")
|
| 108 | |
}
|
| 109 | |
p2 := new(Progress)
|
| 110 | |
*p2 = *p
|
| 111 | |
p2.width = n
|
| 112 | |
return p2
|
| 113 | |
}
|
| 114 | |
|
| 115 | |
// SetOut sets underlying writer of progress. Default is os.Stdout
|
|
112 |
conf := <-p.userConf
|
|
113 |
conf.cancel = ch
|
|
114 |
p.userConf <- conf
|
|
115 |
return p
|
|
116 |
}
|
|
117 |
|
|
118 |
// SetWidth overrides default (70) width of bar(s).
|
|
119 |
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
|
|
120 |
func (p *Progress) SetWidth(width int) *Progress {
|
|
121 |
if isClosed(p.done) {
|
|
122 |
panic(ErrCallAfterStop)
|
|
123 |
}
|
|
124 |
if width < 0 {
|
|
125 |
return p
|
|
126 |
}
|
|
127 |
conf := <-p.userConf
|
|
128 |
conf.width = width
|
|
129 |
p.userConf <- conf
|
|
130 |
return p
|
|
131 |
}
|
|
132 |
|
|
133 |
// SetOut sets underlying writer of progress. Default one is os.Stdout.
|
| 116 | 134 |
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
|
| 117 | 135 |
func (p *Progress) SetOut(w io.Writer) *Progress {
|
| 118 | 136 |
if isClosed(p.done) {
|
|
| 121 | 139 |
if w == nil {
|
| 122 | 140 |
return p
|
| 123 | 141 |
}
|
| 124 | |
p.outChangeReqCh <- w
|
|
142 |
conf := <-p.userConf
|
|
143 |
conf.cw.Flush()
|
|
144 |
conf.cw = cwriter.New(w)
|
|
145 |
p.userConf <- conf
|
| 125 | 146 |
return p
|
| 126 | 147 |
}
|
| 127 | 148 |
|
|
| 131 | 152 |
if isClosed(p.done) {
|
| 132 | 153 |
panic(ErrCallAfterStop)
|
| 133 | 154 |
}
|
| 134 | |
p.rrChangeReqCh <- d
|
|
155 |
conf := <-p.userConf
|
|
156 |
conf.ticker.Stop()
|
|
157 |
rr = d
|
|
158 |
conf.ticker = time.NewTicker(rr)
|
|
159 |
p.userConf <- conf
|
| 135 | 160 |
return p
|
| 136 | 161 |
}
|
| 137 | 162 |
|
| 138 | 163 |
// BeforeRenderFunc accepts a func, which gets called before render process.
|
|
164 |
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
|
| 139 | 165 |
func (p *Progress) BeforeRenderFunc(f BeforeRender) *Progress {
|
| 140 | 166 |
if isClosed(p.done) {
|
| 141 | 167 |
panic(ErrCallAfterStop)
|
| 142 | 168 |
}
|
| 143 | |
p.brCh <- f
|
| 144 | |
return p
|
| 145 | |
}
|
| 146 | |
|
| 147 | |
// AddBar creates a new progress bar and adds to the container
|
|
169 |
conf := <-p.userConf
|
|
170 |
conf.beforeRender = f
|
|
171 |
p.userConf <- conf
|
|
172 |
return p
|
|
173 |
}
|
|
174 |
|
|
175 |
// AddBar creates a new progress bar and adds to the container.
|
| 148 | 176 |
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
|
| 149 | 177 |
func (p *Progress) AddBar(total int64) *Bar {
|
| 150 | 178 |
return p.AddBarWithID(0, total)
|
| 151 | 179 |
}
|
| 152 | 180 |
|
| 153 | |
// AddBarWithID creates a new progress bar and adds to the container
|
|
181 |
// AddBarWithID creates a new progress bar and adds to the container.
|
| 154 | 182 |
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
|
| 155 | 183 |
func (p *Progress) AddBarWithID(id int, total int64) *Bar {
|
| 156 | 184 |
if isClosed(p.done) {
|
| 157 | 185 |
panic(ErrCallAfterStop)
|
| 158 | 186 |
}
|
|
187 |
conf := <-p.userConf
|
| 159 | 188 |
result := make(chan bool)
|
| 160 | |
bar := newBar(id, total, p.width, p.format, p.wg, p.cancel)
|
|
189 |
bar := newBar(id, total, p.wg, &conf)
|
| 161 | 190 |
p.bCommandCh <- &bCommandData{bAdd, bar, result}
|
| 162 | 191 |
if <-result {
|
| 163 | 192 |
p.wg.Add(1)
|
|
| 187 | 216 |
return <-respCh
|
| 188 | 217 |
}
|
| 189 | 218 |
|
| 190 | |
// Format sets custom format for underlying bar(s).
|
| 191 | |
// The default one is "[=>-]"
|
|
219 |
// ShutdownNotify means to be notified when main rendering goroutine quits, usualy after p.Stop() call.
|
|
220 |
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
|
|
221 |
func (p *Progress) ShutdownNotify(ch chan struct{}) *Progress {
|
|
222 |
if isClosed(p.done) {
|
|
223 |
panic(ErrCallAfterStop)
|
|
224 |
}
|
|
225 |
conf := <-p.userConf
|
|
226 |
conf.shutdownNotifier = ch
|
|
227 |
p.userConf <- conf
|
|
228 |
return p
|
|
229 |
}
|
|
230 |
|
|
231 |
// Format sets custom format for underlying bar(s), default one is "[=>-]".
|
|
232 |
// pancis, if called on stopped Progress instance, i.e after (*Progress).Stop()
|
| 192 | 233 |
func (p *Progress) Format(format string) *Progress {
|
|
234 |
if isClosed(p.done) {
|
|
235 |
panic(ErrCallAfterStop)
|
|
236 |
}
|
| 193 | 237 |
if utf8.RuneCountInString(format) != numFmtRunes {
|
| 194 | 238 |
return p
|
| 195 | 239 |
}
|
| 196 | |
p.format = format
|
|
240 |
conf := <-p.userConf
|
|
241 |
conf.format = format
|
|
242 |
p.userConf <- conf
|
| 197 | 243 |
return p
|
| 198 | 244 |
}
|
| 199 | 245 |
|
|
| 211 | 257 |
}
|
| 212 | 258 |
|
| 213 | 259 |
// server monitors underlying channels and renders any progress bars
|
| 214 | |
func (p *Progress) server() {
|
| 215 | |
userRR := rr * time.Millisecond
|
| 216 | |
t := time.NewTicker(userRR)
|
|
260 |
func (p *Progress) server(conf userConf) {
|
| 217 | 261 |
|
| 218 | 262 |
defer func() {
|
| 219 | |
t.Stop()
|
|
263 |
conf.ticker.Stop()
|
| 220 | 264 |
close(p.done)
|
|
265 |
if conf.shutdownNotifier != nil {
|
|
266 |
close(conf.shutdownNotifier)
|
|
267 |
}
|
| 221 | 268 |
}()
|
| 222 | 269 |
|
| 223 | 270 |
recoverFn := func(ch chan []byte) {
|
|
| 230 | 277 |
close(ch)
|
| 231 | 278 |
}
|
| 232 | 279 |
|
| 233 | |
var beforeRender BeforeRender
|
| 234 | |
cw := cwriter.New(os.Stdout)
|
| 235 | 280 |
bars := make([]*Bar, 0, 3)
|
| 236 | 281 |
|
| 237 | 282 |
for {
|
| 238 | 283 |
select {
|
| 239 | |
case w := <-p.outChangeReqCh:
|
| 240 | |
cw.Flush()
|
| 241 | |
cw = cwriter.New(w)
|
|
284 |
case p.userConf <- conf:
|
|
285 |
case conf = <-p.userConf:
|
| 242 | 286 |
case data, ok := <-p.bCommandCh:
|
| 243 | 287 |
if !ok {
|
| 244 | 288 |
return
|
|
| 261 | 305 |
}
|
| 262 | 306 |
case respCh := <-p.barCountReqCh:
|
| 263 | 307 |
respCh <- len(bars)
|
| 264 | |
case beforeRender = <-p.brCh:
|
| 265 | |
case <-t.C:
|
|
308 |
case <-conf.ticker.C:
|
| 266 | 309 |
numBars := len(bars)
|
| 267 | 310 |
|
| 268 | 311 |
if numBars == 0 {
|
| 269 | 312 |
break
|
| 270 | 313 |
}
|
| 271 | 314 |
|
| 272 | |
if beforeRender != nil {
|
| 273 | |
beforeRender(bars)
|
|
315 |
if conf.beforeRender != nil {
|
|
316 |
conf.beforeRender(bars)
|
| 274 | 317 |
}
|
| 275 | 318 |
|
| 276 | 319 |
quitWidthSyncCh := make(chan struct{})
|
| 277 | |
time.AfterFunc(userRR, func() {
|
|
320 |
time.AfterFunc(rr, func() {
|
| 278 | 321 |
close(quitWidthSyncCh)
|
| 279 | 322 |
})
|
| 280 | 323 |
|
|
| 292 | 335 |
ch := fanIn(sequence...)
|
| 293 | 336 |
|
| 294 | 337 |
for buf := range ch {
|
| 295 | |
cw.Write(buf)
|
| 296 | |
}
|
| 297 | |
|
| 298 | |
cw.Flush()
|
|
338 |
conf.cw.Write(buf)
|
|
339 |
}
|
|
340 |
|
|
341 |
conf.cw.Flush()
|
| 299 | 342 |
|
| 300 | 343 |
for _, b := range bars {
|
| 301 | 344 |
b.flushed()
|
| 302 | 345 |
}
|
| 303 | |
case userRR = <-p.rrChangeReqCh:
|
| 304 | |
t.Stop()
|
| 305 | |
t = time.NewTicker(userRR)
|
| 306 | 346 |
case <-p.beforeStop:
|
| 307 | 347 |
for _, b := range bars {
|
| 308 | 348 |
if b.GetStatistics().Total <= 0 {
|
| 309 | 349 |
b.Completed()
|
| 310 | 350 |
}
|
| 311 | 351 |
}
|
| 312 | |
case <-p.cancel:
|
|
352 |
case <-conf.cancel:
|
| 313 | 353 |
return
|
| 314 | 354 |
}
|
| 315 | 355 |
}
|