done = true, if n > b.total
Vladimir Bauer
9 years ago
| 1 | 1 |
|
| 2 | 2 |
import (
|
| 3 | 3 |
"fmt"
|
| 4 | |
"os"
|
| 5 | 4 |
"sync"
|
| 6 | 5 |
"time"
|
| 7 | 6 |
)
|
|
| 79 | 78 |
|
| 80 | 79 |
stopCh chan struct{}
|
| 81 | 80 |
done chan struct{}
|
|
81 |
|
|
82 |
stopped bool
|
| 82 | 83 |
}
|
| 83 | 84 |
|
| 84 | 85 |
type Statistics struct {
|
|
| 167 | 168 |
}
|
| 168 | 169 |
|
| 169 | 170 |
func (b *Bar) Incr(n int) {
|
| 170 | |
// result := make(chan bool)
|
| 171 | |
// b.incrRequestCh <- &incrRequest{n, result}
|
| 172 | |
// return <-result
|
| 173 | 171 |
if !b.IsCompleted() {
|
| 174 | 172 |
b.incrCh <- n
|
| 175 | 173 |
}
|
|
| 186 | 184 |
select {
|
| 187 | 185 |
case i := <-b.incrCh:
|
| 188 | 186 |
n := completed + i
|
|
187 |
// fmt.Fprintf(os.Stderr, "n = %+v\n", n)
|
| 189 | 188 |
if n > b.total {
|
| 190 | 189 |
completed = b.total
|
|
190 |
done = true
|
| 191 | 191 |
break
|
| 192 | 192 |
}
|
| 193 | 193 |
b.updateTimePerItemEstimate(i, blockStartTime)
|
|
| 207 | 207 |
r.bufCh <- b.draw(buf, completed, appendFuncs, prependFuncs)
|
| 208 | 208 |
case <-b.flushedCh:
|
| 209 | 209 |
if done && !b.IsCompleted() {
|
| 210 | |
fmt.Fprintln(os.Stderr, "flushedCh: wg.Done")
|
|
210 |
// fmt.Fprintln(os.Stderr, "flushedCh: wg.Done")
|
| 211 | 211 |
close(b.done)
|
| 212 | 212 |
wg.Done()
|
| 213 | 213 |
}
|
| 214 | 214 |
case <-b.stopCh:
|
| 215 | |
fmt.Fprintln(os.Stderr, "received stop signal")
|
|
215 |
// fmt.Fprintln(os.Stderr, "received stop signal")
|
| 216 | 216 |
if !done {
|
| 217 | |
fmt.Fprintln(os.Stderr, "closing done chan: done = false")
|
|
217 |
// fmt.Fprintln(os.Stderr, "closing done chan: done = false")
|
| 218 | 218 |
close(b.done)
|
| 219 | |
done = true
|
| 220 | 219 |
wg.Done()
|
| 221 | 220 |
}
|
| 222 | 221 |
return
|
|
| 225 | 224 |
}
|
| 226 | 225 |
|
| 227 | 226 |
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 |
}
|
| 234 | 231 |
}
|
| 235 | 232 |
|
| 236 | 233 |
func (b *Bar) IsCompleted() bool {
|
| 26 | 26 |
// Width is the width of the progress bars
|
| 27 | 27 |
// Width int
|
| 28 | 28 |
|
| 29 | |
// lw *uilive.Writer
|
| 30 | |
|
| 31 | 29 |
op chan *operation
|
| 32 | 30 |
|
| 33 | 31 |
// new refresh interval to be send over this channel
|
| 34 | 32 |
interval chan time.Duration
|
| 35 | 33 |
|
| 36 | 34 |
wg *sync.WaitGroup
|
|
35 |
|
|
36 |
stopped bool
|
| 37 | 37 |
}
|
| 38 | 38 |
|
| 39 | 39 |
type operation struct {
|
|
| 45 | 45 |
// New returns a new progress bar with defaults
|
| 46 | 46 |
func New() *progress {
|
| 47 | 47 |
p := &progress{
|
| 48 | |
out: os.Stdout,
|
| 49 | |
// lw: uilive.New(),
|
|
48 |
out: os.Stdout,
|
| 50 | 49 |
op: make(chan *operation),
|
| 51 | 50 |
interval: make(chan time.Duration),
|
| 52 | 51 |
wg: new(sync.WaitGroup),
|
|
| 88 | 87 |
// return p.lw.Bypass()
|
| 89 | 88 |
// }
|
| 90 | 89 |
|
| 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 |
}
|
| 96 | 98 |
}
|
| 97 | 99 |
|
| 98 | 100 |
// server monitors underlying channels and renders any progress bars
|
|
| 105 | 107 |
select {
|
| 106 | 108 |
case op, ok := <-p.op:
|
| 107 | 109 |
if !ok {
|
| 108 | |
fmt.Fprintln(os.Stderr, "Sopping bars")
|
|
110 |
// fmt.Fprintln(os.Stderr, "Sopping bars")
|
| 109 | 111 |
for _, b := range bars {
|
| 110 | 112 |
b.Stop()
|
| 111 | 113 |
}
|