Skip to content

Commit f2c9874

Browse files
committed
cmd: Don't zero-pad small byte numbers
1 parent eb98381 commit f2c9874

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎cmd/cmd.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ func formatSizeColored(size int64) string {
118118
}
119119
}
120120
if remainder > 0 || padRemainder {
121-
fmt.Fprintf(&sb, "%06d", remainder)
121+
if padRemainder {
122+
fmt.Fprintf(&sb, "%06d", remainder)
123+
} else {
124+
fmt.Fprintf(&sb, "%d", remainder)
125+
}
122126
}
123127
return sb.String()
124128
}

‎cmd/cmd_test.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/fatih/color"
7+
)
8+
9+
func TestFormatSizeColored(t *testing.T) {
10+
originalNoColor := color.NoColor
11+
color.NoColor = true
12+
t.Cleanup(func() { color.NoColor = originalNoColor })
13+
14+
tests := []struct {
15+
name string
16+
size int64
17+
want string
18+
}{
19+
{name: "zero", size: 0, want: "0"},
20+
{name: "small value", size: 42, want: "42"},
21+
{name: "five digit value", size: 99_999, want: "99999"},
22+
{name: "six digit value", size: 100_000, want: "100000"},
23+
{name: "megabyte grouping", size: 1_000_042, want: "1000042"},
24+
{name: "gigabyte grouping", size: 1_002_000_042, want: "1002000042"},
25+
}
26+
27+
for _, tt := range tests {
28+
t.Run(tt.name, func(t *testing.T) {
29+
if got := formatSizeColored(tt.size); got != tt.want {
30+
t.Fatalf("formatSizeColored(%d) = %q, want %q", tt.size, got, tt.want)
31+
}
32+
})
33+
}
34+
}

0 commit comments

Comments
 (0)