goncdu/node/utils.go
2025-02-10 16:26:13 +08:00

27 lines
773 B
Go

package node
import "fmt"
// BytesToHumanReadable 将字节数转换为人类可读的字符串
func BytesToHumanReadable(num float64, suffix string) string {
for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} {
if num < 1024 {
return fmt.Sprintf("%3.1f %s%s", float64(num), unit, suffix)
}
num /= 1024
}
return fmt.Sprintf("%.1f Yi%s", float64(num), suffix)
}
// FormatSize 格式化大小为人类可读的格式
func FormatSize(size int64) string {
res := BytesToHumanReadable(float64(size), "B") //.PadLeft(10)
return fmt.Sprintf("%10s", res)
}
func (pn *PathNode) ShowChildrenSimple() {
for _, child := range pn.children {
fmt.Printf("%d %s %s %d\n", child.node_type, FormatSize(child.size), child.path, child.count)
}
}