Skip to content

Commit

Permalink
check manualRefresh at bar.triggerCompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Feb 17, 2023
1 parent 54ce425 commit 00e7b6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
23 changes: 12 additions & 11 deletions bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type bState struct {
dropOnComplete bool
noPop bool
autoRefresh bool
manualRefresh bool
aDecorators []decor.Decorator
pDecorators []decor.Decorator
averageDecorators []decor.AverageDecorator
Expand Down Expand Up @@ -186,7 +187,7 @@ func (b *Bar) EnableTriggerComplete() {
if s.current >= s.total {
s.current = s.total
s.completed = true
b.triggerCompletion(s.autoRefresh, s.refreshCh)
b.triggerCompletion(s)
} else {
s.triggerComplete = true
}
Expand Down Expand Up @@ -214,7 +215,7 @@ func (b *Bar) SetTotal(total int64, triggerCompleteNow bool) {
if triggerCompleteNow {
s.current = s.total
s.completed = true
b.triggerCompletion(s.autoRefresh, s.refreshCh)
b.triggerCompletion(s)
}
}:
case <-b.done:
Expand All @@ -232,7 +233,7 @@ func (b *Bar) SetCurrent(current int64) {
if s.triggerComplete && s.current >= s.total {
s.current = s.total
s.completed = true
b.triggerCompletion(s.autoRefresh, s.refreshCh)
b.triggerCompletion(s)
}
}:
case <-b.done:
Expand All @@ -254,7 +255,7 @@ func (b *Bar) EwmaSetCurrent(current int64, iterDur time.Duration) {
if s.triggerComplete && s.current >= s.total {
s.current = s.total
s.completed = true
b.triggerCompletion(s.autoRefresh, s.refreshCh)
b.triggerCompletion(s)
}
}:
case <-b.done:
Expand Down Expand Up @@ -282,7 +283,7 @@ func (b *Bar) IncrInt64(n int64) {
if s.triggerComplete && s.current >= s.total {
s.current = s.total
s.completed = true
b.triggerCompletion(s.autoRefresh, s.refreshCh)
b.triggerCompletion(s)
}
}:
case <-b.done:
Expand Down Expand Up @@ -312,7 +313,7 @@ func (b *Bar) EwmaIncrInt64(n int64, iterDur time.Duration) {
if s.triggerComplete && s.current >= s.total {
s.current = s.total
s.completed = true
b.triggerCompletion(s.autoRefresh, s.refreshCh)
b.triggerCompletion(s)
}
}:
case <-b.done:
Expand Down Expand Up @@ -350,7 +351,7 @@ func (b *Bar) Abort(drop bool) {
}
s.aborted = true
s.dropOnComplete = drop
b.triggerCompletion(s.autoRefresh, s.refreshCh)
b.triggerCompletion(s)
}:
case <-b.done:
}
Expand Down Expand Up @@ -449,13 +450,13 @@ func (b *Bar) render(tw int) {
}
}

func (b *Bar) triggerCompletion(autoRefresh bool, refreshCh chan<- time.Time) {
if autoRefresh {
func (b *Bar) triggerCompletion(s *bState) {
if s.autoRefresh {
// Technically this call isn't required, but if refresh rate is set to
// one hour for example and bar completes within a few minutes p.Wait()
// will wait for one hour. This call helps to avoid unnecessary waiting.
go b.tryEarlyRefresh(refreshCh)
} else {
go b.tryEarlyRefresh(s.refreshCh)
} else if !s.manualRefresh {
b.cancel()
}
}
Expand Down
15 changes: 8 additions & 7 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,14 @@ func (s *pState) flush(cw *cwriter.Writer, height int) error {

func (s *pState) makeBarState(total int64, filler BarFiller, options ...BarOption) *bState {
bs := &bState{
id: s.idCount,
priority: s.idCount,
reqWidth: s.reqWidth,
total: total,
filler: filler,
refreshCh: s.refreshCh,
autoRefresh: s.autoRefresh,
id: s.idCount,
priority: s.idCount,
reqWidth: s.reqWidth,
total: total,
filler: filler,
refreshCh: s.refreshCh,
autoRefresh: s.autoRefresh,
manualRefresh: s.manualRefresh,
}

if total > 0 {
Expand Down

0 comments on commit 00e7b6f

Please sign in to comment.