diff --git a/bar.go b/bar.go index be10ba1..eaab082 100644 --- a/bar.go +++ b/bar.go @@ -47,6 +47,7 @@ till int64 } state struct { + id int fill byte empty byte tip byte @@ -67,7 +68,7 @@ } ) -func newBar(ctx context.Context, wg *sync.WaitGroup, total int64, barWidth int) *Bar { +func newBar(ctx context.Context, wg *sync.WaitGroup, total int64, width, id int) *Bar { b := &Bar{ fillCh: make(chan byte), emptyCh: make(chan byte), @@ -87,7 +88,7 @@ completeReqCh: make(chan struct{}), done: make(chan struct{}), } - go b.server(ctx, wg, total, barWidth) + go b.server(ctx, wg, total, width, id) return b } @@ -207,7 +208,7 @@ return s.appendFuncs } -// GetAppenders returns slice of prepender DecoratorFunc +// GetPrependers returns slice of prepender DecoratorFunc func (b *Bar) GetPrependers() []DecoratorFunc { s := b.getState() return s.prependFuncs @@ -220,6 +221,12 @@ return state.newStat() } +// GetID returs id of the bar +func (b *Bar) GetID() int { + state := b.getState() + return state.id +} + // InProgress returns true, while progress is running // Can be used as condition in for loop func (b *Bar) InProgress() bool { @@ -284,18 +291,19 @@ return state.draw(termWidth) } -func (b *Bar) server(ctx context.Context, wg *sync.WaitGroup, total int64, barWidth int) { +func (b *Bar) server(ctx context.Context, wg *sync.WaitGroup, total int64, width, id int) { var completed bool timeStarted := time.Now() blockStartTime := timeStarted state := state{ + id: id, fill: '=', empty: '-', tip: '>', leftEnd: '[', rightEnd: ']', etaAlpha: 0.25, - barWidth: barWidth, + barWidth: width, total: total, } if total <= 0 { diff --git a/progress.go b/progress.go index 5d4ccb3..1ad58a9 100644 --- a/progress.go +++ b/progress.go @@ -132,11 +132,17 @@ // AddBar creates a new progress bar and adds to the container // pancis, if called on stopped Progress instance, i.e after Stop() func (p *Progress) AddBar(total int64) *Bar { + return p.AddBarWithID(total, 0) +} + +// AddBarWithID creates a new progress bar and adds to the container +// pancis, if called on stopped Progress instance, i.e after Stop() +func (p *Progress) AddBarWithID(total int64, id int) *Bar { if IsClosed(p.done) { panic(ErrCallAfterStop) } result := make(chan bool) - bar := newBar(p.ctx, p.wg, total, p.width) + bar := newBar(p.ctx, p.wg, total, p.width, id) p.operationCh <- &operation{barAdd, bar, result} if <-result { p.wg.Add(1)