Codebase list golang-github-vbauerster-mpb / 64a610c
Internal id counter Vladimir Bauer 8 years ago
2 changed file(s) with 14 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
2525 )
2626
2727 type fmtRunes [formatLen]rune
28 type fmtByteSegments [formatLen][]byte
2928
3029 // Bar represents a progress Bar
3130 type Bar struct {
6766 }
6867 )
6968
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 {
7170 s := state{
71 id: ID,
7272 total: total,
7373 etaAlpha: etaAlpha,
7474 }
265265 select {
266266 case op := <-b.ops:
267267 op(&s)
268 case <-b.quit:
269 s.completed = true
270 return
271268 case <-cancel:
272269 s.aborted = true
273270 cancel = nil
274271 b.Complete()
272 case <-b.quit:
273 s.completed = true
274 return
275275 }
276276 }
277277 }
280280 ch := make(chan []byte)
281281
282282 go func() {
283 var st state
283284 defer func() {
284285 // recovering if external decorators panic
285286 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)
287288 }
288289 }()
289 var st state
290290 result := make(chan state, 1)
291291 select {
292292 case b.ops <- func(s *state) {
423423 }
424424 }
425425
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
436426 func getSpinner() func() byte {
437427 chars := []byte(`-\|/`)
438428 repeat := len(chars) - 1
2222 pConf struct {
2323 bars []*Bar
2424
25 idCounter int
2526 width int
2627 format string
2728 rr time.Duration
8990
9091 // AddBar creates a new progress bar and adds to the container.
9192 func (p *Progress) AddBar(total int64, options ...BarOption) *Bar {
93 p.wg.Add(1)
9294 result := make(chan *Bar, 1)
93 op := func(c *pConf) {
95 select {
96 case p.ops <- func(c *pConf) {
9497 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...)
9699 c.bars = append(c.bars, b)
97 p.wg.Add(1)
100 c.idCounter++
98101 result <- b
99 }
100 select {
101 case p.ops <- op:
102 }:
102103 return <-result
103104 case <-p.quit:
104105 return new(Bar)