From 97f5c24dc3459e81314863f5356c87758ee08262 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Wed, 30 Dec 2020 23:08:17 +0100 Subject: [PATCH] added posibility to ignore some paths --- README.md | 1 + cli.go | 39 +++++++++++++++++++++++---------------- cli_test.go | 30 ++++++++++++++++++++++++++++++ dir_test.go | 7 +++++++ main.go | 3 +++ 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 6265be3c8..dff151943 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Arch Linux: gdu # show all mounted disks gdu some_dir_to_analyze # analyze given dir gdu -log-file=./gdu.log some_dir # write errors to log file + gdu -ignore=/sys,/proc / # ignore some paths ## Running tests diff --git a/cli.go b/cli.go index 7f1d2b74c..49323454d 100644 --- a/cli.go +++ b/cli.go @@ -34,6 +34,7 @@ type UI struct { topDirPath string currentDirPath string askBeforeDelete bool + ignorePaths []string } // CreateUI creates the whole UI app @@ -136,7 +137,28 @@ func (ui *UI) AnalyzePath(path string) { }() } -// showDir shows content of the selected dir +// StartUILoop starts tview application +func (ui *UI) StartUILoop() { + if err := ui.app.Run(); err != nil { + panic(err) + } +} + +// SetIgnorePaths sets paths to ignore +func (ui *UI) SetIgnorePaths(paths []string) { + ui.ignorePaths = paths +} + +// ShouldBeIgnored returns true if given path should be ignored +func (ui *UI) ShouldBeIgnored(path string) bool { + for _, ignorePath := range ui.ignorePaths { + if strings.HasPrefix(path, ignorePath) { + return true + } + } + return false +} + func (ui *UI) showDir() { ui.currentDirPath = ui.currentDir.path ui.currentDirLabel.SetText("--- " + ui.currentDirPath + " ---") @@ -168,21 +190,6 @@ func (ui *UI) showDir() { ui.app.SetFocus(ui.table) } -// StartUILoop starts tview application -func (ui *UI) StartUILoop() { - if err := ui.app.Run(); err != nil { - panic(err) - } -} - -// ShouldBeIgnored returns true if given path should be ignored -func (ui *UI) ShouldBeIgnored(path string) bool { - if strings.HasPrefix(path, "/proc") || strings.HasPrefix(path, "/dev") || strings.HasPrefix(path, "/sys") || strings.HasPrefix(path, "/run") { - return true - } - return false -} - func (ui *UI) fileItemSelected(row, column int) { selectedDir := ui.table.GetCell(row, column).GetReference().(*File) if !selectedDir.isDir { diff --git a/cli_test.go b/cli_test.go index 7fc99ef5a..db3f99d6b 100644 --- a/cli_test.go +++ b/cli_test.go @@ -1,6 +1,7 @@ package main import ( + "path/filepath" "runtime" "sync" "testing" @@ -216,3 +217,32 @@ func TestKeys(t *testing.T) { assert.NoFileExists(t, "test_dir/nested/subnested/file") } + +func TestSetIgnorePaths(t *testing.T) { + fin := CreateTestDir() + defer fin() + + simScreen := tcell.NewSimulationScreen("UTF-8") + simScreen.Init() + simScreen.SetSize(50, 50) + + ui := CreateUI(simScreen) + + path, _ := filepath.Abs("test_dir/nested/subnested") + ui.SetIgnorePaths([]string{path}) + + ui.AnalyzePath("test_dir") + + go func() { + time.Sleep(100 * time.Millisecond) + simScreen.InjectKey(tcell.KeyRune, 'q', 1) + time.Sleep(100 * time.Millisecond) + }() + + ui.StartUILoop() + + dir := ui.currentDir + + assert.Equal(t, 3, dir.itemCount) + +} diff --git a/dir_test.go b/dir_test.go index 8b6ff3576..b2d1dcbc8 100644 --- a/dir_test.go +++ b/dir_test.go @@ -44,3 +44,10 @@ func TestProcessDir(t *testing.T) { // test parent link assert.Equal(t, "test_dir", dir.files[0].files[1].files[0].parent.parent.parent.name) } + +func TestIgnoreDir(t *testing.T) { + dir := ProcessDir("/proc", &CurrentProgress{mutex: &sync.Mutex{}}, func(_ string) bool { return true }) + + assert.Equal(t, "proc", dir.name) + assert.Equal(t, 1, dir.itemCount) +} diff --git a/main.go b/main.go index ba282d260..7ff77faeb 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "log" "os" "runtime" + "strings" "github.com/gdamore/tcell/v2" ) @@ -15,6 +16,7 @@ var AppVersion = "development" func main() { logFile := flag.String("log-file", "/dev/null", "Path to a logfile") + ignorePath := flag.String("ignore", "/proc,/dev,/sys,/run", "Absolute paths to ignore (separated by comma)") showVersion := flag.Bool("v", false, "Prints version") flag.Parse() @@ -38,6 +40,7 @@ func main() { screen.Init() ui := CreateUI(screen) + ui.SetIgnorePaths(strings.Split(*ignorePath, ",")) args := flag.Args()