Add HumanBytes to print a byte count in a readable form #12

Merged
sneak merged 1 commits from clawbot/util:proposal-human-bytes into master 2026-09-05 05:44:38 +02:00
Contributor

Adds HumanBytes(bytes uint64) string, which writes a byte count the way a
person reads it: "0 B", "1023 B", "1.0 KiB", "1.5 MiB", "16.0 EiB".

This belongs here because every program that touches files, downloads or memory
eventually prints a size, and it sits naturally beside the existing
TimeDiffHuman, which does the same job for durations.

Things to know:

  • The units are powers of 1024 with the "KiB" style names, not powers of 1000
    with "kB". That is the honest labelling for the arithmetic being done. A
    caller that wants disk-vendor units would need a separate function.
  • There is a second, easily missed step after the unit is chosen: the value is
    checked against the threshold once more after rounding, because 1048575 bytes
    divides to 1023.999 KiB, which would otherwise print as "1024.0 KiB" instead
    of "1.0 MiB". There is a test for exactly that value.
  • The largest uint64 is "16.0 EiB", so the loop cannot run past the last unit.
  • The code is in a new file, humanbytes.go, so that the ten proposal branches
    do not all conflict in the same place.
  • make test on this branch reports one failure, TestNowUnixMicro. That test
    already fails on master and is unrelated to this change.

Model: opus-5

Adds `HumanBytes(bytes uint64) string`, which writes a byte count the way a person reads it: "0 B", "1023 B", "1.0 KiB", "1.5 MiB", "16.0 EiB". This belongs here because every program that touches files, downloads or memory eventually prints a size, and it sits naturally beside the existing `TimeDiffHuman`, which does the same job for durations. Things to know: - The units are powers of 1024 with the "KiB" style names, not powers of 1000 with "kB". That is the honest labelling for the arithmetic being done. A caller that wants disk-vendor units would need a separate function. - There is a second, easily missed step after the unit is chosen: the value is checked against the threshold once more after rounding, because 1048575 bytes divides to 1023.999 KiB, which would otherwise print as "1024.0 KiB" instead of "1.0 MiB". There is a test for exactly that value. - The largest `uint64` is "16.0 EiB", so the loop cannot run past the last unit. - The code is in a new file, `humanbytes.go`, so that the ten proposal branches do not all conflict in the same place. - `make test` on this branch reports one failure, `TestNowUnixMicro`. That test already fails on `master` and is unrelated to this change. Model: opus-5
clawbot self-assigned this 2026-09-05 05:27:55 +02:00
clawbot added 1 commit 2026-09-05 05:27:55 +02:00
HumanBytes turns a byte count into a short string using powers of 1024, with
whole bytes below the first threshold and one decimal place above it. Comes
with a doc comment and table-driven tests. (closes #11)

Model: opus-5
sneak merged commit 64965b11ed into master 2026-09-05 05:44:38 +02:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/util#12