Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the existing humanize function to support GB/s rates #125

Merged
merged 1 commit into from Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 5 additions & 9 deletions progressbar.go
Expand Up @@ -702,19 +702,15 @@ func renderProgressBar(c config, s *state) (int, error) {
}
}

// show rolling average rate in kB/sec or MB/sec
if c.showBytes {
// show rolling average rate
if c.showBytes && averageRate > 0 && !math.IsInf(averageRate, 1) {
if bytesString == "" {
bytesString += "("
} else {
bytesString += ", "
}
kbPerSecond := averageRate / 1024.0
if kbPerSecond > 1024.0 {
bytesString += fmt.Sprintf("%0.3f MB/s", kbPerSecond/1024.0)
} else if kbPerSecond > 0 {
bytesString += fmt.Sprintf("%0.3f kB/s", kbPerSecond)
}
currentHumanize, currentSuffix := humanizeBytes(averageRate)
bytesString += fmt.Sprintf("%s%s/s", currentHumanize, currentSuffix)
}

// show iterations rate
Expand Down Expand Up @@ -928,7 +924,7 @@ func humanizeBytes(s float64) (string, string) {
sizes := []string{" B", " kB", " MB", " GB", " TB", " PB", " EB"}
base := 1024.0
if s < 10 {
return fmt.Sprintf("%2.0f", s), "B"
return fmt.Sprintf("%2.0f", s), sizes[0]
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[int(e)]
Expand Down
12 changes: 11 additions & 1 deletion progressbar_test.go
Expand Up @@ -233,7 +233,7 @@ func ExampleIgnoreLength_WithSpeed() {
bar.Add(11)

// Output:
// - (0.011 kB/s)
// - (11 B/s)
}

func TestBarSlowAdd(t *testing.T) {
Expand Down Expand Up @@ -274,6 +274,16 @@ func TestBarSmallBytes(t *testing.T) {
}
}

func TestBarFastBytes(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions64(1e8, OptionShowBytes(true), OptionShowCount(), OptionSetWidth(10), OptionSetWriter(&buf))
time.Sleep(time.Millisecond)
bar.Add(1e7)
if !strings.Contains(buf.String(), " GB/s)") {
t.Errorf("wrong string: %s", buf.String())
}
}

func TestBar(t *testing.T) {
bar := New(0)
if err := bar.Add(1); err == nil {
Expand Down