Skip to content

Commit

Permalink
Improve zero-checks in progress printer
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Sep 30, 2024
1 parent 7684ca3 commit 82085cf
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,41 +200,43 @@ func (p *Progress) Reset() {
}

func (p *Progress) speedStatus(s *Status) {
if p.IncrementalSpeed {
lc := s.LinesCompleted - p.prevStatus.LinesCompleted
bc := s.BytesCompleted - p.prevStatus.BytesCompleted
dc := s.DonePercent - p.prevStatus.DonePercent
ela := s.Elapsed - p.prevStatus.Elapsed

if lc > 0 {
s.SpeedLPS = float64(lc) / ela.Seconds()
}

if bc > 0 {
s.SpeedMBPS = (float64(bc) / ela.Seconds()) / (1024 * 1024)
}
if !p.IncrementalSpeed {
s.SpeedMBPS = (float64(s.BytesCompleted) / s.Elapsed.Seconds()) / (1024 * 1024)
s.SpeedLPS = float64(s.LinesCompleted) / s.Elapsed.Seconds()

if s.DonePercent > 0 {
s.Remaining = time.Duration((100.0 - s.DonePercent) * float64(ela) / dc)
s.Remaining = time.Duration(float64(100*s.Elapsed)/s.DonePercent) - s.Elapsed
s.Remaining = s.Remaining.Truncate(time.Second)
} else {
s.Remaining = 0
}

p.prevStatus = *s

return
}

s.SpeedMBPS = (float64(s.BytesCompleted) / s.Elapsed.Seconds()) / (1024 * 1024)
s.SpeedLPS = float64(s.LinesCompleted) / s.Elapsed.Seconds()
lc := s.LinesCompleted - p.prevStatus.LinesCompleted
bc := s.BytesCompleted - p.prevStatus.BytesCompleted
dc := s.DonePercent - p.prevStatus.DonePercent
ela := s.Elapsed - p.prevStatus.Elapsed

if s.DonePercent > 0 {
s.Remaining = time.Duration(float64(100*s.Elapsed)/s.DonePercent) - s.Elapsed
if ela != 0 {
if lc > 0 {
s.SpeedLPS = float64(lc) / ela.Seconds()
}

if bc > 0 {
s.SpeedMBPS = (float64(bc) / ela.Seconds()) / (1024 * 1024)
}
}

if dc > 0 {
s.Remaining = time.Duration((100.0 - s.DonePercent) * float64(ela) / dc)
s.Remaining = s.Remaining.Truncate(time.Second)
} else {
s.Remaining = 0
}

p.prevStatus = *s
}

func (p *Progress) printStatus(last bool) {
Expand Down

0 comments on commit 82085cf

Please sign in to comment.