aboutsummaryrefslogtreecommitdiff
path: root/internal/progress/humanize.go
blob: 8363d6b1499539cf623cd8b941375f60fb55465c (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package progress

import "fmt"

func humanizeBytes(n uint64) string {
	const unit = 1024
	if n < unit {
		return fmt.Sprintf("%d B", n)
	}

	value := float64(n)
	units := []string{"KiB", "MiB", "GiB", "TiB", "PiB"}
	for i := 0; i < len(units); i++ {
		value /= unit
		if value < unit || i == len(units)-1 {
			return fmt.Sprintf("%.2f %s", value, units[i])
		}
	}

	return fmt.Sprintf("%d B", n)
}