WithManualRefresh
same for windows
Vladimir Bauer
7 years ago
| 50 | 50 |
}
|
| 51 | 51 |
}
|
| 52 | 52 |
|
|
53 |
// WithManualRefresh disables internal auto refresh time.Ticker.
|
|
54 |
// Refresh will occur upon receive value from provided ch.
|
|
55 |
func WithManualRefresh(ch <-chan time.Time) ProgressOption {
|
|
56 |
return func(s *pState) {
|
|
57 |
s.manualRefreshCh = ch
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
| 53 | 61 |
// WithCancel provide your cancel channel,
|
| 54 | 62 |
// which you plan to close at some point.
|
| 55 | 63 |
func WithCancel(ch <-chan struct{}) ProgressOption {
|
| 43 | 43 |
|
| 44 | 44 |
// following are provided by user
|
| 45 | 45 |
uwg *sync.WaitGroup
|
|
46 |
manualRefreshCh <-chan time.Time
|
| 46 | 47 |
cancel <-chan struct{}
|
| 47 | 48 |
shutdownNotifier chan struct{}
|
| 48 | 49 |
waitBars map[*Bar]*Bar
|
| 10 | 10 |
|
| 11 | 11 |
func (p *Progress) serve(s *pState) {
|
| 12 | 12 |
|
|
13 |
var ticker *time.Ticker
|
|
14 |
var refreshCh <-chan time.Time
|
|
15 |
var winch chan os.Signal
|
| 13 | 16 |
var resumeTimer *time.Timer
|
| 14 | 17 |
var resumeEvent <-chan time.Time
|
| 15 | 18 |
winchIdleDur := s.rr * 2
|
| 16 | |
winch := make(chan os.Signal, 2)
|
| 17 | |
signal.Notify(winch, syscall.SIGWINCH)
|
| 18 | 19 |
|
| 19 | |
ticker := time.NewTicker(s.rr)
|
| 20 | |
refreshCh := ticker.C
|
|
20 |
if s.manualRefreshCh == nil {
|
|
21 |
ticker = time.NewTicker(s.rr)
|
|
22 |
refreshCh = ticker.C
|
|
23 |
winch = make(chan os.Signal, 2)
|
|
24 |
signal.Notify(winch, syscall.SIGWINCH)
|
|
25 |
} else {
|
|
26 |
refreshCh = s.manualRefreshCh
|
|
27 |
}
|
| 21 | 28 |
|
| 22 | 29 |
for {
|
| 23 | 30 |
select {
|
|
| 25 | 32 |
op(s)
|
| 26 | 33 |
case <-refreshCh:
|
| 27 | 34 |
if s.zeroWait {
|
| 28 | |
ticker.Stop()
|
| 29 | |
signal.Stop(winch)
|
|
35 |
if s.manualRefreshCh == nil {
|
|
36 |
signal.Stop(winch)
|
|
37 |
ticker.Stop()
|
|
38 |
}
|
| 30 | 39 |
if s.shutdownNotifier != nil {
|
| 31 | 40 |
close(s.shutdownNotifier)
|
| 32 | 41 |
}
|
| 1 | 1 |
|
| 2 | 2 |
package mpb
|
| 3 | 3 |
|
| 4 | |
import "time"
|
|
4 |
import (
|
|
5 |
"time"
|
|
6 |
)
|
| 5 | 7 |
|
| 6 | 8 |
func (p *Progress) serve(s *pState) {
|
| 7 | 9 |
|
| 8 | |
ticker := time.NewTicker(s.rr)
|
| 9 | |
refreshCh := ticker.C
|
|
10 |
var ticker *time.Ticker
|
|
11 |
var refreshCh <-chan time.Time
|
|
12 |
|
|
13 |
if s.manualRefreshCh == nil {
|
|
14 |
ticker = time.NewTicker(s.rr)
|
|
15 |
refreshCh = ticker.C
|
|
16 |
} else {
|
|
17 |
refreshCh = s.manualRefreshCh
|
|
18 |
}
|
| 10 | 19 |
|
| 11 | 20 |
for {
|
| 12 | 21 |
select {
|
|
| 14 | 23 |
op(s)
|
| 15 | 24 |
case <-refreshCh:
|
| 16 | 25 |
if s.zeroWait {
|
| 17 | |
ticker.Stop()
|
|
26 |
if s.manualRefreshCh == nil {
|
|
27 |
ticker.Stop()
|
|
28 |
}
|
| 18 | 29 |
if s.shutdownNotifier != nil {
|
| 19 | 30 |
close(s.shutdownNotifier)
|
| 20 | 31 |
}
|