summaryrefslogtreecommitdiffstats
path: root/tui/format.go
diff options
context:
space:
mode:
authorDaniel Milde <daniel@milde.cz>2022-09-19 22:37:45 +0200
committerDaniel Milde <daniel@milde.cz>2022-09-19 22:45:43 +0200
commit70426a6f75c7d950878c9fa3b3dee3d851115286 (patch)
tree03289bdedc9998bcee326b75595053d2dd72e346 /tui/format.go
parent0491056750cfc15be89b6ddaa9015762481836a4 (diff)
feat: format negative numbers correctly
closes #178
Diffstat (limited to 'tui/format.go')
-rw-r--r--tui/format.go42
1 files changed, 23 insertions, 19 deletions
diff --git a/tui/format.go b/tui/format.go
index d756bd7..e67a9f0 100644
--- a/tui/format.go
+++ b/tui/format.go
@@ -2,6 +2,7 @@ package tui
import (
"fmt"
+ "math"
"github.com/dundee/gdu/v5/internal/common"
"github.com/dundee/gdu/v5/pkg/fs"
@@ -90,7 +91,7 @@ func (ui *UI) formatSize(size int64, reverseColor bool, transparentBg bool) stri
func (ui *UI) formatCount(count int) string {
row := ""
color := "[-::]"
- count64 := int64(count)
+ count64 := float64(count)
switch {
case count64 >= common.G:
@@ -106,18 +107,20 @@ func (ui *UI) formatCount(count int) string {
}
func formatWithBinPrefix(fsize float64, color string) string {
+ asize := math.Abs(fsize)
+
switch {
- case fsize >= common.Ei:
+ case asize >= common.Ei:
return fmt.Sprintf("%.1f%s EiB", fsize/common.Ei, color)
- case fsize >= common.Pi:
+ case asize >= common.Pi:
return fmt.Sprintf("%.1f%s PiB", fsize/common.Pi, color)
- case fsize >= common.Ti:
+ case asize >= common.Ti:
return fmt.Sprintf("%.1f%s TiB", fsize/common.Ti, color)
- case fsize >= common.Gi:
+ case asize >= common.Gi:
return fmt.Sprintf("%.1f%s GiB", fsize/common.Gi, color)
- case fsize >= common.Mi:
+ case asize >= common.Mi:
return fmt.Sprintf("%.1f%s MiB", fsize/common.Mi, color)
- case fsize >= common.Ki:
+ case asize >= common.Ki:
return fmt.Sprintf("%.1f%s KiB", fsize/common.Ki, color)
default:
return fmt.Sprintf("%d%s B", int64(fsize), color)
@@ -126,19 +129,20 @@ func formatWithBinPrefix(fsize float64, color string) string {
func formatWithDecPrefix(size int64, color string) string {
fsize := float64(size)
+ asize := math.Abs(fsize)
switch {
- case size >= common.E:
- return fmt.Sprintf("%.1f%s EB", fsize/float64(common.E), color)
- case size >= common.P:
- return fmt.Sprintf("%.1f%s PB", fsize/float64(common.P), color)
- case size >= common.T:
- return fmt.Sprintf("%.1f%s TB", fsize/float64(common.T), color)
- case size >= common.G:
- return fmt.Sprintf("%.1f%s GB", fsize/float64(common.G), color)
- case size >= common.M:
- return fmt.Sprintf("%.1f%s MB", fsize/float64(common.M), color)
- case size >= common.K:
- return fmt.Sprintf("%.1f%s kB", fsize/float64(common.K), color)
+ case asize >= common.E:
+ return fmt.Sprintf("%.1f%s EB", fsize/common.E, color)
+ case asize >= common.P:
+ return fmt.Sprintf("%.1f%s PB", fsize/common.P, color)
+ case asize >= common.T:
+ return fmt.Sprintf("%.1f%s TB", fsize/common.T, color)
+ case asize >= common.G:
+ return fmt.Sprintf("%.1f%s GB", fsize/common.G, color)
+ case asize >= common.M:
+ return fmt.Sprintf("%.1f%s MB", fsize/common.M, color)
+ case asize >= common.K:
+ return fmt.Sprintf("%.1f%s kB", fsize/common.K, color)
default:
return fmt.Sprintf("%d%s B", size, color)
}