Skip to content

Commit

Permalink
added posibility to ignore some paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Milde committed Dec 30, 2020
1 parent 9922b91 commit 97f5c24
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 23 additions & 16 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type UI struct {
topDirPath string
currentDirPath string
askBeforeDelete bool
ignorePaths []string
}

// CreateUI creates the whole UI app
Expand Down Expand Up @@ -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 + " ---")
Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"path/filepath"
"runtime"
"sync"
"testing"
Expand Down Expand Up @@ -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)

}
7 changes: 7 additions & 0 deletions dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"runtime"
"strings"

"github.com/gdamore/tcell/v2"
)
Expand All @@ -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()

Expand All @@ -38,6 +40,7 @@ func main() {
screen.Init()

ui := CreateUI(screen)
ui.SetIgnorePaths(strings.Split(*ignorePath, ","))

args := flag.Args()

Expand Down

0 comments on commit 97f5c24

Please sign in to comment.