mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
refactor byte unit formatting, add unit-specific display precision
This commit is contained in:
@@ -1,53 +1,84 @@
|
||||
package cwidgets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
kb = 1024
|
||||
mb = kb * 1024
|
||||
gb = mb * 1024
|
||||
tb = gb * 1024
|
||||
// byte ratio constants
|
||||
_ = iota
|
||||
kib float64 = 1 << (10 * iota)
|
||||
mib
|
||||
gib
|
||||
tib
|
||||
pib
|
||||
)
|
||||
|
||||
// convenience method
|
||||
func ByteFormatInt(n int) string {
|
||||
return ByteFormat(int64(n))
|
||||
var (
|
||||
units = []float64{
|
||||
1,
|
||||
kib,
|
||||
mib,
|
||||
gib,
|
||||
tib,
|
||||
pib,
|
||||
}
|
||||
|
||||
// short, full unit labels
|
||||
labels = [][2]string{
|
||||
[2]string{"B", "B"},
|
||||
[2]string{"K", "KiB"},
|
||||
[2]string{"M", "MiB"},
|
||||
[2]string{"G", "GiB"},
|
||||
[2]string{"T", "TiB"},
|
||||
[2]string{"P", "PiB"},
|
||||
}
|
||||
|
||||
// maximum displayed precision per unit
|
||||
maxPrecision = []int{
|
||||
0, // B
|
||||
0, // kib
|
||||
1, // mib
|
||||
1, // gib
|
||||
2, // tib
|
||||
2, // pib
|
||||
}
|
||||
)
|
||||
|
||||
// convenience methods
|
||||
func ByteFormat(n int) string { return byteFormat(float64(n), false) }
|
||||
func ByteFormatShort(n int) string { return byteFormat(float64(n), true) }
|
||||
func ByteFormat64(n int64) string { return byteFormat(float64(n), false) }
|
||||
func ByteFormat64Short(n int64) string { return byteFormat(float64(n), true) }
|
||||
|
||||
func byteFormat(n float64, short bool) string {
|
||||
i := len(units) - 1
|
||||
|
||||
for i > 0 {
|
||||
if n >= units[i] {
|
||||
n /= units[i]
|
||||
break
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
if short {
|
||||
return unpadFloat(n, maxPrecision[i]) + labels[i][0]
|
||||
}
|
||||
return unpadFloat(n, maxPrecision[i]) + labels[i][1]
|
||||
}
|
||||
|
||||
func ByteFormat(n int64) string {
|
||||
if n < kb {
|
||||
return fmt.Sprintf("%sB", strconv.FormatInt(n, 10))
|
||||
}
|
||||
if n < mb {
|
||||
n = n / kb
|
||||
return fmt.Sprintf("%sK", strconv.FormatInt(n, 10))
|
||||
}
|
||||
if n < gb {
|
||||
n = n / mb
|
||||
return fmt.Sprintf("%sM", strconv.FormatInt(n, 10))
|
||||
}
|
||||
if n < tb {
|
||||
nf := float64(n) / gb
|
||||
return fmt.Sprintf("%sG", unpadFloat(nf))
|
||||
}
|
||||
nf := float64(n) / tb
|
||||
return fmt.Sprintf("%sT", unpadFloat(nf))
|
||||
func unpadFloat(f float64, maxp int) string {
|
||||
return strconv.FormatFloat(f, 'f', getPrecision(f, maxp), 64)
|
||||
}
|
||||
|
||||
func unpadFloat(f float64) string {
|
||||
return strconv.FormatFloat(f, 'f', getPrecision(f), 64)
|
||||
}
|
||||
|
||||
func getPrecision(f float64) int {
|
||||
func getPrecision(f float64, maxp int) int {
|
||||
frac := int((f - float64(int(f))) * 100)
|
||||
if frac == 0 {
|
||||
if frac == 0 || maxp == 0 {
|
||||
return 0
|
||||
}
|
||||
if frac%10 == 0 {
|
||||
if frac%10 == 0 || maxp < 2 {
|
||||
return 1
|
||||
}
|
||||
return 2 // default precision
|
||||
return maxp
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user