Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --no-delete option, resolves #224 #333

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/gdu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
NoProgress bool `yaml:"no-progress"`
NoCross bool `yaml:"no-cross"`
NoHidden bool `yaml:"no-hidden"`
NoDelete bool `yaml:"no-delete"`
FollowSymlinks bool `yaml:"follow-symlinks"`
Profiling bool `yaml:"profiling"`
ConstGC bool `yaml:"const-gc"`
Expand Down Expand Up @@ -274,6 +275,11 @@
ui.SetShowItemCount()
})
}
if a.Flags.NoDelete {
opts = append(opts, func(ui *tui.UI) {
ui.SetNoDelete()
})

Check warning on line 281 in cmd/gdu/app/app.go

View check run for this annotation

Codecov / codecov/patch

cmd/gdu/app/app.go#L279-L281

Added lines #L279 - L281 were not covered by tests
}

ui = tui.CreateUI(
a.TermApp,
Expand Down
1 change: 1 addition & 0 deletions cmd/gdu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func init() {
flags.BoolVar(&af.UseSIPrefix, "si", false, "Show sizes with decimal SI prefixes (kB, MB, GB) instead of binary prefixes (KiB, MiB, GiB)")
flags.BoolVar(&af.NoPrefix, "no-prefix", false, "Show sizes as raw numbers without any prefixes (SI or binary) in non-interactive mode")
flags.BoolVar(&af.NoMouse, "no-mouse", false, "Do not use mouse")
flags.BoolVar(&af.NoDelete, "no-delete", false, "Do not allow deletions")
flags.BoolVar(&af.WriteConfig, "write-config", false, "Write current configuration to file (default is $HOME/.gdu.yaml)")

initConfig()
Expand Down
30 changes: 30 additions & 0 deletions tui/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,36 @@ func TestDelete(t *testing.T) {
assert.NoDirExists(t, "test_dir/nested")
}

func TestDeleteWithNoDelete(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
simScreen := testapp.CreateSimScreen()
defer simScreen.Fini()

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false, false)
ui.done = make(chan struct{})
err := ui.AnalyzePath("test_dir", nil)
assert.Nil(t, err)

<-ui.done // wait for analyzer

for _, f := range ui.app.(*testapp.MockedApp).GetUpdateDraws() {
f()
}

assert.Equal(t, "test_dir", ui.currentDir.GetName())

assert.Equal(t, 1, ui.table.GetRowCount())

ui.table.Select(0, 0)

ui.SetNoDelete()
ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'd', 0))

assert.DirExists(t, "test_dir/nested")
}

func TestDeleteMarked(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
Expand Down
36 changes: 24 additions & 12 deletions tui/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,10 @@
text.SetTitle(" gdu help ")
text.SetScrollable(true)

if ui.UseColors {
text.SetText(
strings.ReplaceAll(
strings.ReplaceAll(helpText, "[::b]", "[red]"),
"[white:black:-]",
"[white]",
),
)
} else {
text.SetText(helpText)
}
formattedHelpText := ui.formatHelpTextFor()
text.SetText(formattedHelpText)

maxHeight := strings.Count(helpText, "\n") + 7
maxHeight := strings.Count(formattedHelpText, "\n") + 7
_, height := ui.screen.Size()
if height > maxHeight {
height = maxHeight
Expand All @@ -280,3 +271,24 @@
ui.pages.AddPage("help", flex, true, true)
ui.app.SetFocus(text)
}

func (ui *UI) formatHelpTextFor() string {
lines := strings.Split(helpText, "\n")

for i, line := range lines {
if ui.UseColors {
lines[i] = strings.ReplaceAll(
strings.ReplaceAll(line, "[::b]", "[red]"),
"[white:black:-]",
"[white]",
)
}

if ui.noDelete && (strings.Contains(line, "Empty file or directory") ||
strings.Contains(line, "Delete file or directory")) {
lines[i] += " (disabled)"
}

Check warning on line 290 in tui/show.go

View check run for this annotation

Codecov / codecov/patch

tui/show.go#L289-L290

Added lines #L289 - L290 were not covered by tests
}

return strings.Join(lines, "\n")
}
24 changes: 24 additions & 0 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"io"
"time"

"golang.org/x/exp/slices"

Expand Down Expand Up @@ -57,6 +58,7 @@
defaultSortOrder string
markedRows map[int]struct{}
exportName string
noDelete bool
}

// Option is optional function customizing the bahaviour of UI
Expand Down Expand Up @@ -99,6 +101,7 @@
defaultSortOrder: "desc",
markedRows: make(map[int]struct{}),
exportName: "export.json",
noDelete: false,
}
for _, o := range opts {
o(ui)
Expand Down Expand Up @@ -205,6 +208,10 @@
ui.showItemCount = true
}

func (ui *UI) SetNoDelete() {
ui.noDelete = true
}

func (ui *UI) resetSorting() {
ui.sortBy = ui.defaultSortBy
ui.sortOrder = ui.defaultSortOrder
Expand Down Expand Up @@ -280,6 +287,23 @@
}

func (ui *UI) confirmDeletion(shouldEmpty bool) {
if ui.noDelete {
previousHeaderText := ui.header.GetText(false)

// show feedback to user
ui.header.SetText(" Deletion is disabled!")

go func() {
time.Sleep(2 * time.Second)
ui.app.QueueUpdateDraw(func() {
ui.header.Clear()
ui.header.SetText(previousHeaderText)
})

Check warning on line 301 in tui/tui.go

View check run for this annotation

Codecov / codecov/patch

tui/tui.go#L299-L301

Added lines #L299 - L301 were not covered by tests
}()

return
}

if len(ui.markedRows) > 0 {
ui.confirmDeletionMarked(shouldEmpty)
} else {
Expand Down
12 changes: 12 additions & 0 deletions tui/tui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,18 @@ func TestSetShowItemCount(t *testing.T) {
assert.Equal(t, ui.showItemCount, true)
}

func TestNoDelete(t *testing.T) {
simScreen := testapp.CreateSimScreen()
defer simScreen.Fini()

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false, false)

ui.SetNoDelete()

assert.Equal(t, ui.noDelete, true)
}

// nolint: deadcode,unused // Why: for debugging
func printScreen(simScreen tcell.SimulationScreen) {
b, _, _ := simScreen.GetContents()
Expand Down
Loading