Codebase list golang-github-vbauerster-mpb / 073e26a
add RefreshInterval method Vladimir Bauer 9 years ago
1 changed file(s) with 43 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
88 "github.com/gosuri/uilive"
99 )
1010
11 // Out is the default writer to render progress bars to
12 // var Out = os.Stdout
13
14 // RefreshInterval in the default time duration to wait for refreshing the output
15 var RefreshInterval = time.Millisecond * 10
16
17 // Progress represents the container that renders progress bars
18 type Progress struct {
19 // Out is the writer to render progress bars to
11 // progress represents the container that renders progress bars
12 type progress struct {
13 // out is the writer to render progress bars to
2014 out io.Writer
2115
2216 // Width is the width of the progress bars
2317 // Width int
2418
25 // Bars is the collection of progress bars
26 // Bars []*Bar
19 lw *uilive.Writer
2720
28 // RefreshInterval in the time duration to wait for refreshing the output
29 // RefreshInterval time.Duration
21 // new Bars can be added over this channel
22 bars chan *Bar
3023
31 lw *uilive.Writer
32 // stopChan chan struct{}
33 // mtx *sync.RWMutex
34 bars chan *Bar
35 ticker *time.Ticker
24 // new refresh interval to be send over this channel
25 interval chan time.Duration
3626 }
3727
3828 // New returns a new progress bar with defaults
39 func New() *Progress {
40 p := &Progress{
41 out: os.Stdout,
42 lw: uilive.New(),
43 bars: make(chan *Bar),
44 ticker: time.NewTicker(RefreshInterval),
29 func New() *progress {
30 p := &progress{
31 out: os.Stdout,
32 lw: uilive.New(),
33 bars: make(chan *Bar),
34 interval: make(chan time.Duration),
4535 }
4636 go p.server()
4737 return p
4838 }
4939
50 // SetOut is the writer to render progress bars to
51 func (p *Progress) SetOut(w io.Writer) *Progress {
40 // RefreshInterval overrides default interval value 30 ms
41 func (p *progress) RefreshInterval(d time.Duration) *progress {
42 p.interval <- d
43 return p
44 }
45
46 // SetOut sets underlying writer of progress
47 // default is os.Stdout
48 func (p *progress) SetOut(w io.Writer) *progress {
5249 p.out = w
5350 return p
5451 }
5552
5653 // AddBar creates a new progress bar and adds to the container
57 func (p *Progress) AddBar(total int) *Bar {
54 func (p *progress) AddBar(total int) *Bar {
5855 bar := NewBar(total)
5956 // bar.Width = p.Width
6057 p.bars <- bar
6158 return bar
6259 }
6360
64 // Listen listens for updates and renders the progress bars
65 func (p *Progress) server() {
61 // Bypass returns a writer which allows non-buffered data to be written to the underlying output
62 func (p *progress) Bypass() io.Writer {
63 return p.lw.Bypass()
64 }
65
66 // Stop stops listening
67 func (p *progress) Stop() {
68 close(p.bars)
69 }
70
71 // server listens for updates and renders the progress bars
72 func (p *progress) server() {
73 t := time.NewTicker(30 * time.Millisecond)
6674 bars := make([]*Bar, 0)
6775 p.lw.Out = p.out
68 loop:
6976 for {
7077 select {
7178 case bar, ok := <-p.bars:
7279 if !ok {
73 break loop
80 t.Stop()
81 return
7482 }
7583 bars = append(bars, bar)
76 case <-p.ticker.C:
84 case <-t.C:
7785 for _, bar := range bars {
7886 fmt.Fprintln(p.lw, bar.String())
7987 }
8088 p.lw.Flush()
89 case d := <-p.interval:
90 t.Stop()
91 t = time.NewTicker(d)
8192 }
8293 }
8394 }