diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | cmd/gdu/app/app.go | 2 | ||||
-rw-r--r-- | cmd/gdu/main.go | 2 | ||||
-rw-r--r-- | gdu.1.md | 4 | ||||
-rw-r--r-- | stdout/stdout.go | 69 | ||||
-rw-r--r-- | stdout/stdout_linux_test.go | 3 | ||||
-rw-r--r-- | stdout/stdout_test.go | 39 |
7 files changed, 80 insertions, 41 deletions
@@ -86,6 +86,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 + -s, --summarize Show only a total in non-interactive mode -v, --version Print version ``` @@ -102,6 +103,7 @@ Flags: gdu -n / # only print stats, do not start interactive mode gdu -np / # do not show progress, useful when using its output in a script + gdu -nps /some/dir # show only total usage for given dir gdu / > file # write stats to file, do not start interactive mode gdu -o- / | gzip -c >report.json.gz # write all info to JSON file for later analysis diff --git a/cmd/gdu/app/app.go b/cmd/gdu/app/app.go index 3949d95..ed527b6 100644 --- a/cmd/gdu/app/app.go +++ b/cmd/gdu/app/app.go @@ -48,6 +48,7 @@ type Flags struct { NoProgress bool NoCross bool NoHidden bool + Summarize bool } // App defines the main application @@ -168,6 +169,7 @@ func (a *App) createUI() (UI, error) { !a.Flags.NoColor && a.Istty, !a.Flags.NoProgress && a.Istty, a.Flags.ShowApparentSize, + a.Flags.Summarize, ) } else { ui = tui.CreateUI( diff --git a/cmd/gdu/main.go b/cmd/gdu/main.go index 1dc6c10..0b2805e 100644 --- a/cmd/gdu/main.go +++ b/cmd/gdu/main.go @@ -47,7 +47,7 @@ func init() { flags.BoolVarP(&af.NoColor, "no-color", "c", false, "Do not use colorized output") 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") } func runE(command *cobra.Command, args []string) error { @@ -40,10 +40,12 @@ ignore (separated by comma) **-H**, **\--no-hidden**\[=false\] Ignore hidden directories (beginning with dot) +**-n**, **\--non-interactive**\[=false\] Do not run in interactive mode + **-p**, **\--no-progress**\[=false\] Do not show progress in non-interactive mode -**-n**, **\--non-interactive**\[=false\] Do not run in interactive mode +**-s**, **\--summarize**\[=false\] Show only a total in non-interactive mode **-d**, **\--show-disks**\[=false\] Show all mounted disks 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") |