Skip to content

Commit

Permalink
progress: option to display progress speed (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Sep 3, 2022
1 parent 782119a commit ab70b68
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 70 deletions.
5 changes: 5 additions & 0 deletions cmd/demo-progress/demo.go
Expand Up @@ -18,6 +18,8 @@ var (
flagHidePercentage = flag.Bool("hide-percentage", false, "Hide the progress percent?")
flagHideTime = flag.Bool("hide-time", false, "Hide the time taken?")
flagHideValue = flag.Bool("hide-value", false, "Hide the tracker value?")
flagHideSpeed = flag.Bool("hide-speed", false, "Hide the tracker speed?")
flagHideSpeedOverall = flag.Bool("hide-speed-overall", false, "Hide the overall tracker speed?")
flagNumTrackers = flag.Int("num-trackers", 13, "Number of Trackers")
flagRandomFail = flag.Bool("rnd-fail", false, "Introduce random failures in tracking")
flagRandomLogs = flag.Bool("rnd-logs", false, "Output random logs in the middle of tracking")
Expand Down Expand Up @@ -113,12 +115,15 @@ func main() {
pw.SetUpdateFrequency(time.Millisecond * 100)
pw.Style().Colors = progress.StyleColorsExample
pw.Style().Options.PercentFormat = "%4.1f%%"
pw.Style().Options.SpeedOverallFormatter = progress.FormatNumber
pw.Style().Visibility.ETA = !*flagHideETA
pw.Style().Visibility.ETAOverall = !*flagHideETAOverall
pw.Style().Visibility.Percentage = !*flagHidePercentage
pw.Style().Visibility.Time = !*flagHideTime
pw.Style().Visibility.TrackerOverall = !*flagHideOverallTracker
pw.Style().Visibility.Value = !*flagHideValue
pw.Style().Visibility.Speed = !*flagHideSpeed
pw.Style().Visibility.SpeedOverall = !*flagHideSpeedOverall

// call Render() in async mode; yes we don't have any trackers at the moment
go pw.Render()
Expand Down
Binary file modified progress/images/demo.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions progress/render.go
Expand Up @@ -322,6 +322,9 @@ func (p *Progress) renderTrackerStats(out *strings.Builder, t *Tracker, hint ren
if !hint.hideValue || !hint.hideTime {
var outStats strings.Builder
outStats.WriteString(" [")

p.renderTrackerSpeed(&outStats, t, hint)

if !hint.hideValue {
outStats.WriteString(p.style.Colors.Value.Sprint(t.Units.Sprint(t.Value())))
}
Expand Down Expand Up @@ -372,3 +375,42 @@ func (p *Progress) renderTrackerStatsETA(out *strings.Builder, t *Tracker, hint
out.WriteString(p.style.Colors.Time.Sprint(eta))
}
}

func (p *Progress) renderTrackerSpeed(out *strings.Builder, t *Tracker, hint renderHint) {
if hint.isOverallTracker && !p.style.Visibility.SpeedOverall {
return
}
if !hint.isOverallTracker && !p.style.Visibility.Speed {
return
}

tpSpeed := p.style.Options.SpeedPrecision

write := func(speed float64, formatter func(int64) string) {
if formatter == nil {
formatter = FormatNumber
}
out.WriteString(p.style.Colors.Speed.Sprint(formatter(int64(speed))))
out.WriteString("/s; ")
}

if hint.isOverallTracker {
speed := float64(0)

p.trackersActiveMutex.RLock()
for _, tracker := range p.trackersActive {
speed += float64(tracker.Value()) / time.Since(tracker.timeStart).Round(tpSpeed).Seconds()
}
p.trackersActiveMutex.RUnlock()

if speed > 0 {
write(speed, p.style.Options.SpeedOverallFormatter)
}
return
}

since := time.Since(t.timeStart)
if eta := since.Round(tpSpeed); eta > tpSpeed {
write(float64(t.Value())/eta.Seconds(), t.Units.Formatter)
}
}

0 comments on commit ab70b68

Please sign in to comment.