diff --git a/options.go b/options.go index 2f5d1f8..05d2ecf 100644 --- a/options.go +++ b/options.go @@ -51,6 +51,14 @@ } } +// WithManualRefresh disables internal auto refresh time.Ticker. +// Refresh will occur upon receive value from provided ch. +func WithManualRefresh(ch <-chan time.Time) ProgressOption { + return func(s *pState) { + s.manualRefreshCh = ch + } +} + // WithCancel provide your cancel channel, // which you plan to close at some point. func WithCancel(ch <-chan struct{}) ProgressOption { diff --git a/progress.go b/progress.go index d2733d5..5fc91fd 100644 --- a/progress.go +++ b/progress.go @@ -44,6 +44,7 @@ // following are provided by user uwg *sync.WaitGroup + manualRefreshCh <-chan time.Time cancel <-chan struct{} shutdownNotifier chan struct{} waitBars map[*Bar]*Bar diff --git a/progress_posix.go b/progress_posix.go index 426f64d..545245a 100644 --- a/progress_posix.go +++ b/progress_posix.go @@ -11,14 +11,21 @@ func (p *Progress) serve(s *pState) { + var ticker *time.Ticker + var refreshCh <-chan time.Time + var winch chan os.Signal var resumeTimer *time.Timer var resumeEvent <-chan time.Time winchIdleDur := s.rr * 2 - winch := make(chan os.Signal, 2) - signal.Notify(winch, syscall.SIGWINCH) - ticker := time.NewTicker(s.rr) - refreshCh := ticker.C + if s.manualRefreshCh == nil { + ticker = time.NewTicker(s.rr) + refreshCh = ticker.C + winch = make(chan os.Signal, 2) + signal.Notify(winch, syscall.SIGWINCH) + } else { + refreshCh = s.manualRefreshCh + } for { select { @@ -26,8 +33,10 @@ op(s) case <-refreshCh: if s.zeroWait { - ticker.Stop() - signal.Stop(winch) + if s.manualRefreshCh == nil { + signal.Stop(winch) + ticker.Stop() + } if s.shutdownNotifier != nil { close(s.shutdownNotifier) } diff --git a/progress_windows.go b/progress_windows.go index acd2dbb..cab03d3 100644 --- a/progress_windows.go +++ b/progress_windows.go @@ -2,12 +2,21 @@ package mpb -import "time" +import ( + "time" +) func (p *Progress) serve(s *pState) { - ticker := time.NewTicker(s.rr) - refreshCh := ticker.C + var ticker *time.Ticker + var refreshCh <-chan time.Time + + if s.manualRefreshCh == nil { + ticker = time.NewTicker(s.rr) + refreshCh = ticker.C + } else { + refreshCh = s.manualRefreshCh + } for { select { @@ -15,7 +24,9 @@ op(s) case <-refreshCh: if s.zeroWait { - ticker.Stop() + if s.manualRefreshCh == nil { + ticker.Stop() + } if s.shutdownNotifier != nil { close(s.shutdownNotifier) }