diff options
Diffstat (limited to 'stdout')
-rw-r--r-- | stdout/stdout.go | 30 | ||||
-rw-r--r-- | stdout/stdout_linux_test.go | 2 | ||||
-rw-r--r-- | stdout/stdout_test.go | 40 |
3 files changed, 58 insertions, 14 deletions
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)) |