summaryrefslogtreecommitdiff
path: root/internal/textutil
diff options
context:
space:
mode:
Diffstat (limited to 'internal/textutil')
-rw-r--r--internal/textutil/human.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/internal/textutil/human.go b/internal/textutil/human.go
new file mode 100644
index 0000000..21907c3
--- /dev/null
+++ b/internal/textutil/human.go
@@ -0,0 +1,25 @@
+package textutil
+
+import "fmt"
+
+// HumanBytes renders n in a short human-friendly form using base-1000 units.
+// Examples: 999 -> 999B, 1200 -> 1.2k, 1540000 -> 1.5M
+func HumanBytes(n int64) string {
+ if n < 1000 {
+ return fmt.Sprintf("%dB", n)
+ }
+ const unit = 1000.0
+ v := float64(n)
+ suffix := []string{"k", "M", "G", "T"}
+ i := 0
+ for v >= unit && i < len(suffix)-1 {
+ v /= unit
+ i++
+ }
+ s := fmt.Sprintf("%.1f%s", v, suffix[i])
+ // Strip trailing ".0"
+ if len(s) >= 3 && s[len(s)-2:] == ".0" {
+ s = fmt.Sprintf("%d%s", int(v), suffix[i])
+ }
+ return s
+}