From c4c92f54d75bfbe45a96388325bba52c1d33ba1c Mon Sep 17 00:00:00 2001 From: s008nyx Date: Sun, 25 Apr 2021 12:24:54 +0300 Subject: [PATCH] Update main.go --- main.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index c96ad9e..d0a4df8 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ type Item struct { func main() { fmt.Println("----------------------------------------------------") - fmt.Println("------------| (Re)HLDS Installer v0.1 |-------------") + fmt.Println("------------| (Re)HLDS Installer v0.4 |-------------") fmt.Println("----------------------------------------------------") routes := make(map[int]string) @@ -75,16 +75,16 @@ func list(routes map[int]string, item Item, depth int) { } fmt.Println("Choose item (enter number):") - fmt.Printf("\n--------------------------------\n") + fmt.Printf("--------------------------------\n") for i, item := range data{ if isFile(item) { - fmt.Printf("%d: %s (%d Mb)\n", i + 1, item.Name, item.Size / 1024 / 1024) + fmt.Printf("%-3d | %-110s | %-9s |\n", i + 1, item.Name, ByteCountDecimal(item.Size)) } else { - fmt.Printf("%d: %s\n", i + 1, item.Name) + fmt.Printf("%-3d | %-110s | --- |\n", i + 1, item.Name) } } - fmt.Printf("\n--------------------------------\n") + fmt.Printf("--------------------------------\n") if depth <= 0 { fmt.Printf("0: Exit\n") } else { @@ -156,3 +156,16 @@ func load(routes map[int]string, item Item, depth int) error { return nil } + +func ByteCountDecimal(b uint64) string { + const unit = 1000 + if b < unit { + return fmt.Sprintf("%d B", b) + } + div, exp := int64(unit), 0 + for n := b / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp]) +}