Codebase list golang-github-vbauerster-mpb / bba87a4
AddBarWithID Vladimir Bauer 9 years ago
2 changed file(s) with 20 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
4646 till int64
4747 }
4848 state struct {
49 id int
4950 fill byte
5051 empty byte
5152 tip byte
6667 }
6768 )
6869
69 func newBar(ctx context.Context, wg *sync.WaitGroup, total int64, barWidth int) *Bar {
70 func newBar(ctx context.Context, wg *sync.WaitGroup, total int64, width, id int) *Bar {
7071 b := &Bar{
7172 fillCh: make(chan byte),
7273 emptyCh: make(chan byte),
8687 completeReqCh: make(chan struct{}),
8788 done: make(chan struct{}),
8889 }
89 go b.server(ctx, wg, total, barWidth)
90 go b.server(ctx, wg, total, width, id)
9091 return b
9192 }
9293
206207 return s.appendFuncs
207208 }
208209
209 // GetAppenders returns slice of prepender DecoratorFunc
210 // GetPrependers returns slice of prepender DecoratorFunc
210211 func (b *Bar) GetPrependers() []DecoratorFunc {
211212 s := b.getState()
212213 return s.prependFuncs
219220 return state.newStat()
220221 }
221222
223 // GetID returs id of the bar
224 func (b *Bar) GetID() int {
225 state := b.getState()
226 return state.id
227 }
228
222229 // InProgress returns true, while progress is running
223230 // Can be used as condition in for loop
224231 func (b *Bar) InProgress() bool {
283290 return state.draw(termWidth)
284291 }
285292
286 func (b *Bar) server(ctx context.Context, wg *sync.WaitGroup, total int64, barWidth int) {
293 func (b *Bar) server(ctx context.Context, wg *sync.WaitGroup, total int64, width, id int) {
287294 var completed bool
288295 timeStarted := time.Now()
289296 blockStartTime := timeStarted
290297 state := state{
298 id: id,
291299 fill: '=',
292300 empty: '-',
293301 tip: '>',
294302 leftEnd: '[',
295303 rightEnd: ']',
296304 etaAlpha: 0.25,
297 barWidth: barWidth,
305 barWidth: width,
298306 total: total,
299307 }
300308 if total <= 0 {
131131 // AddBar creates a new progress bar and adds to the container
132132 // pancis, if called on stopped Progress instance, i.e after Stop()
133133 func (p *Progress) AddBar(total int64) *Bar {
134 return p.AddBarWithID(total, 0)
135 }
136
137 // AddBarWithID creates a new progress bar and adds to the container
138 // pancis, if called on stopped Progress instance, i.e after Stop()
139 func (p *Progress) AddBarWithID(total int64, id int) *Bar {
134140 if IsClosed(p.done) {
135141 panic(ErrCallAfterStop)
136142 }
137143 result := make(chan bool)
138 bar := newBar(p.ctx, p.wg, total, p.width)
144 bar := newBar(p.ctx, p.wg, total, p.width, id)
139145 p.operationCh <- &operation{barAdd, bar, result}
140146 if <-result {
141147 p.wg.Add(1)