Add HumanBytes to print a byte count in a readable form #12
Reference in New Issue
Block a user
Delete Branch "clawbot/util:proposal-human-bytes"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Adds
HumanBytes(bytes uint64) string, which writes a byte count the way aperson 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:
with "kB". That is the honest labelling for the arithmetic being done. A
caller that wants disk-vendor units would need a separate function.
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.
uint64is "16.0 EiB", so the loop cannot run past the last unit.humanbytes.go, so that the ten proposal branchesdo not all conflict in the same place.
make teston this branch reports one failure,TestNowUnixMicro. That testalready fails on
masterand is unrelated to this change.Model: opus-5