2011-06-22 23:36:30 +00:00
|
|
|
|
2012-01-13 21:23:03 +00:00
|
|
|
if {![exists -proc pretty_size]} {
|
2017-09-03 21:02:14 +00:00
|
|
|
proc pretty_size {size} {{units {bytes KiB MiB GiB TiB}}} {
|
2011-06-22 23:36:30 +00:00
|
|
|
|
2017-08-31 20:04:49 +00:00
|
|
|
# for {set i 0; set l [llength $units]; incr l -1} {
|
|
|
|
# $size > 1023 && $i < $l} {incr i} {
|
|
|
|
# set size $($size / 1024.0)
|
|
|
|
# }
|
|
|
|
|
|
|
|
# We need to know how many powers of 1024 there are in
|
|
|
|
# size. This calculates the answer effeciently. Testing
|
|
|
|
# shows that this version takes 2/3 the time of the above.
|
2017-08-31 20:11:10 +00:00
|
|
|
if {$size <= 0} {
|
2017-08-31 20:04:49 +00:00
|
|
|
set i 0
|
|
|
|
} else {
|
|
|
|
set i $(int(log($size) / log(1024)))
|
|
|
|
set size $($size / 1024.0 ** $i)
|
2011-06-22 23:36:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 17:30:17 +00:00
|
|
|
set size [string trimright \
|
|
|
|
[string trimright [format "%.2f" $size] "0"] "."]
|
2011-06-22 23:36:30 +00:00
|
|
|
|
|
|
|
return "$size [lindex $units $i]"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|