Skip to content

Commit

Permalink
bug fix: show bytes suffix if byte suffixes do not match total
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Sep 16, 2020
1 parent b72da62 commit 984611c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
28 changes: 18 additions & 10 deletions progressbar.go
Expand Up @@ -52,6 +52,8 @@ type state struct {

type config struct {
max int64 // max number of the counter
maxHumanized string
maxHumanizedSuffix string
width int
writer io.Writer
theme Theme
Expand Down Expand Up @@ -265,6 +267,8 @@ func NewOptions64(max int64, options ...Option) *ProgressBar {
b.config.predictTime = false
}

b.config.maxHumanized, b.config.maxHumanizedSuffix = humanizeBytes(float64(b.config.max))

if b.config.renderWithBlankState {
b.RenderBlank()
}
Expand Down Expand Up @@ -561,13 +565,20 @@ func renderProgressBar(c config, s state) (int, error) {
}
if !c.ignoreLength {
if c.showBytes {
bytesString += fmt.Sprintf("%s/%s", humanizeBytes(s.currentBytes, false), humanizeBytes(float64(c.max), true))
currentHumanize, currentSuffix := humanizeBytes(s.currentBytes)
if currentSuffix == c.maxHumanizedSuffix {
bytesString += fmt.Sprintf("%s/%s%s", currentHumanize, c.maxHumanized, c.maxHumanizedSuffix)
} else {
bytesString += fmt.Sprintf("%s%s/%s%s", currentHumanize, currentSuffix, c.maxHumanized, c.maxHumanizedSuffix)

}
} else {
bytesString += fmt.Sprintf("%.0f/%d", s.currentBytes, c.max)
}
} else {
if c.showBytes {
bytesString += fmt.Sprintf("%s", humanizeBytes(s.currentBytes, true))
currentHumanize, currentSuffix := humanizeBytes(s.currentBytes)
bytesString += fmt.Sprintf("%s%s", currentHumanize, currentSuffix)
} else {
bytesString += fmt.Sprintf("%.0f/%s", s.currentBytes, "-")
}
Expand Down Expand Up @@ -759,24 +770,21 @@ func average(xs []float64) float64 {
return total / float64(len(xs))
}

func humanizeBytes(s float64, withSuffix bool) string {
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 B", s)
return fmt.Sprintf("%2.0f", s), "B"
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[int(e)]
val := math.Floor(float64(s)/math.Pow(base, e)*10+0.5) / 10
f := "%.0f%s"
f := "%.0f"
if val < 10 {
f = "%.1f%s"
}
if !withSuffix {
suffix = ""
f = "%.1f"
}

return fmt.Sprintf(f, val, suffix)
return fmt.Sprintf(f, val), suffix
}

func logn(n, b float64) float64 {
Expand Down
19 changes: 19 additions & 0 deletions progressbar_test.go
Expand Up @@ -216,6 +216,25 @@ func ExampleIgnoreLength_WithSpeed() {
// | (0.011 kB/s)
}

func TestBarSmallBytes(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions64(100000000, OptionShowBytes(true), OptionShowCount(), OptionSetWidth(10), OptionSetWriter(&buf))
for i := 1; i < 10; i++ {
time.Sleep(100 * time.Millisecond)
bar.Add(1000)
}
if !strings.Contains(buf.String(), "8.8 kB/95 MB") {
t.Errorf("wrong string: %s", buf.String())
}
for i := 1; i < 10; i++ {
time.Sleep(10 * time.Millisecond)
bar.Add(1000000)
}
if !strings.Contains(buf.String(), "8.6/95 MB") {
t.Errorf("wrong string: %s", buf.String())
}
}

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

0 comments on commit 984611c

Please sign in to comment.