diff --git a/container_option.go b/container_option.go index 0b14ebb0..ab8cb2df 100644 --- a/container_option.go +++ b/container_option.go @@ -40,9 +40,9 @@ func WithRefreshRate(d time.Duration) ContainerOption { // WithManualRefresh disables internal auto refresh time.Ticker. // Refresh will occur upon receive value from provided ch. -func WithManualRefresh(ch <-chan time.Time) ContainerOption { +func WithManualRefresh(ch <-chan interface{}) ContainerOption { return func(s *pState) { - s.refreshSrc = ch + s.externalRefresh = ch } } @@ -70,8 +70,8 @@ func WithShutdownNotifier(ch chan struct{}) ContainerOption { func WithOutput(w io.Writer) ContainerOption { return func(s *pState) { if w == nil { - s.refreshSrc = make(chan time.Time) s.output = ioutil.Discard + s.outputDiscarded = true return } s.output = w diff --git a/progress.go b/progress.go index e479994f..c51caac8 100644 --- a/progress.go +++ b/progress.go @@ -46,9 +46,10 @@ type pState struct { idCount int reqWidth int popCompleted bool + outputDiscarded bool rr time.Duration uwg *sync.WaitGroup - refreshSrc <-chan time.Time + externalRefresh <-chan interface{} renderDelay <-chan struct{} shutdownNotifier chan struct{} parkedBars map[*Bar]*Bar @@ -234,15 +235,26 @@ func (s *pState) newTicker(done <-chan struct{}) chan time.Time { if s.renderDelay != nil { <-s.renderDelay } - if s.refreshSrc == nil { - ticker := time.NewTicker(s.rr) - defer ticker.Stop() - s.refreshSrc = ticker.C + var internalRefresh <-chan time.Time + if !s.outputDiscarded { + if s.externalRefresh == nil { + ticker := time.NewTicker(s.rr) + defer ticker.Stop() + internalRefresh = ticker.C + } + } else { + s.externalRefresh = nil } for { select { - case tick := <-s.refreshSrc: - ch <- tick + case t := <-internalRefresh: + ch <- t + case x := <-s.externalRefresh: + if t, ok := x.(time.Time); ok { + ch <- t + } else { + ch <- time.Now() + } case <-done: close(s.shutdownNotifier) return