IncrWithReFill
Vladimir Bauer
9 years ago
| 28 | 28 | removeReqCh chan struct{} |
| 29 | 29 | done chan struct{} |
| 30 | 30 | |
| 31 | refill *reFill | |
| 31 | 32 | lastState state |
| 32 | 33 | } |
| 33 | 34 | |
| 43 | 44 | return time.Duration(s.Total-s.Current) * s.TimePerItemEstimate |
| 44 | 45 | } |
| 45 | 46 | |
| 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 | ) | |
| 52 | 59 | |
| 53 | 60 | func newBar(ctx context.Context, wg *sync.WaitGroup, total int64, width int) *Bar { |
| 54 | 61 | b := &Bar{ |
| 147 | 154 | |
| 148 | 155 | // Incr increments progress bar |
| 149 | 156 | func (b *Bar) Incr(n int) { |
| 150 | if !b.isDone() { | |
| 157 | if n > 0 && !b.isDone() { | |
| 151 | 158 | 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)} | |
| 152 | 167 | } |
| 153 | 168 | } |
| 154 | 169 | |
| 209 | 224 | for { |
| 210 | 225 | select { |
| 211 | 226 | case i := <-b.incrCh: |
| 212 | if i <= 0 { | |
| 213 | break | |
| 214 | } | |
| 215 | 227 | n := state.current + i |
| 216 | 228 | if n > total { |
| 217 | 229 | state.current = total |
| 328 | 340 | buf := make([]byte, width) |
| 329 | 341 | completedWidth := percentage(total, current, width) |
| 330 | 342 | |
| 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 | ||
| 334 | 357 | for i := completedWidth; i < width-1; i++ { |
| 335 | 358 | buf[i] = b.empty |
| 336 | 359 | } |