| 33 | 33 |
leftEnd byte
|
| 34 | 34 |
rightEnd byte
|
| 35 | 35 |
|
| 36 | |
incrCh chan int
|
| 37 | |
redrawRequestCh chan *redrawRequest
|
| 38 | |
decoratorCh chan *decorator
|
| 39 | |
flushedCh chan struct{}
|
| 40 | |
stopCh chan struct{}
|
| 41 | |
done chan struct{}
|
|
36 |
incrCh chan int
|
|
37 |
redrawReqCh chan chan []byte
|
|
38 |
progressReqCh chan chan int
|
|
39 |
decoratorCh chan *decorator
|
|
40 |
flushedCh chan struct{}
|
|
41 |
stopCh chan struct{}
|
|
42 |
done chan struct{}
|
| 42 | 43 |
|
| 43 | 44 |
timePerItemEstimate time.Duration
|
| 44 | 45 |
}
|
|
| 48 | 49 |
TimePerItemEstimate time.Duration
|
| 49 | 50 |
}
|
| 50 | 51 |
|
| 51 | |
type redrawRequest struct {
|
| 52 | |
bufCh chan []byte
|
| 53 | |
}
|
|
52 |
// type redrawRequest struct {
|
|
53 |
// respCh chan []byte
|
|
54 |
// }
|
|
55 |
|
|
56 |
// type progressRequest struct {
|
|
57 |
// respCh chan int
|
|
58 |
// }
|
| 54 | 59 |
|
| 55 | 60 |
func newBar(total, width int, wg *sync.WaitGroup) *Bar {
|
| 56 | 61 |
b := &Bar{
|
| 57 | |
fill: '=',
|
| 58 | |
empty: '-',
|
| 59 | |
tip: '>',
|
| 60 | |
leftEnd: '[',
|
| 61 | |
rightEnd: ']',
|
| 62 | |
alpha: 0.25,
|
| 63 | |
total: total,
|
| 64 | |
width: width,
|
| 65 | |
incrCh: make(chan int),
|
| 66 | |
redrawRequestCh: make(chan *redrawRequest),
|
| 67 | |
decoratorCh: make(chan *decorator),
|
| 68 | |
flushedCh: make(chan struct{}),
|
| 69 | |
stopCh: make(chan struct{}),
|
| 70 | |
done: make(chan struct{}),
|
|
62 |
fill: '=',
|
|
63 |
empty: '-',
|
|
64 |
tip: '>',
|
|
65 |
leftEnd: '[',
|
|
66 |
rightEnd: ']',
|
|
67 |
alpha: 0.25,
|
|
68 |
total: total,
|
|
69 |
width: width,
|
|
70 |
incrCh: make(chan int),
|
|
71 |
redrawReqCh: make(chan chan []byte),
|
|
72 |
progressReqCh: make(chan chan int),
|
|
73 |
decoratorCh: make(chan *decorator),
|
|
74 |
flushedCh: make(chan struct{}),
|
|
75 |
stopCh: make(chan struct{}),
|
|
76 |
done: make(chan struct{}),
|
| 71 | 77 |
}
|
| 72 | 78 |
go b.server(wg)
|
| 73 | 79 |
return b
|
|
| 127 | 133 |
|
| 128 | 134 |
// String returns the string representation of the bar
|
| 129 | 135 |
func (b *Bar) String() string {
|
| 130 | |
bufCh := make(chan []byte)
|
| 131 | |
b.redrawRequestCh <- &redrawRequest{bufCh}
|
| 132 | |
return string(<-bufCh)
|
|
136 |
respCh := make(chan []byte)
|
|
137 |
b.redrawReqCh <- respCh
|
|
138 |
return string(<-respCh)
|
| 133 | 139 |
}
|
| 134 | 140 |
|
| 135 | 141 |
func (b *Bar) Incr(n int) {
|
|
| 227 | 233 |
case decoratorPrepend:
|
| 228 | 234 |
prependFuncs = append(prependFuncs, d.f)
|
| 229 | 235 |
}
|
| 230 | |
case r := <-b.redrawRequestCh:
|
| 231 | |
r.bufCh <- b.draw(buf, completed, appendFuncs, prependFuncs)
|
|
236 |
case respCh := <-b.redrawReqCh:
|
|
237 |
respCh <- b.draw(buf, completed, appendFuncs, prependFuncs)
|
|
238 |
case respCh := <-b.progressReqCh:
|
|
239 |
respCh <- int(100 * float64(completed) / float64(b.total))
|
| 232 | 240 |
case <-b.flushedCh:
|
| 233 | 241 |
if done && !b.IsCompleted() {
|
| 234 | 242 |
// fmt.Fprintln(os.Stderr, "flushedCh: wg.Done")
|
|
| 286 | 294 |
lastItemEstimate := float64(lastBlockTime) / float64(items)
|
| 287 | 295 |
b.timePerItemEstimate = time.Duration((b.alpha * lastItemEstimate) + (1-b.alpha)*float64(b.timePerItemEstimate))
|
| 288 | 296 |
}
|
|
297 |
|
|
298 |
func (b *Bar) progress() int {
|
|
299 |
respCh := make(chan int)
|
|
300 |
b.progressReqCh <- respCh
|
|
301 |
return <-respCh
|
|
302 |
}
|
|
303 |
|
|
304 |
type SortableBarSlice []*Bar
|
|
305 |
|
|
306 |
func (p SortableBarSlice) Len() int { return len(p) }
|
|
307 |
|
|
308 |
func (p SortableBarSlice) Less(i, j int) bool { return p[i].progress() < p[j].progress() }
|
|
309 |
|
|
310 |
func (p SortableBarSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|