Codebase list golang-github-vbauerster-mpb / 08a6972
non-blocking status() method Vladimir Bauer 9 years ago
1 changed file(s) with 33 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
77
88 // Bar represents a progress Bar
99 type Bar struct {
10 total int
10 // total int
1111 width int
1212 alpha float64
1313
1717 leftEnd byte
1818 rightEnd byte
1919
20 lastFrame []byte
20 lastFrame []byte
21 lastStatus int
2122
2223 incrCh chan int
2324 redrawReqCh chan chan []byte
4041
4142 func newBar(total, width int, wg *sync.WaitGroup) *Bar {
4243 b := &Bar{
43 fill: '=',
44 empty: '-',
45 tip: '>',
46 leftEnd: '[',
47 rightEnd: ']',
48 alpha: 0.25,
49 total: total,
44 fill: '=',
45 empty: '-',
46 tip: '>',
47 leftEnd: '[',
48 rightEnd: ']',
49 alpha: 0.25,
50 // total: total,
5051 width: width,
5152 incrCh: make(chan int),
5253 redrawReqCh: make(chan chan []byte),
5758 stopCh: make(chan struct{}),
5859 done: make(chan struct{}),
5960 }
60 go b.server(wg)
61 go b.server(wg, total)
6162 return b
6263 }
6364
166167 return string(b.lastFrame)
167168 }
168169
169 func (b *Bar) server(wg *sync.WaitGroup) {
170 func (b *Bar) server(wg *sync.WaitGroup, total int) {
170171 timeStarted := time.Now()
171172 blockStartTime := timeStarted
172173 buf := make([]byte, b.width, b.width+24)
180181 select {
181182 case i := <-b.incrCh:
182183 n := current + i
183 if n > b.total {
184 current = b.total
184 if n > total {
185 current = total
185186 completed = true
186187 break
187188 }
189190 tpie = calcTimePerItemEstimate(tpie, blockStartTime, b.alpha, i)
190191 blockStartTime = time.Now()
191192 current = n
192 if current == b.total && !completed {
193 if current == total && !completed {
193194 completed = true
194195 }
195196 case d := <-b.decoratorCh:
202203 case respCh := <-b.currentReqCh:
203204 respCh <- current
204205 case respCh := <-b.redrawReqCh:
205 stat := &Statistics{b.total, current, timeElapsed, tpie}
206 stat := &Statistics{total, current, timeElapsed, tpie}
206207 respCh <- b.draw(stat, buf, appendFuncs, prependFuncs)
207208 case respCh := <-b.statusReqCh:
208 respCh <- int(100 * float64(current) / float64(b.total))
209 respCh <- percentage(total, current, 100)
209210 case <-b.flushedCh:
210211 if completed && !b.isDone() {
211 stat := &Statistics{b.total, current, timeElapsed, tpie}
212 stat := &Statistics{total, current, timeElapsed, tpie}
212213 b.lastFrame = b.draw(stat, buf, appendFuncs, prependFuncs)
214 b.lastStatus = percentage(total, current, 100)
213215 close(b.done)
214216 wg.Done()
215217 return
225227 }
226228
227229 func (b *Bar) draw(stat *Statistics, buf []byte, appendFuncs, prependFuncs []DecoratorFunc) []byte {
228 completedWidth := stat.Current * b.width / b.total
230 completedWidth := percentage(stat.Total, stat.Current, b.width)
229231
230232 for i := 0; i < completedWidth; i++ {
231233 buf[i] = b.fill
266268 }
267269
268270 func (b *Bar) status() int {
269 respCh := make(chan int)
270 b.statusReqCh <- respCh
271 return <-respCh
271 if !b.isDone() {
272 respCh := make(chan int)
273 b.statusReqCh <- respCh
274 return <-respCh
275 }
276 return b.lastStatus
272277 }
273278
274279 // SortableBarSlice satisfies sort interface
285290 lastItemEstimate := float64(lastBlockTime) / float64(items)
286291 return time.Duration((alpha * lastItemEstimate) + (1-alpha)*float64(tpie))
287292 }
293
294 func percentage(total, current, ratio int) int {
295 if total == 0 {
296 return 0
297 }
298 return int(float64(ratio) * float64(current) / float64(total))
299 }