Codebase list golang-github-vbauerster-mpb / c91fdb4
IncrWithReFill Vladimir Bauer 9 years ago
1 changed file(s) with 36 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
2828 removeReqCh chan struct{}
2929 done chan struct{}
3030
31 refill *reFill
3132 lastState state
3233 }
3334
4344 return time.Duration(s.Total-s.Current) * s.TimePerItemEstimate
4445 }
4546
46 type state struct {
47 total, current int64
48 timeElapsed, timePerItem time.Duration
49 appendFuncs, prependFuncs []DecoratorFunc
50 trimLeftSpace, trimRightSpace bool
51 }
47 type (
48 state struct {
49 total, current int64
50 timeElapsed, timePerItem time.Duration
51 appendFuncs, prependFuncs []DecoratorFunc
52 trimLeftSpace, trimRightSpace bool
53 }
54 reFill struct {
55 c byte
56 till int64
57 }
58 )
5259
5360 func newBar(ctx context.Context, wg *sync.WaitGroup, total int64, width int) *Bar {
5461 b := &Bar{
147154
148155 // Incr increments progress bar
149156 func (b *Bar) Incr(n int) {
150 if !b.isDone() {
157 if n > 0 && !b.isDone() {
151158 b.incrCh <- int64(n)
159 }
160 }
161
162 // IncrWithReFill increments pb with different fill character
163 func (b *Bar) IncrWithReFill(n int, c byte) {
164 if n > 0 {
165 b.Incr(n)
166 b.refill = &reFill{c, int64(n)}
152167 }
153168 }
154169
209224 for {
210225 select {
211226 case i := <-b.incrCh:
212 if i <= 0 {
213 break
214 }
215227 n := state.current + i
216228 if n > total {
217229 state.current = total
328340 buf := make([]byte, width)
329341 completedWidth := percentage(total, current, width)
330342
331 for i := 1; i < completedWidth; i++ {
332 buf[i] = b.fill
333 }
343 if b.refill != nil {
344 till := percentage(total, b.refill.till, width)
345 for i := 1; i < till; i++ {
346 buf[i] = b.refill.c
347 }
348 for i := till; i < completedWidth; i++ {
349 buf[i] = b.fill
350 }
351 } else {
352 for i := 1; i < completedWidth; i++ {
353 buf[i] = b.fill
354 }
355 }
356
334357 for i := completedWidth; i < width-1; i++ {
335358 buf[i] = b.empty
336359 }