Skip to content

Commit 9454ec3

Browse files
committed
refactoring: pState.rmOnComplete
One bool is enough to track last bar with rmOnComplete option. minor: prefer switch to if else chain
1 parent 8ed132c commit 9454ec3

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

progress.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ type pState struct {
4242
hm heapManager
4343
renderReq chan time.Time
4444
idCount int
45-
shCount int
4645
popPriority int
4746

4847
// following are provided/overrode by user
@@ -59,6 +58,7 @@ type pState struct {
5958
uwg *sync.WaitGroup
6059
popCompleted bool
6160
autoRefresh bool
61+
rmOnComplete bool
6262
}
6363

6464
// New creates new Progress container instance. It's not possible to
@@ -314,13 +314,11 @@ func (p *Progress) serve(s *pState, cw *cwriter.Writer) {
314314
}
315315
}
316316
case <-p.done:
317-
// may enter the loop on last bar and frame.rmOnComplete
318-
for s.autoRefresh && s.shCount > 0 {
317+
if s.autoRefresh && s.rmOnComplete {
319318
if err := s.render(cw); err != nil {
320319
_, _ = fmt.Fprintln(s.debugOut, err.Error())
321320
return
322321
}
323-
s.shCount--
324322
}
325323
return
326324
}
@@ -378,6 +376,8 @@ func (s *pState) flush(cw *cwriter.Writer, height int, seq iter.Seq[*Bar]) error
378376
var total, popCount int
379377
var rows [][]io.Reader
380378

379+
s.rmOnComplete = false
380+
381381
for b := range seq {
382382
frame := <-b.frameCh
383383
if frame.err != nil {
@@ -403,15 +403,16 @@ func (s *pState) flush(cw *cwriter.Writer, height int, seq iter.Seq[*Bar]) error
403403
qb.bar.priority = b.priority
404404
go qb.bar.serve(qb.state)
405405
s.hm.push(qb.bar, true)
406-
s.shCount--
407-
} else if s.popCompleted && !frame.noPop {
408-
b.priority = s.popPriority
409-
s.popPriority++
410-
s.hm.push(b, false)
411-
s.shCount--
412-
} else if !frame.rmOnComplete {
413-
s.hm.push(b, false)
414-
s.shCount--
406+
} else {
407+
switch {
408+
case s.popCompleted && !frame.noPop:
409+
b.priority = s.popPriority
410+
s.popPriority++
411+
fallthrough
412+
case !frame.rmOnComplete:
413+
s.hm.push(b, false)
414+
}
415+
s.rmOnComplete = s.rmOnComplete || frame.rmOnComplete
415416
}
416417
b.cancel()
417418
case 2:
@@ -472,6 +473,5 @@ func (s *pState) makeBarState(total int64, filler BarFiller, options ...BarOptio
472473
bs.buffers[2] = bytes.NewBuffer(make([]byte, 0, 128)) // append
473474

474475
s.idCount++
475-
s.shCount++
476476
return bs
477477
}

0 commit comments

Comments
 (0)