From 5ab3e6276a2ebb7089665cc605b0f9156acc8ca7 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Sat, 22 Oct 2022 10:19:00 +0200 Subject: [PATCH] ui: Fix FormatBytes at exactly 1024 time a unit 1024 would be displayed as "1024 bytes" instead of "1.000 KiB", etc. --- internal/ui/format.go | 8 ++++---- internal/ui/format_test.go | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/internal/ui/format.go b/internal/ui/format.go index dc0166089..13d02f9e3 100644 --- a/internal/ui/format.go +++ b/internal/ui/format.go @@ -8,13 +8,13 @@ import ( func FormatBytes(c uint64) string { b := float64(c) switch { - case c > 1<<40: + case c >= 1<<40: return fmt.Sprintf("%.3f TiB", b/(1<<40)) - case c > 1<<30: + case c >= 1<<30: return fmt.Sprintf("%.3f GiB", b/(1<<30)) - case c > 1<<20: + case c >= 1<<20: return fmt.Sprintf("%.3f MiB", b/(1<<20)) - case c > 1<<10: + case c >= 1<<10: return fmt.Sprintf("%.3f KiB", b/(1<<10)) default: return fmt.Sprintf("%d B", c) diff --git a/internal/ui/format_test.go b/internal/ui/format_test.go index 3be08d496..b6a1c13d1 100644 --- a/internal/ui/format_test.go +++ b/internal/ui/format_test.go @@ -8,8 +8,13 @@ func TestFormatBytes(t *testing.T) { want string }{ {0, "0 B"}, - {1025, "1.001 KiB"}, - {1<<30 + 7, "1.000 GiB"}, + {1023, "1023 B"}, + {1024, "1.000 KiB"}, + {5<<20 + 1<<19, "5.500 MiB"}, + {1 << 30, "1.000 GiB"}, + {2 << 30, "2.000 GiB"}, + {1<<40 - 1<<36, "960.000 GiB"}, + {1 << 40, "1.000 TiB"}, } { if got := FormatBytes(c.size); got != c.want { t.Errorf("want %q, got %q", c.want, got)