summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--internal/common/ui.go18
-rw-r--r--stdout/stdout.go22
-rw-r--r--stdout/stdout_test.go15
-rw-r--r--tui/format.go55
4 files changed, 65 insertions, 45 deletions
diff --git a/internal/common/ui.go b/internal/common/ui.go
index 3d8c0bd..6039fb1 100644
--- a/internal/common/ui.go
+++ b/internal/common/ui.go
@@ -18,3 +18,21 @@ type UI struct {
ShowApparentSize bool
PathChecker func(string) (fs.FileInfo, error)
}
+
+// file size constants
+const (
+ _ = iota
+ KB float64 = 1 << (10 * iota)
+ MB
+ GB
+ TB
+ PB
+ EB
+)
+
+// file count constants
+const (
+ K int = 1e3
+ M int = 1e6
+ G int = 1e9
+)
diff --git a/stdout/stdout.go b/stdout/stdout.go
index 7da9f7b..0d246ad 100644
--- a/stdout/stdout.go
+++ b/stdout/stdout.go
@@ -215,15 +215,21 @@ func (ui *UI) updateProgress() {
}
func (ui *UI) formatSize(size int64) string {
+ fsize := float64(size)
+
switch {
- case size > 1e12:
- return ui.orange.Sprintf("%.1f", float64(size)/math.Pow(2, 40)) + " TiB"
- case size > 1e9:
- return ui.orange.Sprintf("%.1f", float64(size)/math.Pow(2, 30)) + " GiB"
- case size > 1e6:
- return ui.orange.Sprintf("%.1f", float64(size)/math.Pow(2, 20)) + " MiB"
- case size > 1e3:
- return ui.orange.Sprintf("%.1f", float64(size)/math.Pow(2, 10)) + " KiB"
+ case fsize >= common.EB:
+ return ui.orange.Sprintf("%.1f", fsize/common.EB) + " EiB"
+ case fsize >= common.PB:
+ return ui.orange.Sprintf("%.1f", fsize/common.PB) + " PiB"
+ case fsize >= common.TB:
+ return ui.orange.Sprintf("%.1f", fsize/common.TB) + " TiB"
+ case fsize >= common.GB:
+ return ui.orange.Sprintf("%.1f", fsize/common.GB) + " GiB"
+ case fsize >= common.MB:
+ return ui.orange.Sprintf("%.1f", fsize/common.MB) + " MiB"
+ case fsize >= common.KB:
+ return ui.orange.Sprintf("%.1f", fsize/common.KB) + " KiB"
default:
return ui.orange.Sprintf("%d", size) + " B"
}
diff --git a/stdout/stdout_test.go b/stdout/stdout_test.go
index 9ad6c8d..1022285 100644
--- a/stdout/stdout_test.go
+++ b/stdout/stdout_test.go
@@ -86,7 +86,6 @@ func TestItemRows(t *testing.T) {
err := ui.AnalyzePath("test_dir", nil)
assert.Nil(t, err)
- assert.Contains(t, output.String(), "TiB")
assert.Contains(t, output.String(), "GiB")
assert.Contains(t, output.String(), "MiB")
assert.Contains(t, output.String(), "KiB")
@@ -133,6 +132,20 @@ func TestMaxInt(t *testing.T) {
assert.Equal(t, 4, maxInt(4, 2))
}
+func TestFormatSize(t *testing.T) {
+ output := bytes.NewBuffer(make([]byte, 10))
+
+ ui := CreateStdoutUI(output, true, true, true)
+
+ assert.Contains(t, ui.formatSize(1), "B")
+ assert.Contains(t, ui.formatSize(1<<10+1), "KiB")
+ assert.Contains(t, ui.formatSize(1<<20+1), "MiB")
+ assert.Contains(t, ui.formatSize(1<<30+1), "GiB")
+ assert.Contains(t, ui.formatSize(1<<40+1), "TiB")
+ assert.Contains(t, ui.formatSize(1<<50+1), "PiB")
+ assert.Contains(t, ui.formatSize(1<<60+1), "EiB")
+}
+
// func printBuffer(buff *bytes.Buffer) {
// for i, x := range buff.String() {
// println(i, string(x))
diff --git a/tui/format.go b/tui/format.go
index ec9e0e5..a18802d 100644
--- a/tui/format.go
+++ b/tui/format.go
@@ -3,27 +3,10 @@ package tui
import (
"fmt"
+ "github.com/dundee/gdu/v5/internal/common"
"github.com/dundee/gdu/v5/pkg/analyze"
)
-// file size constants
-const (
- _ = iota
- KB float64 = 1 << (10 * iota)
- MB
- GB
- TB
- PB
- EB
-)
-
-// file count constants
-const (
- K int = 1e3
- M int = 1e6
- G int = 1e9
-)
-
func (ui *UI) formatFileRow(item analyze.Item) string {
var part int
@@ -88,18 +71,18 @@ func (ui *UI) formatSize(size int64, reverseColor bool, transparentBg bool) stri
fsize := float64(size)
switch {
- case fsize >= EB:
- return fmt.Sprintf("%.1f%s EiB", fsize/EB, color)
- case fsize >= PB:
- return fmt.Sprintf("%.1f%s PiB", fsize/PB, color)
- case fsize >= TB:
- return fmt.Sprintf("%.1f%s TiB", fsize/TB, color)
- case fsize >= GB:
- return fmt.Sprintf("%.1f%s GiB", fsize/GB, color)
- case fsize >= MB:
- return fmt.Sprintf("%.1f%s MiB", fsize/MB, color)
- case fsize >= KB:
- return fmt.Sprintf("%.1f%s KiB", fsize/KB, color)
+ case fsize >= common.EB:
+ return fmt.Sprintf("%.1f%s EiB", fsize/common.EB, color)
+ case fsize >= common.PB:
+ return fmt.Sprintf("%.1f%s PiB", fsize/common.PB, color)
+ case fsize >= common.TB:
+ return fmt.Sprintf("%.1f%s TiB", fsize/common.TB, color)
+ case fsize >= common.GB:
+ return fmt.Sprintf("%.1f%s GiB", fsize/common.GB, color)
+ case fsize >= common.MB:
+ return fmt.Sprintf("%.1f%s MiB", fsize/common.MB, color)
+ case fsize >= common.KB:
+ return fmt.Sprintf("%.1f%s KiB", fsize/common.KB, color)
default:
return fmt.Sprintf("%d%s B", size, color)
}
@@ -110,12 +93,12 @@ func (ui *UI) formatCount(count int) string {
color := "[-::]"
switch {
- case count >= G:
- row += fmt.Sprintf("%.1f%sG", float64(count)/float64(G), color)
- case count >= M:
- row += fmt.Sprintf("%.1f%sM", float64(count)/float64(M), color)
- case count >= K:
- row += fmt.Sprintf("%.1f%sk", float64(count)/float64(K), color)
+ case count >= common.G:
+ row += fmt.Sprintf("%.1f%sG", float64(count)/float64(common.G), color)
+ case count >= common.M:
+ row += fmt.Sprintf("%.1f%sM", float64(count)/float64(common.M), color)
+ case count >= common.K:
+ row += fmt.Sprintf("%.1f%sk", float64(count)/float64(common.K), color)
default:
row += fmt.Sprintf("%d%s", count, color)
}