Skip to content

Commit

Permalink
feat: mark multiple items for deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
dundee committed Dec 26, 2022
1 parent 133f991 commit ab0730a
Show file tree
Hide file tree
Showing 10 changed files with 512 additions and 16 deletions.
8 changes: 8 additions & 0 deletions tui/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ func (ui *UI) ReadAnalysis(input io.Reader) error {
return nil
}

func (ui *UI) delete(shouldEmpty bool) {
if len(ui.markedRows) > 0 {
ui.deleteMarked(shouldEmpty)
} else {
ui.deleteSelected(shouldEmpty)
}
}

func (ui *UI) deleteSelected(shouldEmpty bool) {
row, column := ui.table.GetSelection()
selectedItem := ui.table.GetCell(row, column).GetReference().(fs.Item)
Expand Down
19 changes: 14 additions & 5 deletions tui/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/rivo/tview"
)

func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64) string {
func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64, marked bool) string {
var part int

if ui.ShowApparentSize {
Expand All @@ -20,7 +20,7 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64) string

row := string(item.GetFlag())

if ui.UseColors {
if ui.UseColors && !marked {
row += "[#e67100::b]"
} else {
row += "[::b]"
Expand All @@ -35,7 +35,7 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64) string
row += getUsageGraph(part)

if ui.showItemCount {
if ui.UseColors {
if ui.UseColors && !marked {
row += "[#e67100::b]"
} else {
row += "[::b]"
Expand All @@ -44,7 +44,7 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64) string
}

if ui.showMtime {
if ui.UseColors {
if ui.UseColors && !marked {
row += "[#e67100::b]"
} else {
row += "[::b]"
Expand All @@ -55,8 +55,17 @@ func (ui *UI) formatFileRow(item fs.Item, maxUsage int64, maxSize int64) string
)
}

if len(ui.markedRows) > 0 {
if marked {
row += string('✓')
} else {
row += " "
}
row += " "
}

if item.IsDir() {
if ui.UseColors {
if ui.UseColors && !marked {
row += "[#3498db::b]/"
} else {
row += "[::b]/"
Expand Down
26 changes: 25 additions & 1 deletion tui/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,29 @@ func TestEscapeName(t *testing.T) {
Usage: 10,
}

assert.Contains(t, ui.formatFileRow(file, file.GetUsage(), file.GetSize()), "Aaa [red[] bbb")
assert.Contains(t, ui.formatFileRow(file, file.GetUsage(), file.GetSize(), false), "Aaa [red[] bbb")
}

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

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false, false)
ui.markedRows[0] = struct{}{}

dir := &analyze.Dir{
File: &analyze.File{
Usage: 10,
},
}

file := &analyze.File{
Name: "Aaa",
Parent: dir,
Usage: 10,
}

assert.Contains(t, ui.formatFileRow(file, file.GetUsage(), file.GetSize(), true), "✓ Aaa")
assert.Contains(t, ui.formatFileRow(file, file.GetUsage(), file.GetSize(), false), "[##########] Aaa")
}
18 changes: 17 additions & 1 deletion tui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ func (ui *UI) handleMainActions(key *tcell.EventKey) *tcell.EventKey {
case '/':
ui.showFilterInput()
return nil
case ' ':
ui.handleMark()
}
return key
}
Expand Down Expand Up @@ -233,6 +235,20 @@ func (ui *UI) handleDelete(shouldEmpty bool) {
if ui.askBeforeDelete {
ui.confirmDeletion(shouldEmpty)
} else {
ui.deleteSelected(shouldEmpty)
ui.delete(shouldEmpty)
}
}

func (ui *UI) handleMark() {
if ui.currentDir == nil {
return
}
// do not allow deleting parent dir
row, column := ui.table.GetSelection()
selectedFile, ok := ui.table.GetCell(row, column).GetReference().(fs.Item)
if !ok || selectedFile == ui.currentDir.GetParent() {
return
}

ui.fileItemMarked(row)
}
162 changes: 162 additions & 0 deletions tui/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,18 @@ func TestDeleteEmpty(t *testing.T) {
assert.NotNil(t, key)
}

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

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

key := ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, ' ', 0))
assert.NotNil(t, key)
}

func TestDelete(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
Expand Down Expand Up @@ -380,6 +392,44 @@ func TestDelete(t *testing.T) {
assert.NoDirExists(t, "test_dir/nested")
}

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

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false, false)
ui.done = make(chan struct{})
ui.askBeforeDelete = false
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.keyPressed(tcell.NewEventKey(tcell.KeyRune, ' ', 0))

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

<-ui.done

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

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

func TestDeleteParent(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
Expand Down Expand Up @@ -410,6 +460,36 @@ func TestDeleteParent(t *testing.T) {
assert.DirExists(t, "test_dir/nested")
}

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

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false, false)
ui.done = make(chan struct{})
ui.askBeforeDelete = false
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.keyPressed(tcell.NewEventKey(tcell.KeyRight, 'l', 0))
ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, ' ', 0))

assert.Equal(t, len(ui.markedRows), 0)
}

func TestEmptyDir(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
Expand Down Expand Up @@ -447,6 +527,45 @@ func TestEmptyDir(t *testing.T) {
assert.NoDirExists(t, "test_dir/nested/subnested")
}

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

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false, false)
ui.done = make(chan struct{})
ui.askBeforeDelete = false
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.keyPressed(tcell.NewEventKey(tcell.KeyRune, ' ', 0))

ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'e', 0))

<-ui.done

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

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

func TestEmptyFile(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
Expand Down Expand Up @@ -488,6 +607,49 @@ func TestEmptyFile(t *testing.T) {
assert.DirExists(t, "test_dir/nested/subnested")
}

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

app := testapp.CreateMockedApp(true)
ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false, false)
ui.done = make(chan struct{})
ui.askBeforeDelete = false
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.keyPressed(tcell.NewEventKey(tcell.KeyRight, 'l', 0)) // into nested

ui.table.Select(2, 0) // file2

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

ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'e', 0))

<-ui.done

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

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

func TestSortByApparentSize(t *testing.T) {
simScreen := testapp.CreateSimScreen(50, 50)
defer simScreen.Fini()
Expand Down
Loading

0 comments on commit ab0730a

Please sign in to comment.