| 94 | 94 |
done: make(chan struct{}),
|
| 95 | 95 |
wg: new(sync.WaitGroup),
|
| 96 | 96 |
}
|
| 97 | |
go p.server(cwriter.New(os.Stdout), time.NewTicker(rr*time.Millisecond))
|
|
97 |
go p.server(cwriter.New(os.Stdout))
|
| 98 | 98 |
return p
|
| 99 | 99 |
}
|
| 100 | 100 |
|
|
| 218 | 218 |
}
|
| 219 | 219 |
|
| 220 | 220 |
// server monitors underlying channels and renders any progress bars
|
| 221 | |
func (p *Progress) server(cw *cwriter.Writer, t *time.Ticker) {
|
|
221 |
func (p *Progress) server(cw *cwriter.Writer) {
|
|
222 |
userRR := rr * time.Millisecond
|
|
223 |
t := time.NewTicker(userRR)
|
|
224 |
|
| 222 | 225 |
defer func() {
|
| 223 | 226 |
t.Stop()
|
| 224 | 227 |
close(p.done)
|
| 225 | 228 |
}()
|
| 226 | |
bars := make([]*Bar, 0, 3)
|
| 227 | |
var beforeRender BeforeRender
|
|
229 |
|
| 228 | 230 |
var wg sync.WaitGroup
|
| 229 | 231 |
recoverIfPanic := func() {
|
| 230 | 232 |
if p := recover(); p != nil {
|
|
| 235 | 237 |
}
|
| 236 | 238 |
wg.Done()
|
| 237 | 239 |
}
|
|
240 |
var beforeRender BeforeRender
|
|
241 |
bars := make([]*Bar, 0, 3)
|
|
242 |
|
| 238 | 243 |
for {
|
| 239 | 244 |
select {
|
| 240 | 245 |
case w := <-p.outChangeReqCh:
|
|
| 274 | 279 |
beforeRender(bars)
|
| 275 | 280 |
}
|
| 276 | 281 |
|
| 277 | |
prependWs := newWidthSync(numBars, bars[0].NumOfPrependers())
|
| 278 | |
appendWs := newWidthSync(numBars, bars[0].NumOfAppenders())
|
|
282 |
prependWs := newWidthSync(userRR, numBars, bars[0].NumOfPrependers())
|
|
283 |
appendWs := newWidthSync(userRR, numBars, bars[0].NumOfAppenders())
|
| 279 | 284 |
|
| 280 | 285 |
width, _, _ := cwriter.GetTermSize()
|
| 281 | 286 |
ibars := iBarsGen(bars, width)
|
|
| 311 | 316 |
for _, b := range bars {
|
| 312 | 317 |
b.flushed()
|
| 313 | 318 |
}
|
| 314 | |
case d := <-p.rrChangeReqCh:
|
|
319 |
case userRR = <-p.rrChangeReqCh:
|
| 315 | 320 |
t.Stop()
|
| 316 | |
t = time.NewTicker(d)
|
|
321 |
t = time.NewTicker(userRR)
|
| 317 | 322 |
case <-p.cancel:
|
| 318 | 323 |
return
|
| 319 | 324 |
}
|
| 320 | 325 |
}
|
| 321 | 326 |
}
|
| 322 | 327 |
|
| 323 | |
func newWidthSync(numBars, numColumn int) *widthSync {
|
|
328 |
func newWidthSync(userRR time.Duration, numBars, numColumn int) *widthSync {
|
| 324 | 329 |
ws := &widthSync{
|
| 325 | 330 |
listen: make([]chan int, numColumn),
|
| 326 | 331 |
result: make([]chan int, numColumn),
|
|
| 341 | 346 |
if len(widths) == numBars {
|
| 342 | 347 |
break loop
|
| 343 | 348 |
}
|
| 344 | |
case <-time.After(rr * time.Millisecond):
|
|
349 |
case <-time.After(userRR):
|
| 345 | 350 |
return
|
| 346 | 351 |
}
|
| 347 | 352 |
}
|