diff --git a/bar.go b/bar.go index a238149..3499aab 100644 --- a/bar.go +++ b/bar.go @@ -26,7 +26,6 @@ ) type fmtRunes [formatLen]rune -type fmtByteSegments [formatLen][]byte // Bar represents a progress Bar type Bar struct { @@ -68,8 +67,9 @@ } ) -func newBar(total int64, wg *sync.WaitGroup, cancel <-chan struct{}, options ...BarOption) *Bar { +func newBar(ID int, total int64, wg *sync.WaitGroup, cancel <-chan struct{}, options ...BarOption) *Bar { s := state{ + id: ID, total: total, etaAlpha: etaAlpha, } @@ -266,13 +266,13 @@ select { case op := <-b.ops: op(&s) - case <-b.quit: - s.completed = true - return case <-cancel: s.aborted = true cancel = nil b.Complete() + case <-b.quit: + s.completed = true + return } } } @@ -281,13 +281,13 @@ ch := make(chan []byte) go func() { + var st state defer func() { // recovering if external decorators panic if p := recover(); p != nil { - fmt.Fprintf(os.Stderr, "bar panic: %q\n", p) + fmt.Fprintf(os.Stderr, "bar %d: %q\n", st.id, p) } }() - var st state result := make(chan state, 1) select { case b.ops <- func(s *state) { @@ -424,16 +424,6 @@ } } -func fmtRunesToByteSegments(format fmtRunes) fmtByteSegments { - var segments fmtByteSegments - for i, r := range format { - buf := make([]byte, utf8.RuneLen(r)) - utf8.EncodeRune(buf, r) - segments[i] = buf - } - return segments -} - func getSpinner() func() byte { chars := []byte(`-\|/`) repeat := len(chars) - 1 diff --git a/progress.go b/progress.go index aa0cf1b..5d5a5e1 100644 --- a/progress.go +++ b/progress.go @@ -23,6 +23,7 @@ pConf struct { bars []*Bar + idCounter int width int format string rr time.Duration @@ -90,16 +91,16 @@ // AddBar creates a new progress bar and adds to the container. func (p *Progress) AddBar(total int64, options ...BarOption) *Bar { + p.wg.Add(1) result := make(chan *Bar, 1) - op := func(c *pConf) { + select { + case p.ops <- func(c *pConf) { options = append(options, barWidth(c.width), barFormat(c.format)) - b := newBar(total, p.wg, c.cancel, options...) + b := newBar(c.idCounter, total, p.wg, c.cancel, options...) c.bars = append(c.bars, b) - p.wg.Add(1) + c.idCounter++ result <- b - } - select { - case p.ops <- op: + }: return <-result case <-p.quit: return new(Bar)