From 71c3eb56b6ddfc487d43e961228fd52d234962da Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 6 Feb 2022 03:00:32 +0400 Subject: [PATCH] Utility function to convert size ib bytes to its corresponding biggest unit. --- pkg/utils/filesize.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 pkg/utils/filesize.go diff --git a/pkg/utils/filesize.go b/pkg/utils/filesize.go new file mode 100644 index 0000000..fec6f49 --- /dev/null +++ b/pkg/utils/filesize.go @@ -0,0 +1,16 @@ +package utils + +import "fmt" + +var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB"} + +// ConvertFileSize converts size in bytes down to biggest units it represents. +// Returns converted size, unit and, a concatenation of size and unit +func ConvertFileSize(size int64) (int64, string, string) { + var idx int + + for idx = 0; size >= 1024; size /= 1024 { + } + + return size, sizeSuffixes[idx], fmt.Sprint(size, sizeSuffixes[idx]) +}