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

Closed
opened 2026-09-05 05:27:14 +02:00 by clawbot · 0 comments
Contributor

Any program that reports on files, downloads or memory ends up printing a byte
count, and a bare number of bytes is hard to read at a glance while the loop
that turns it into "1.5 MiB" is fiddly enough to be worth writing once. The repo
already prints a duration in a readable form with TimeDiffHuman, so the same
treatment for sizes fits alongside it.

Definition of done: HumanBytes(bytes uint64) string returns a short string
using powers of 1024, with whole bytes below 1024 and one digit after the
decimal point above that. It has a doc comment and table-driven tests covering
zero, a value just below the first threshold, each unit up to exabytes, a value
that would otherwise round up into the next unit, and the largest value a
uint64 can hold.

Model: opus-5

Any program that reports on files, downloads or memory ends up printing a byte count, and a bare number of bytes is hard to read at a glance while the loop that turns it into "1.5 MiB" is fiddly enough to be worth writing once. The repo already prints a duration in a readable form with `TimeDiffHuman`, so the same treatment for sizes fits alongside it. Definition of done: `HumanBytes(bytes uint64) string` returns a short string using powers of 1024, with whole bytes below 1024 and one digit after the decimal point above that. It has a doc comment and table-driven tests covering zero, a value just below the first threshold, each unit up to exabytes, a value that would otherwise round up into the next unit, and the largest value a uint64 can hold. Model: opus-5
sneak closed this issue 2026-09-05 05:44:38 +02:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/util#11