summaryrefslogtreecommitdiffstats
path: root/stdout
diff options
context:
space:
mode:
authorDaniel Milde <daniel@milde.cz>2021-10-22 21:55:03 +0200
committerDaniel Milde <daniel@milde.cz>2021-10-22 21:55:19 +0200
commitba96a8a2e5572898f46f2534d9ce8f4b3cab1175 (patch)
tree9cae9aaf38b90d36c6a658c27a57cacccda9b013 /stdout
parent963996e470a11ccd5ff24cb7c9a53cfe34dbbfd8 (diff)
added option to show total usage for given path #96
Diffstat (limited to 'stdout')
-rw-r--r--stdout/stdout.go69
-rw-r--r--stdout/stdout_linux_test.go3
-rw-r--r--stdout/stdout_test.go39
3 files changed, 72 insertions, 39 deletions
diff --git a/stdout/stdout.go b/stdout/stdout.go
index 355097c..fcdafef 100644
--- a/stdout/stdout.go
+++ b/stdout/stdout.go
@@ -20,16 +20,23 @@ import (
// UI struct
type UI struct {
*common.UI
- output io.Writer
- red *color.Color
- orange *color.Color
- blue *color.Color
+ output io.Writer
+ red *color.Color
+ orange *color.Color
+ blue *color.Color
+ summarize bool
}
var progressRunes = []rune(`⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧`)
// CreateStdoutUI creates UI for stdout
-func CreateStdoutUI(output io.Writer, useColors bool, showProgress bool, showApparentSize bool) *UI {
+func CreateStdoutUI(
+ output io.Writer,
+ useColors bool,
+ showProgress bool,
+ showApparentSize bool,
+ summarize bool,
+) *UI {
ui := &UI{
UI: &common.UI{
UseColors: useColors,
@@ -37,7 +44,8 @@ func CreateStdoutUI(output io.Writer, useColors bool, showProgress bool, showApp
ShowApparentSize: showApparentSize,
Analyzer: analyze.CreateAnalyzer(),
},
- output: output,
+ output: output,
+ summarize: summarize,
}
ui.red = color.New(color.FgRed).Add(color.Bold)
@@ -138,7 +146,11 @@ func (ui *UI) AnalyzePath(path string, _ *analyze.Dir) error {
wait.Wait()
- ui.showDir(dir)
+ if ui.summarize {
+ ui.printItem(dir)
+ } else {
+ ui.showDir(dir)
+ }
return nil
}
@@ -146,6 +158,12 @@ func (ui *UI) AnalyzePath(path string, _ *analyze.Dir) error {
func (ui *UI) showDir(dir *analyze.Dir) {
sort.Sort(dir.Files)
+ for _, file := range dir.Files {
+ ui.printItem(file)
+ }
+}
+
+func (ui *UI) printItem(file analyze.Item) {
var lineFormat string
if ui.UseColors {
lineFormat = "%s %20s %s\n"
@@ -154,27 +172,24 @@ func (ui *UI) showDir(dir *analyze.Dir) {
}
var size int64
+ if ui.ShowApparentSize {
+ size = file.GetSize()
+ } else {
+ size = file.GetUsage()
+ }
- for _, file := range dir.Files {
- if ui.ShowApparentSize {
- size = file.GetSize()
- } else {
- size = file.GetUsage()
- }
-
- if file.IsDir() {
- fmt.Fprintf(ui.output,
- lineFormat,
- string(file.GetFlag()),
- ui.formatSize(size),
- ui.blue.Sprintf("/"+file.GetName()))
- } else {
- fmt.Fprintf(ui.output,
- lineFormat,
- string(file.GetFlag()),
- ui.formatSize(size),
- file.GetName())
- }
+ if file.IsDir() {
+ fmt.Fprintf(ui.output,
+ lineFormat,
+ string(file.GetFlag()),
+ ui.formatSize(size),
+ ui.blue.Sprintf("/"+file.GetName()))
+ } else {
+ fmt.Fprintf(ui.output,
+ lineFormat,
+ string(file.GetFlag()),
+ ui.formatSize(size),
+ file.GetName())
}
}
diff --git a/stdout/stdout_linux_test.go b/stdout/stdout_linux_test.go
index 3a287d2..dce7145 100644
--- a/stdout/stdout_linux_test.go
+++ b/stdout/stdout_linux_test.go
@@ -1,3 +1,4 @@
+//go:build linux
// +build linux
package stdout
@@ -20,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)
+ ui := CreateStdoutUI(output, false, true, 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 0c3f40d..4895d84 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)
+ ui := CreateStdoutUI(output, false, false, false, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir", nil)
assert.Nil(t, err)
@@ -35,6 +35,23 @@ func TestAnalyzePath(t *testing.T) {
assert.Contains(t, output.String(), "nested")
}
+func TestShowSummary(t *testing.T) {
+ fin := testdir.CreateTestDir()
+ defer fin()
+
+ buff := make([]byte, 10)
+ output := bytes.NewBuffer(buff)
+
+ ui := CreateStdoutUI(output, false, false, false, true)
+ ui.SetIgnoreDirPaths([]string{"/xxx"})
+ err := ui.AnalyzePath("test_dir", nil)
+ assert.Nil(t, err)
+ err = ui.StartUILoop()
+
+ assert.Nil(t, err)
+ assert.Contains(t, output.String(), "test_dir")
+}
+
func TestAnalyzeSubdir(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
@@ -42,7 +59,7 @@ func TestAnalyzeSubdir(t *testing.T) {
buff := make([]byte, 10)
output := bytes.NewBuffer(buff)
- ui := CreateStdoutUI(output, false, false, false)
+ ui := CreateStdoutUI(output, false, false, false, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir/nested", nil)
assert.Nil(t, err)
@@ -59,7 +76,7 @@ func TestAnalyzePathWithColors(t *testing.T) {
buff := make([]byte, 10)
output := bytes.NewBuffer(buff)
- ui := CreateStdoutUI(output, true, false, true)
+ ui := CreateStdoutUI(output, true, false, true, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir/nested", nil)
@@ -70,7 +87,7 @@ func TestAnalyzePathWithColors(t *testing.T) {
func TestItemRows(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
- ui := CreateStdoutUI(output, false, true, false)
+ ui := CreateStdoutUI(output, false, true, false, false)
ui.Analyzer = &testanalyze.MockedAnalyzer{}
err := ui.AnalyzePath("test_dir", nil)
@@ -86,7 +103,7 @@ func TestAnalyzePathWithProgress(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
- ui := CreateStdoutUI(output, false, true, true)
+ ui := CreateStdoutUI(output, false, true, true, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir", nil)
@@ -97,7 +114,7 @@ func TestAnalyzePathWithProgress(t *testing.T) {
func TestShowDevices(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
- ui := CreateStdoutUI(output, false, true, false)
+ ui := CreateStdoutUI(output, false, true, false, false)
err := ui.ListDevices(getDevicesInfoMock())
assert.Nil(t, err)
@@ -108,7 +125,7 @@ func TestShowDevices(t *testing.T) {
func TestShowDevicesWithColor(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
- ui := CreateStdoutUI(output, true, true, true)
+ ui := CreateStdoutUI(output, true, true, true, false)
err := ui.ListDevices(getDevicesInfoMock())
assert.Nil(t, err)
@@ -122,7 +139,7 @@ func TestReadAnalysisWithColor(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
- ui := CreateStdoutUI(output, true, true, true)
+ ui := CreateStdoutUI(output, true, true, true, false)
err = ui.ReadAnalysis(input)
assert.Nil(t, err)
@@ -135,7 +152,7 @@ func TestReadAnalysisBw(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
- ui := CreateStdoutUI(output, false, false, false)
+ ui := CreateStdoutUI(output, false, false, false, false)
err = ui.ReadAnalysis(input)
assert.Nil(t, err)
@@ -148,7 +165,7 @@ func TestReadAnalysisWithWrongFile(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
- ui := CreateStdoutUI(output, true, true, true)
+ ui := CreateStdoutUI(output, true, true, true, false)
err = ui.ReadAnalysis(input)
assert.NotNil(t, err)
@@ -162,7 +179,7 @@ func TestMaxInt(t *testing.T) {
func TestFormatSize(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
- ui := CreateStdoutUI(output, true, true, true)
+ ui := CreateStdoutUI(output, true, true, true, false)
assert.Contains(t, ui.formatSize(1), "B")
assert.Contains(t, ui.formatSize(1<<10+1), "KiB")