diff options
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | cmd/gdu/app/app.go | 4 | ||||
-rw-r--r-- | cmd/gdu/main.go | 1 | ||||
-rw-r--r-- | gdu.1 | 7 | ||||
-rw-r--r-- | gdu.1.md | 2 | ||||
-rw-r--r-- | internal/common/ui.go | 14 | ||||
-rw-r--r-- | report/export.go | 38 | ||||
-rw-r--r-- | report/export_test.go | 27 | ||||
-rw-r--r-- | stdout/stdout.go | 30 | ||||
-rw-r--r-- | stdout/stdout_linux_test.go | 2 | ||||
-rw-r--r-- | stdout/stdout_test.go | 40 | ||||
-rw-r--r-- | tui/actions_linux_test.go | 2 | ||||
-rw-r--r-- | tui/actions_test.go | 30 | ||||
-rw-r--r-- | tui/filter_test.go | 8 | ||||
-rw-r--r-- | tui/format.go | 54 | ||||
-rw-r--r-- | tui/format_test.go | 22 | ||||
-rw-r--r-- | tui/keys_test.go | 54 | ||||
-rw-r--r-- | tui/sort_test.go | 6 | ||||
-rw-r--r-- | tui/tui.go | 9 | ||||
-rw-r--r-- | tui/tui_test.go | 26 |
20 files changed, 268 insertions, 109 deletions
@@ -88,6 +88,7 @@ Flags: -o, --output-file string Export all info into file as JSON -a, --show-apparent-size Show apparent size -d, --show-disks Show all mounted disks + --si Show sizes with decimal SI prefixes (kB, MB, GB) instead of binary prefixes (KiB, MiB, GiB) -s, --summarize Show only a total in non-interactive mode -v, --version Print version ``` diff --git a/cmd/gdu/app/app.go b/cmd/gdu/app/app.go index 7e67732..0dbd922 100644 --- a/cmd/gdu/app/app.go +++ b/cmd/gdu/app/app.go @@ -57,6 +57,7 @@ type Flags struct { Profiling bool EnableGC bool Summarize bool + UseSIPrefix bool } // App defines the main application @@ -178,6 +179,7 @@ func (a *App) createUI() (UI, error) { !a.Flags.NoColor && a.Istty, !a.Flags.NoProgress && a.Istty, a.Flags.EnableGC, + a.Flags.UseSIPrefix, ) return ui, nil } @@ -190,6 +192,7 @@ func (a *App) createUI() (UI, error) { a.Flags.ShowApparentSize, a.Flags.Summarize, a.Flags.EnableGC, + a.Flags.UseSIPrefix, ) } else { ui = tui.CreateUI( @@ -199,6 +202,7 @@ func (a *App) createUI() (UI, error) { !a.Flags.NoColor, a.Flags.ShowApparentSize, a.Flags.EnableGC, + a.Flags.UseSIPrefix, ) if !a.Flags.NoColor { diff --git a/cmd/gdu/main.go b/cmd/gdu/main.go index 5ece2ac..579fb9e 100644 --- a/cmd/gdu/main.go +++ b/cmd/gdu/main.go @@ -51,6 +51,7 @@ func init() { flags.BoolVarP(&af.NonInteractive, "non-interactive", "n", false, "Do not run in interactive mode") flags.BoolVarP(&af.NoProgress, "no-progress", "p", false, "Do not show progress in non-interactive mode") flags.BoolVarP(&af.Summarize, "summarize", "s", false, "Show only a total in non-interactive mode") + flags.BoolVar(&af.UseSIPrefix, "si", false, "Show sizes with decimal SI prefixes (kB, MB, GB) instead of binary prefixes (KiB, MiB, GiB)") } func runE(command *cobra.Command, args []string) error { @@ -1,6 +1,6 @@ -.\" Automatically generated by Pandoc 2.14.1 +.\" Automatically generated by Pandoc 2.14.2 .\" -.TH "gdu" "1" "2021-11-27" "" "" +.TH "gdu" "1" "2022-01-16" "" "" .hy .SH NAME .PP @@ -53,6 +53,9 @@ non-interactive mode .PP \f[B]-a\f[R], \f[B]--show-apparent-size\f[R][=false] Show apparent size .PP +\f[B]--si\f[R][=false] Show sizes with decimal SI prefixes (kB, MB, GB) +instead of binary prefixes (KiB, MiB, GiB) +.PP \f[B]-f\f[R], \f[B]-\[em]input-file\f[R] Import analysis from JSON file. If the file is \[dq]-\[dq], read from standard input. .PP @@ -53,6 +53,8 @@ non-interactive mode **-a**, **\--show-apparent-size**\[=false\] Show apparent size +**\--si**\[=false\] Show sizes with decimal SI prefixes (kB, MB, GB) instead of binary prefixes (KiB, MiB, GiB) + **-f**, **\----input-file** Import analysis from JSON file. If the file is \"-\", read from standard input. **-o**, **\----output-file** Export all info into file as JSON. If the file is \"-\", write to standard output. diff --git a/internal/common/ui.go b/internal/common/ui.go index 4beac89..d9d9bcb 100644 --- a/internal/common/ui.go +++ b/internal/common/ui.go @@ -12,12 +12,13 @@ type UI struct { IgnoreDirPathPatterns *regexp.Regexp IgnoreHidden bool UseColors bool + UseSIPrefix bool ShowProgress bool ShowApparentSize bool EnableGC bool } -// file size constants +// binary multiplies prefixes (IEC) const ( _ = iota Ki float64 = 1 << (10 * iota) @@ -28,11 +29,14 @@ const ( Ei ) -// file count constants +// SI prefixes const ( - K int = 1e3 - M int = 1e6 - G int = 1e9 + K int64 = 1e3 + M int64 = 1e6 + G int64 = 1e9 + T int64 = 1e12 + P int64 = 1e15 + E int64 = 1e18 ) // FormatNumber returns number as a string with thousands separator diff --git a/report/export.go b/report/export.go index 7396ab3..dd1505a 100644 --- a/report/export.go +++ b/report/export.go @@ -31,12 +31,20 @@ type UI struct { } // CreateExportUI creates UI for stdout -func CreateExportUI(output io.Writer, exportOutput io.Writer, useColors bool, showProgress bool, enableGC bool) *UI { +func CreateExportUI( + output io.Writer, + exportOutput io.Writer, + useColors bool, + showProgress bool, + enableGC bool, + useSIPrefix bool, +) *UI { ui := &UI{ UI: &common.UI{ ShowProgress: showProgress, Analyzer: analyze.CreateAnalyzer(), EnableGC: enableGC, + UseSIPrefix: useSIPrefix, }, output: output, exportOutput: exportOutput, @@ -180,6 +188,13 @@ func (ui *UI) updateProgress() { } func (ui *UI) formatSize(size int64) string { + if ui.UseSIPrefix { + return ui.formatWithDecPrefix(size) + } + return ui.formatWithBinPrefix(size) +} + +func (ui *UI) formatWithBinPrefix(size int64) string { fsize := float64(size) switch { @@ -199,3 +214,24 @@ func (ui *UI) formatSize(size int64) string { return ui.orange.Sprintf("%d", size) + " B" } } + +func (ui *UI) formatWithDecPrefix(size int64) string { + fsize := float64(size) + + switch { + case size >= common.E: + return ui.orange.Sprintf("%.1f", fsize/float64(common.E)) + " EB" + case size >= common.P: + return ui.orange.Sprintf("%.1f", fsize/float64(common.P)) + " PB" + case size >= common.T: + return ui.orange.Sprintf("%.1f", fsize/float64(common.T)) + " TB" + case size >= common.G: + return ui.orange.Sprintf("%.1f", fsize/float64(common.G)) + " GB" + case size >= common.M: + return ui.orange.Sprintf("%.1f", fsize/float64(common.M)) + " MB" + case size >= common.K: + return ui.orange.Sprintf("%.1f", fsize/float64(common.K)) + " kB" + default: + return ui.orange.Sprintf("%d", size) + " B" + } +} diff --git a/report/export_test.go b/report/export_test.go index 4f7155e..3d141be 100644 --- a/report/export_test.go +++ b/report/export_test.go @@ -23,7 +23,7 @@ func TestAnalyzePath(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) reportOutput := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, false, false, false) + ui := CreateExportUI(output, reportOutput, false, false, false, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -40,7 +40,7 @@ func TestAnalyzePathWithProgress(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) reportOutput := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, true, true, true) + ui := CreateExportUI(output, reportOutput, true, true, true, true) ui.SetIgnoreDirPaths([]string{"/xxx"}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -54,7 +54,7 @@ func TestShowDevices(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) reportOutput := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, false, true, false) + ui := CreateExportUI(output, reportOutput, false, true, false, false) err := ui.ListDevices(device.Getter) assert.Contains(t, err.Error(), "not supported") @@ -64,7 +64,7 @@ func TestReadAnalysisWhileExporting(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) reportOutput := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, false, true, false) + ui := CreateExportUI(output, reportOutput, false, true, false, false) err := ui.ReadAnalysis(output) assert.Contains(t, err.Error(), "not possible while exporting") @@ -82,7 +82,7 @@ func TestExportToFile(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, false, true, false) + ui := CreateExportUI(output, reportOutput, false, true, false, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) err = ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -104,7 +104,7 @@ func TestFormatSize(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) reportOutput := bytes.NewBuffer(make([]byte, 10)) - ui := CreateExportUI(output, reportOutput, false, true, false) + ui := CreateExportUI(output, reportOutput, false, true, false, false) assert.Contains(t, ui.formatSize(1), "B") assert.Contains(t, ui.formatSize(1<<10+1), "KiB") @@ -114,3 +114,18 @@ func TestFormatSize(t *testing.T) { assert.Contains(t, ui.formatSize(1<<50+1), "PiB") assert.Contains(t, ui.formatSize(1<<60+1), "EiB") } + +func TestFormatSizeDec(t *testing.T) { + output := bytes.NewBuffer(make([]byte, 10)) + reportOutput := bytes.NewBuffer(make([]byte, 10)) + + ui := CreateExportUI(output, reportOutput, false, true, false, true) + + assert.Contains(t, ui.formatSize(1), "B") + assert.Contains(t, ui.formatSize(1<<10+1), "kB") + assert.Contains(t, ui.formatSize(1<<20+1), "MB") + assert.Contains(t, ui.formatSize(1<<30+1), "GB") + assert.Contains(t, ui.formatSize(1<<40+1), "TB") + assert.Contains(t, ui.formatSize(1<<50+1), "PB") + assert.Contains(t, ui.formatSize(1<<60+1), "EB") +} diff --git a/stdout/stdout.go b/stdout/stdout.go index b4cb59b..4e6b4af 100644 --- a/stdout/stdout.go +++ b/stdout/stdout.go @@ -38,6 +38,7 @@ func CreateStdoutUI( showApparentSize bool, summarize bool, enableGC bool, + useSIPrefix bool, ) *UI { ui := &UI{ UI: &common.UI{ @@ -46,6 +47,7 @@ func CreateStdoutUI( ShowApparentSize: showApparentSize, Analyzer: analyze.CreateAnalyzer(), EnableGC: enableGC, + UseSIPrefix: useSIPrefix, }, output: output, summarize: summarize, @@ -332,6 +334,13 @@ func (ui *UI) updateProgress() { } func (ui *UI) formatSize(size int64) string { + if ui.UseSIPrefix { + return ui.formatWithDecPrefix(size) + } + return ui.formatWithBinPrefix(size) +} + +func (ui *UI) formatWithBinPrefix(size int64) string { fsize := float64(size) switch { @@ -352,6 +361,27 @@ func (ui *UI) formatSize(size int64) string { } } +func (ui *UI) formatWithDecPrefix(size int64) string { + fsize := float64(size) + + switch { + case size >= common.E: + return ui.orange.Sprintf("%.1f", fsize/float64(common.E)) + " EB" + case size >= common.P: + return ui.orange.Sprintf("%.1f", fsize/float64(common.P)) + " PB" + case size >= common.T: + return ui.orange.Sprintf("%.1f", fsize/float64(common.T)) + " TB" + case size >= common.G: + return ui.orange.Sprintf("%.1f", fsize/float64(common.G)) + " GB" + case size >= common.M: + return ui.orange.Sprintf("%.1f", fsize/float64(common.M)) + " MB" + case size >= common.K: + return ui.orange.Sprintf("%.1f", fsize/float64(common.K)) + " kB" + default: + return ui.orange.Sprintf("%d", size) + " B" + } +} + func maxLength(list []*device.Device, keyGetter func(*device.Device) string) int { maxLen := 0 var s string diff --git a/stdout/stdout_linux_test.go b/stdout/stdout_linux_test.go index 510fa50..69ffab2 100644 --- a/stdout/stdout_linux_test.go +++ b/stdout/stdout_linux_test.go @@ -21,7 +21,7 @@ func TestShowDevicesWithErr(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) getter := device.LinuxDevicesInfoGetter{MountsPath: "/xyzxyz"} - ui := CreateStdoutUI(output, false, true, false, false, false) + ui := CreateStdoutUI(output, false, true, false, false, false, false) err := ui.ListDevices(getter) assert.Contains(t, err.Error(), "no such file") diff --git a/stdout/stdout_test.go b/stdout/stdout_test.go index 49c4a81..e0549db 100644 --- a/stdout/stdout_test.go +++ b/stdout/stdout_test.go @@ -25,7 +25,7 @@ func TestAnalyzePath(t *testing.T) { buff := make([]byte, 10) output := bytes.NewBuffer(buff) - ui := CreateStdoutUI(output, false, false, false, false, true) + ui := CreateStdoutUI(output, false, false, false, false, true, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -42,7 +42,7 @@ func TestShowSummary(t *testing.T) { buff := make([]byte, 10) output := bytes.NewBuffer(buff) - ui := CreateStdoutUI(output, true, false, true, true, false) + ui := CreateStdoutUI(output, true, false, true, true, false, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -59,7 +59,7 @@ func TestShowSummaryBw(t *testing.T) { buff := make([]byte, 10) output := bytes.NewBuffer(buff) - ui := CreateStdoutUI(output, false, false, false, true, false) + ui := CreateStdoutUI(output, false, false, false, true, false, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -76,7 +76,7 @@ func TestAnalyzeSubdir(t *testing.T) { buff := make([]byte, 10) output := bytes.NewBuffer(buff) - ui := CreateStdoutUI(output, false, false, false, false, false) + ui := CreateStdoutUI(output, false, false, false, false, false, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) err := ui.AnalyzePath("test_dir/nested", nil) assert.Nil(t, err) @@ -93,7 +93,7 @@ func TestAnalyzePathWithColors(t *testing.T) { buff := make([]byte, 10) output := bytes.NewBuffer(buff) - ui := CreateStdoutUI(output, true, false, true, false, false) + ui := CreateStdoutUI(output, true, false, true, false, false, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) err := ui.AnalyzePath("test_dir/nested", nil) @@ -104,7 +104,7 @@ func TestAnalyzePathWithColors(t *testing.T) { func TestItemRows(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) - ui := CreateStdoutUI(output, false, true, false, false, false) + ui := CreateStdoutUI(output, false, true, false, false, false, false) ui.Analyzer = &testanalyze.MockedAnalyzer{} err := ui.AnalyzePath("test_dir", nil) @@ -118,7 +118,7 @@ func TestAnalyzePathWithProgress(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) - ui := CreateStdoutUI(output, false, true, true, false, false) + ui := CreateStdoutUI(output, false, true, true, false, false, false) ui.SetIgnoreDirPaths([]string{"/xxx"}) err := ui.AnalyzePath("test_dir", nil) @@ -129,7 +129,7 @@ func TestAnalyzePathWithProgress(t *testing.T) { func TestShowDevices(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) - ui := CreateStdoutUI(output, false, true, false, false, false) + ui := CreateStdoutUI(output, false, true, false, false, false, false) err := ui.ListDevices(getDevicesInfoMock()) assert.Nil(t, err) @@ -140,7 +140,7 @@ func TestShowDevices(t *testing.T) { func TestShowDevicesWithColor(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) - ui := CreateStdoutUI(output, true, true, true, false, false) + ui := CreateStdoutUI(output, true, true, true, false, false, false) err := ui.ListDevices(getDevicesInfoMock()) assert.Nil(t, err) @@ -154,7 +154,7 @@ func TestReadAnalysisWithColor(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) - ui := CreateStdoutUI(output, true, true, true, false, false) + ui := CreateStdoutUI(output, true, true, true, false, false, false) err = ui.ReadAnalysis(input) assert.Nil(t, err) @@ -167,7 +167,7 @@ func TestReadAnalysisBw(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) - ui := CreateStdoutUI(output, false, false, false, false, false) + ui := CreateStdoutUI(output, false, false, false, false, false, false) err = ui.ReadAnalysis(input) assert.Nil(t, err) @@ -180,7 +180,7 @@ func TestReadAnalysisWithWrongFile(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) - ui := CreateStdoutUI(output, true, true, true, false, false) + ui := CreateStdoutUI(output, true, true, true, false, false, false) err = ui.ReadAnalysis(input) assert.NotNil(t, err) @@ -194,7 +194,7 @@ func TestMaxInt(t *testing.T) { func TestFormatSize(t *testing.T) { output := bytes.NewBuffer(make([]byte, 10)) - ui := CreateStdoutUI(output, true, true, true, false, false) + ui := CreateStdoutUI(output, true, true, true, false, false, false) assert.Contains(t, ui.formatSize(1), "B") assert.Contains(t, ui.formatSize(1<<10+1), "KiB") @@ -205,6 +205,20 @@ func TestFormatSize(t *testing.T) { assert.Contains(t, ui.formatSize(1<<60+1), "EiB") } +func TestFormatSizeDec(t *testing.T) { + output := bytes.NewBuffer(make([]byte, 10)) + + ui := CreateStdoutUI(output, true, true, true, false, false, true) + + assert.Contains(t, ui.formatSize(1), "B") + assert.Contains(t, ui.formatSize(1<<10+1), "kB") + assert.Contains(t, ui.formatSize(1<<20+1), "MB") + assert.Contains(t, ui.formatSize(1<<30+1), "GB") + assert.Contains(t, ui.formatSize(1<<40+1), "TB") + assert.Contains(t, ui.formatSize(1<<50+1), "PB") + assert.Contains(t, ui.formatSize(1<<60+1), "EB") +} + // func printBuffer(buff *bytes.Buffer) { // for i, x := range buff.String() { // println(i, string(x)) diff --git a/tui/actions_linux_test.go b/tui/actions_linux_test.go index 9021281..7cc777c 100644 --- a/tui/actions_linux_test.go +++ b/tui/actions_linux_test.go @@ -18,7 +18,7 @@ func TestShowDevicesWithError(t *testing.T) { getter := device.LinuxDevicesInfoGetter{MountsPath: "/xyzxyz"} - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false) err := ui.ListDevices(getter) assert.Contains(t, err.Error(), "no such file") diff --git a/tui/actions_test.go b/tui/actions_test.go index f6a006a..57f5ddb 100644 --- a/tui/actions_test.go +++ b/tui/actions_test.go @@ -18,7 +18,7 @@ func TestShowDevices(t *testing.T) { app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50) defer simScreen.Fini() - ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false) err := ui.ListDevices(getDevicesInfoMock()) assert.Nil(t, err) @@ -38,7 +38,7 @@ func TestShowDevicesBW(t *testing.T) { app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50) defer simScreen.Fini() - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false) err := ui.ListDevices(getDevicesInfoMock()) assert.Nil(t, err) @@ -59,7 +59,7 @@ func TestDeviceSelected(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(false) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false) ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.done = make(chan struct{}) ui.SetIgnoreDirPaths([]string{"/xxx"}) @@ -109,7 +109,7 @@ func TestAnalyzePathWithParentDir(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, true) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, true, false) ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.topDir = parentDir ui.done = make(chan struct{}) @@ -138,7 +138,7 @@ func TestReadAnalysis(t *testing.T) { assert.Nil(t, err) app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false) ui.done = make(chan struct{}) err = ui.ReadAnalysis(input) @@ -161,7 +161,7 @@ func TestReadAnalysisWithWrongFile(t *testing.T) { assert.Nil(t, err) app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false) ui.done = make(chan struct{}) err = ui.ReadAnalysis(input) @@ -181,7 +181,7 @@ func TestViewDirContents(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false) ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.done = make(chan struct{}) err := ui.AnalyzePath("test_dir", nil) @@ -204,7 +204,7 @@ func TestViewFileWithoutCurrentDir(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false) ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.done = make(chan struct{}) @@ -217,7 +217,7 @@ func TestViewContentsOfNotExistingFile(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false) ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.done = make(chan struct{}) err := ui.AnalyzePath("test_dir", nil) @@ -247,7 +247,7 @@ func TestViewFile(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false) ui.done = make(chan struct{}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -278,7 +278,7 @@ func TestShowInfo(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false) ui.done = make(chan struct{}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -308,7 +308,7 @@ func TestShowInfoBW(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, false, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, false, false, false) ui.done = make(chan struct{}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -338,7 +338,7 @@ func TestShowInfoWithHardlinks(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false) ui.done = make(chan struct{}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -377,7 +377,7 @@ func TestShowInfoWithoutCurrentDir(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false) ui.done = make(chan struct{}) // pressing `i` will do nothing @@ -392,7 +392,7 @@ func TestExitViewFile(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false) ui.done = make(chan struct{}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) diff --git a/tui/filter_test.go b/tui/filter_test.go index 10a0143..845fc18 100644 --- a/tui/filter_test.go +++ b/tui/filter_test.go @@ -17,7 +17,7 @@ func TestFiltering(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(false) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false) ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.done = make(chan struct{}) err := ui.AnalyzePath("test_dir", nil) @@ -51,7 +51,7 @@ func TestFilteringWithoutCurrentDir(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(false) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false) ui.Analyzer = &testanalyze.MockedAnalyzer{} ui.done = make(chan struct{}) @@ -67,7 +67,7 @@ func TestSwitchToTable(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(false) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false) ui.done = make(chan struct{}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) @@ -110,7 +110,7 @@ func TestExitFiltering(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(false) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false) ui.done = make(chan struct{}) err := ui.AnalyzePath("test_dir", nil) assert.Nil(t, err) diff --git a/tui/format.go b/tui/format.go index ce51737..724e814 100644 --- a/tui/format.go +++ b/tui/format.go @@ -81,9 +81,32 @@ func (ui *UI) formatSize(size int64, reverseColor bool, transparentBg bool) stri } } - fsize := float64(size) + if ui.UseSIPrefix { + return formatWithDecPrefix(size, color) + } + return formatWithBinPrefix(float64(size), color) +} + +func (ui *UI) formatCount(count int) string { + row := "" + color := "[-::]" + count64 := int64(count) switch { + case count64 >= common.G: + row += fmt.Sprintf("%.1f%sG", float64(count)/float64(common.G), color) + case count64 >= common.M: + row += fmt.Sprintf("%.1f%sM", float64(count)/float64(common.M), color) + case count64 >= common.K: + row += fmt.Sprintf("%.1f%sk", float64(count)/float64(common.K), color) + default: + row += fmt.Sprintf("%d%s", count, color) + } + return row +} + +func formatWithBinPrefix(fsize float64, color string) string { + switch { case fsize >= common.Ei: return fmt.Sprintf("%.1f%s EiB", fsize/common.Ei, color) case fsize >= common.Pi: @@ -97,23 +120,26 @@ func (ui *UI) formatSize(size int64, reverseColor bool, transparentBg bool) stri case fsize >= common.Ki: return fmt.Sprintf("%.1f%s KiB", fsize/common.Ki, color) default: - return fmt.Sprintf("%d%s B", size, color) + return fmt.Sprintf("%d%s B", int64(fsize), color) } } -func (ui *UI) formatCount(count int) string { - row := "" - color := "[-::]" - +func formatWithDecPrefix(size int64, color string) string { + fsize := float64(size) switch { - 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) + 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) default: - row += fmt.Sprintf("%d%s", count, color) + return fmt.Sprintf("%d%s B", size, color) } - return row } diff --git a/tui/format_test.go b/tui/format_test.go index 67ab79f..e1af043 100644 --- a/tui/format_test.go +++ b/tui/format_test.go @@ -14,7 +14,7 @@ func TestFormatSize(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false) assert.Equal(t, "1[white:black:-] B", ui.formatSize(1, false, false)) assert.Equal(t, "1.0[white:black:-] KiB", ui.formatSize(1<<10, false, false)) @@ -25,12 +25,28 @@ func TestFormatSize(t *testing.T) { assert.Equal(t, "1.0[white:black:-] EiB", ui.formatSize(1<<60, false, false)) } +func TestFormatSizeDec(t *testing.T) { + simScreen := testapp.CreateSimScreen(50, 50) + defer simScreen.Fini() + + app := testapp.CreateMockedApp(true) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, true) + + assert.Equal(t, "1[white:black:-] B", ui.formatSize(1, false, false)) + assert.Equal(t, "1.0[white:black:-] kB", ui.formatSize(1<<10, false, false)) + assert.Equal(t, "1.0[white:black:-] MB", ui.formatSize(1<<20, false, false)) + assert.Equal(t, "1.1[white:black:-] GB", ui.formatSize(1<<30, false, false)) + assert.Equal(t, "1.1[white:black:-] TB", ui.formatSize(1<<40, false, false)) + assert.Equal(t, "1.1[white:black:-] PB", ui.formatSize(1<<50, false, false)) + assert.Equal(t, "1.2[white:black:-] EB", ui.formatSize(1<<60, false, false)) +} + func TestFormatCount(t *testing.T) { simScreen := testapp.CreateSimScreen(50, 50) defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false) assert.Equal(t, "1[-::]", ui.formatCount(1)) assert.Equal(t, "1.0[-::]k", ui.formatCount(1<<10)) @@ -43,7 +59,7 @@ func TestEscapeName(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(true) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false) dir := &analyze.Dir{ File: &analyze.File{ diff --git a/tui/keys_test.go b/tui/keys_test.go index f686855..281f0a7 100644 --- a/tui/keys_test.go +++ b/tui/keys_test.go @@ -20,7 +20,7 @@ func TestShowHelp(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(false) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false) ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, '?', 0)) @@ -32,7 +32,7 @@ func TestCloseHelp(t *testing.T) { defer simScreen.Fini() app := testapp.CreateMockedApp(false) - ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false) + ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false) ui.showHelp() assert.True(t, ui.pages.HasPage("help")) @@ -47,7 +47,7 @@ func TestKeyWhileDeleting(t *testing.T) { defer simScreen.Fini() |