Skip to content

Commit

Permalink
address issue #67
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Jan 13, 2021
1 parent fa9a892 commit ed4401a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
6 changes: 3 additions & 3 deletions container_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
Expand Down
26 changes: 19 additions & 7 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ed4401a

Please sign in to comment.