Codebase list golang-github-vbauerster-mpb / c910ca1
done = true, if n > b.total Vladimir Bauer 9 years ago
2 changed file(s) with 23 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "fmt"
4 "os"
54 "sync"
65 "time"
76 )
7978
8079 stopCh chan struct{}
8180 done chan struct{}
81
82 stopped bool
8283 }
8384
8485 type Statistics struct {
167168 }
168169
169170 func (b *Bar) Incr(n int) {
170 // result := make(chan bool)
171 // b.incrRequestCh <- &incrRequest{n, result}
172 // return <-result
173171 if !b.IsCompleted() {
174172 b.incrCh <- n
175173 }
186184 select {
187185 case i := <-b.incrCh:
188186 n := completed + i
187 // fmt.Fprintf(os.Stderr, "n = %+v\n", n)
189188 if n > b.total {
190189 completed = b.total
190 done = true
191191 break
192192 }
193193 b.updateTimePerItemEstimate(i, blockStartTime)
207207 r.bufCh <- b.draw(buf, completed, appendFuncs, prependFuncs)
208208 case <-b.flushedCh:
209209 if done && !b.IsCompleted() {
210 fmt.Fprintln(os.Stderr, "flushedCh: wg.Done")
210 // fmt.Fprintln(os.Stderr, "flushedCh: wg.Done")
211211 close(b.done)
212212 wg.Done()
213213 }
214214 case <-b.stopCh:
215 fmt.Fprintln(os.Stderr, "received stop signal")
215 // fmt.Fprintln(os.Stderr, "received stop signal")
216216 if !done {
217 fmt.Fprintln(os.Stderr, "closing done chan: done = false")
217 // fmt.Fprintln(os.Stderr, "closing done chan: done = false")
218218 close(b.done)
219 done = true
220219 wg.Done()
221220 }
222221 return
225224 }
226225
227226 func (b *Bar) Stop() {
228 b.stopCh <- struct{}{}
229 // if !b.IsCompleted() {
230 // fmt.Fprintln(os.Stderr, "sending to stopCh")
231 // } else {
232 // fmt.Fprintln(os.Stderr, "Stop: already stopped")
233 // }
227 if !b.stopped {
228 b.stopCh <- struct{}{}
229 b.stopped = true
230 }
234231 }
235232
236233 func (b *Bar) IsCompleted() bool {
2626 // Width is the width of the progress bars
2727 // Width int
2828
29 // lw *uilive.Writer
30
3129 op chan *operation
3230
3331 // new refresh interval to be send over this channel
3432 interval chan time.Duration
3533
3634 wg *sync.WaitGroup
35
36 stopped bool
3737 }
3838
3939 type operation struct {
4545 // New returns a new progress bar with defaults
4646 func New() *progress {
4747 p := &progress{
48 out: os.Stdout,
49 // lw: uilive.New(),
48 out: os.Stdout,
5049 op: make(chan *operation),
5150 interval: make(chan time.Duration),
5251 wg: new(sync.WaitGroup),
8887 // return p.lw.Bypass()
8988 // }
9089
91 // Stop stops listening
92 func (p *progress) Stop() {
93 fmt.Fprintln(os.Stderr, "p.Stop")
94 p.wg.Wait()
95 close(p.op)
90 // WaitAndStop stops listening
91 func (p *progress) WaitAndStop() {
92 if !p.stopped {
93 // fmt.Fprintln(os.Stderr, "p.WaitAndStop")
94 p.stopped = true
95 p.wg.Wait()
96 close(p.op)
97 }
9698 }
9799
98100 // server monitors underlying channels and renders any progress bars
105107 select {
106108 case op, ok := <-p.op:
107109 if !ok {
108 fmt.Fprintln(os.Stderr, "Sopping bars")
110 // fmt.Fprintln(os.Stderr, "Sopping bars")
109111 for _, b := range bars {
110112 b.Stop()
111113 }