| 0 | 0 |
package mpb
|
| 1 | 1 |
|
| 2 | 2 |
import (
|
| 3 | |
"fmt"
|
| 4 | 3 |
"io"
|
| 5 | 4 |
"math"
|
| 6 | |
"os"
|
| 7 | 5 |
"sync"
|
| 8 | 6 |
"time"
|
| 9 | 7 |
"unicode/utf8"
|
|
| 22 | 20 |
|
| 23 | 21 |
// Bar represents a progress Bar
|
| 24 | 22 |
type Bar struct {
|
| 25 | |
stateReqCh chan state
|
| 26 | |
incrCh chan incrReq
|
| 27 | |
// dCommandCh chan *dCommandData
|
|
23 |
stateCh chan state
|
|
24 |
incrCh chan incrReq
|
| 28 | 25 |
flushedCh chan struct{}
|
|
26 |
completedCh chan struct{}
|
| 29 | 27 |
removeReqCh chan struct{}
|
| 30 | |
completedCh chan struct{}
|
| 31 | 28 |
done chan struct{}
|
| 32 | 29 |
cancel <-chan struct{}
|
| 33 | 30 |
|
|
| 85 | 82 |
|
| 86 | 83 |
func newBar(id int, total int64, wg *sync.WaitGroup, conf *userConf) *Bar {
|
| 87 | 84 |
b := &Bar{
|
| 88 | |
width: conf.width,
|
| 89 | |
stateReqCh: make(chan state),
|
| 90 | |
incrCh: make(chan incrReq),
|
| 91 | |
// dCommandCh: make(chan *dCommandData),
|
|
85 |
width: conf.width,
|
|
86 |
stateCh: make(chan state),
|
|
87 |
incrCh: make(chan incrReq),
|
| 92 | 88 |
flushedCh: make(chan struct{}),
|
| 93 | 89 |
removeReqCh: make(chan struct{}),
|
| 94 | 90 |
completedCh: make(chan struct{}),
|
|
| 266 | 262 |
select {
|
| 267 | 263 |
case b.flushedCh <- struct{}{}:
|
| 268 | 264 |
case <-b.done:
|
| 269 | |
fmt.Fprintf(os.Stderr, "flushedCh abort: %d\n", b.state.id)
|
| 270 | 265 |
return
|
| 271 | 266 |
}
|
| 272 | 267 |
}
|
|
| 281 | 276 |
|
| 282 | 277 |
func (b *Bar) getState() state {
|
| 283 | 278 |
select {
|
| 284 | |
case s := <-b.stateReqCh:
|
|
279 |
case s := <-b.stateCh:
|
| 285 | 280 |
return s
|
| 286 | 281 |
case <-b.done:
|
| 287 | 282 |
return b.state
|
|
| 292 | 287 |
s := b.getState()
|
| 293 | 288 |
cb(&s)
|
| 294 | 289 |
select {
|
| 295 | |
case b.stateReqCh <- s:
|
|
290 |
case b.stateCh <- s:
|
| 296 | 291 |
case <-b.done:
|
| 297 | 292 |
return
|
| 298 | 293 |
}
|
|
| 305 | 300 |
b.state = s
|
| 306 | 301 |
wg.Done()
|
| 307 | 302 |
close(b.done)
|
| 308 | |
fmt.Fprintf(os.Stderr, "Exited bar %d\n", s.id)
|
| 309 | 303 |
}()
|
| 310 | 304 |
|
| 311 | 305 |
for {
|
| 312 | 306 |
select {
|
| 313 | |
case b.stateReqCh <- s:
|
| 314 | |
case s = <-b.stateReqCh:
|
|
307 |
case b.stateCh <- s:
|
|
308 |
case s = <-b.stateCh:
|
| 315 | 309 |
case r := <-b.incrCh:
|
| 316 | 310 |
if s.current == 0 {
|
| 317 | 311 |
incrStartTime = time.Now()
|
|
| 333 | 327 |
incrStartTime = time.Now()
|
| 334 | 328 |
case <-b.flushedCh:
|
| 335 | 329 |
if s.completed {
|
| 336 | |
fmt.Fprintln(os.Stderr, "completed")
|
| 337 | 330 |
return
|
| 338 | 331 |
}
|
| 339 | 332 |
case <-b.completedCh:
|