|
@@ -37,22 +37,34 @@ var (
|
|
|
var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
|
|
var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
|
|
|
|
|
|
-// CustomSize returns a human-readable approximation of a size
|
|
|
-// using custom format.
|
|
|
-func CustomSize(format string, size float64, base float64, _map []string) string {
|
|
|
+func getSizeAndUnit(size float64, base float64, _map []string) (float64, string) {
|
|
|
i := 0
|
|
|
unitsLimit := len(_map) - 1
|
|
|
for size >= base && i < unitsLimit {
|
|
|
size = size / base
|
|
|
i++
|
|
|
}
|
|
|
- return fmt.Sprintf(format, size, _map[i])
|
|
|
+ return size, _map[i]
|
|
|
+}
|
|
|
+
|
|
|
+// CustomSize returns a human-readable approximation of a size
|
|
|
+// using custom format.
|
|
|
+func CustomSize(format string, size float64, base float64, _map []string) string {
|
|
|
+ size, unit := getSizeAndUnit(size, base, _map)
|
|
|
+ return fmt.Sprintf(format, size, unit)
|
|
|
+}
|
|
|
+
|
|
|
+// HumanSizeWithPrecision allows the size to be in any precision,
|
|
|
+// instead of 4 digit precision used in units.HumanSize.
|
|
|
+func HumanSizeWithPrecision(size float64, precision int) string {
|
|
|
+ size, unit := getSizeAndUnit(size, 1000.0, decimapAbbrs)
|
|
|
+ return fmt.Sprintf("%.*g %s", precision, size, unit)
|
|
|
}
|
|
|
|
|
|
// HumanSize returns a human-readable approximation of a size
|
|
|
// capped at 4 valid numbers (eg. "2.746 MB", "796 KB").
|
|
|
func HumanSize(size float64) string {
|
|
|
- return CustomSize("%.4g %s", size, 1000.0, decimapAbbrs)
|
|
|
+ return HumanSizeWithPrecision(size, 4)
|
|
|
}
|
|
|
|
|
|
// BytesSize returns a human-readable size in bytes, kibibytes,
|