mirror of
https://github.com/moparisthebest/pacman
synced 2024-08-13 17:03:46 -04:00
a98fce0896
This function is the reverse of human_to_size, and converts integer byte sizes to human readable SI prefixed values. A logical extension of this might be to mimic the formatter that pacman uses and allow a second argument to be passed in which can coerce the size, rather than reducing until the unit count is below 1024. Signed-off-by: Dave Reisner <dreisner@archlinux.org>
21 lines
325 B
Bash
21 lines
325 B
Bash
size_to_human() {
|
|
awk -v size="$1" '
|
|
BEGIN {
|
|
suffix[1] = "B"
|
|
suffix[2] = "KiB"
|
|
suffix[3] = "MiB"
|
|
suffix[4] = "GiB"
|
|
suffix[5] = "TiB"
|
|
count = 1
|
|
|
|
while (size > 1024) {
|
|
size /= 1024
|
|
count++
|
|
}
|
|
|
|
sizestr = sprintf("%.2f", size)
|
|
sub(/\.?0+$/, "", sizestr)
|
|
printf("%s %s", sizestr, suffix[count])
|
|
}'
|
|
}
|