Codebase list golang-github-vbauerster-mpb / 3e4360c
bar.go decorators.go refactoring Vladimir Bauer 9 years ago
2 changed file(s) with 10 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
00 package mpb
11
22 import (
3 "fmt"
43 "io"
54 "math"
6 "os"
75 "sync"
86 "time"
97 "unicode/utf8"
2220
2321 // Bar represents a progress Bar
2422 type Bar struct {
25 stateReqCh chan state
26 incrCh chan incrReq
27 // dCommandCh chan *dCommandData
23 stateCh chan state
24 incrCh chan incrReq
2825 flushedCh chan struct{}
26 completedCh chan struct{}
2927 removeReqCh chan struct{}
30 completedCh chan struct{}
3128 done chan struct{}
3229 cancel <-chan struct{}
3330
8582
8683 func newBar(id int, total int64, wg *sync.WaitGroup, conf *userConf) *Bar {
8784 b := &Bar{
88 width: conf.width,
89 stateReqCh: make(chan state),
90 incrCh: make(chan incrReq),
91 // dCommandCh: make(chan *dCommandData),
85 width: conf.width,
86 stateCh: make(chan state),
87 incrCh: make(chan incrReq),
9288 flushedCh: make(chan struct{}),
9389 removeReqCh: make(chan struct{}),
9490 completedCh: make(chan struct{}),
266262 select {
267263 case b.flushedCh <- struct{}{}:
268264 case <-b.done:
269 fmt.Fprintf(os.Stderr, "flushedCh abort: %d\n", b.state.id)
270265 return
271266 }
272267 }
281276
282277 func (b *Bar) getState() state {
283278 select {
284 case s := <-b.stateReqCh:
279 case s := <-b.stateCh:
285280 return s
286281 case <-b.done:
287282 return b.state
292287 s := b.getState()
293288 cb(&s)
294289 select {
295 case b.stateReqCh <- s:
290 case b.stateCh <- s:
296291 case <-b.done:
297292 return
298293 }
305300 b.state = s
306301 wg.Done()
307302 close(b.done)
308 fmt.Fprintf(os.Stderr, "Exited bar %d\n", s.id)
309303 }()
310304
311305 for {
312306 select {
313 case b.stateReqCh <- s:
314 case s = <-b.stateReqCh:
307 case b.stateCh <- s:
308 case s = <-b.stateCh:
315309 case r := <-b.incrCh:
316310 if s.current == 0 {
317311 incrStartTime = time.Now()
333327 incrStartTime = time.Now()
334328 case <-b.flushedCh:
335329 if s.completed {
336 fmt.Fprintln(os.Stderr, "completed")
337330 return
338331 }
339332 case <-b.completedCh:
2020 DextraSpace
2121 )
2222
23 // type decoratorAction uint
24
25 // const (
26 // dAppend decoratorAction = iota
27 // dPrepend
28 // dAppendZero
29 // dPrependZero
30 // )
31
3223 // DecoratorFunc is a function that can be prepended and appended to the progress bar
3324 type DecoratorFunc func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string
34
35 // type dCommandData struct {
36 // action decoratorAction
37 // f DecoratorFunc
38 // }
3925
4026 // PrependName prepends name argument to the bar.
4127 // The conf argument defines the formatting properties