Internal id counter
Vladimir Bauer
8 years ago
| 25 | 25 |
)
|
| 26 | 26 |
|
| 27 | 27 |
type fmtRunes [formatLen]rune
|
| 28 | |
type fmtByteSegments [formatLen][]byte
|
| 29 | 28 |
|
| 30 | 29 |
// Bar represents a progress Bar
|
| 31 | 30 |
type Bar struct {
|
|
| 67 | 66 |
}
|
| 68 | 67 |
)
|
| 69 | 68 |
|
| 70 | |
func newBar(total int64, wg *sync.WaitGroup, cancel <-chan struct{}, options ...BarOption) *Bar {
|
|
69 |
func newBar(ID int, total int64, wg *sync.WaitGroup, cancel <-chan struct{}, options ...BarOption) *Bar {
|
| 71 | 70 |
s := state{
|
|
71 |
id: ID,
|
| 72 | 72 |
total: total,
|
| 73 | 73 |
etaAlpha: etaAlpha,
|
| 74 | 74 |
}
|
|
| 265 | 265 |
select {
|
| 266 | 266 |
case op := <-b.ops:
|
| 267 | 267 |
op(&s)
|
| 268 | |
case <-b.quit:
|
| 269 | |
s.completed = true
|
| 270 | |
return
|
| 271 | 268 |
case <-cancel:
|
| 272 | 269 |
s.aborted = true
|
| 273 | 270 |
cancel = nil
|
| 274 | 271 |
b.Complete()
|
|
272 |
case <-b.quit:
|
|
273 |
s.completed = true
|
|
274 |
return
|
| 275 | 275 |
}
|
| 276 | 276 |
}
|
| 277 | 277 |
}
|
|
| 280 | 280 |
ch := make(chan []byte)
|
| 281 | 281 |
|
| 282 | 282 |
go func() {
|
|
283 |
var st state
|
| 283 | 284 |
defer func() {
|
| 284 | 285 |
// recovering if external decorators panic
|
| 285 | 286 |
if p := recover(); p != nil {
|
| 286 | |
fmt.Fprintf(os.Stderr, "bar panic: %q\n", p)
|
|
287 |
fmt.Fprintf(os.Stderr, "bar %d: %q\n", st.id, p)
|
| 287 | 288 |
}
|
| 288 | 289 |
}()
|
| 289 | |
var st state
|
| 290 | 290 |
result := make(chan state, 1)
|
| 291 | 291 |
select {
|
| 292 | 292 |
case b.ops <- func(s *state) {
|
|
| 423 | 423 |
}
|
| 424 | 424 |
}
|
| 425 | 425 |
|
| 426 | |
func fmtRunesToByteSegments(format fmtRunes) fmtByteSegments {
|
| 427 | |
var segments fmtByteSegments
|
| 428 | |
for i, r := range format {
|
| 429 | |
buf := make([]byte, utf8.RuneLen(r))
|
| 430 | |
utf8.EncodeRune(buf, r)
|
| 431 | |
segments[i] = buf
|
| 432 | |
}
|
| 433 | |
return segments
|
| 434 | |
}
|
| 435 | |
|
| 436 | 426 |
func getSpinner() func() byte {
|
| 437 | 427 |
chars := []byte(`-\|/`)
|
| 438 | 428 |
repeat := len(chars) - 1
|
| 22 | 22 |
pConf struct {
|
| 23 | 23 |
bars []*Bar
|
| 24 | 24 |
|
|
25 |
idCounter int
|
| 25 | 26 |
width int
|
| 26 | 27 |
format string
|
| 27 | 28 |
rr time.Duration
|
|
| 89 | 90 |
|
| 90 | 91 |
// AddBar creates a new progress bar and adds to the container.
|
| 91 | 92 |
func (p *Progress) AddBar(total int64, options ...BarOption) *Bar {
|
|
93 |
p.wg.Add(1)
|
| 92 | 94 |
result := make(chan *Bar, 1)
|
| 93 | |
op := func(c *pConf) {
|
|
95 |
select {
|
|
96 |
case p.ops <- func(c *pConf) {
|
| 94 | 97 |
options = append(options, barWidth(c.width), barFormat(c.format))
|
| 95 | |
b := newBar(total, p.wg, c.cancel, options...)
|
|
98 |
b := newBar(c.idCounter, total, p.wg, c.cancel, options...)
|
| 96 | 99 |
c.bars = append(c.bars, b)
|
| 97 | |
p.wg.Add(1)
|
|
100 |
c.idCounter++
|
| 98 | 101 |
result <- b
|
| 99 | |
}
|
| 100 | |
select {
|
| 101 | |
case p.ops <- op:
|
|
102 |
}:
|
| 102 | 103 |
return <-result
|
| 103 | 104 |
case <-p.quit:
|
| 104 | 105 |
return new(Bar)
|