-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #80
- Loading branch information
Showing
6 changed files
with
227 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package tui | ||
|
||
import ( | ||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
func (ui *UI) hideFilterInput() { | ||
ui.filterValue = "" | ||
ui.footer.Clear() | ||
ui.footer.AddItem(ui.footerLabel, 0, 1, false) | ||
ui.app.SetFocus(ui.table) | ||
ui.filteringInput = nil | ||
ui.filtering = false | ||
} | ||
|
||
func (ui *UI) showFilterInput() { | ||
if ui.filteringInput == nil { | ||
ui.filteringInput = tview.NewInputField() | ||
|
||
if !ui.UseColors { | ||
ui.filteringInput.SetFieldBackgroundColor( | ||
tcell.NewRGBColor(100, 100, 100), | ||
) | ||
ui.filteringInput.SetFieldTextColor( | ||
tcell.NewRGBColor(255, 255, 255), | ||
) | ||
} | ||
|
||
ui.filteringInput.SetChangedFunc(func(text string) { | ||
ui.filterValue = text | ||
ui.showDir() | ||
}) | ||
ui.filteringInput.SetDoneFunc(func(key tcell.Key) { | ||
if key == tcell.KeyESC { | ||
ui.hideFilterInput() | ||
ui.showDir() | ||
} else { | ||
ui.app.SetFocus(ui.table) | ||
ui.filtering = false | ||
} | ||
}) | ||
|
||
ui.footer.Clear() | ||
ui.footer.AddItem(ui.filteringInput, 0, 1, true) | ||
ui.footer.AddItem(ui.footerLabel, 0, 5, false) | ||
} | ||
ui.app.SetFocus(ui.filteringInput) | ||
ui.filtering = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package tui | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dundee/gdu/v5/internal/testanalyze" | ||
"github.com/dundee/gdu/v5/internal/testapp" | ||
"github.com/dundee/gdu/v5/internal/testdir" | ||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFiltering(t *testing.T) { | ||
app := testapp.CreateMockedApp(false) | ||
ui := CreateUI(app, true, true) | ||
ui.Analyzer = &testanalyze.MockedAnalyzer{} | ||
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).UpdateDraws { | ||
f() | ||
} | ||
|
||
ui.showFilterInput() | ||
ui.filterValue = "" | ||
ui.showDir() | ||
|
||
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa") // nothing is filtered | ||
|
||
ui.filterValue = "cc" | ||
ui.showDir() | ||
|
||
assert.Contains(t, ui.table.GetCell(0, 0).Text, "ccc") // shows only cccc | ||
|
||
ui.hideFilterInput() | ||
ui.showDir() | ||
|
||
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa") // filtering reset | ||
} | ||
|
||
func TestSwitchToTable(t *testing.T) { | ||
fin := testdir.CreateTestDir() | ||
defer fin() | ||
|
||
app := testapp.CreateMockedApp(false) | ||
ui := CreateUI(app, false, true) | ||
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).UpdateDraws { | ||
f() | ||
} | ||
|
||
ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, '/', 0)) // open filtering input | ||
handler := ui.filteringInput.InputHandler() | ||
handler(tcell.NewEventKey(tcell.KeyRune, 'n', 0), func(p tview.Primitive) {}) | ||
handler(tcell.NewEventKey(tcell.KeyRune, 'e', 0), func(p tview.Primitive) {}) | ||
handler(tcell.NewEventKey(tcell.KeyRune, 's', 0), func(p tview.Primitive) {}) | ||
|
||
ui.table.Select(0, 0) | ||
ui.keyPressed(tcell.NewEventKey(tcell.KeyRight, 'l', 0)) // we are filtering, should do nothing | ||
|
||
assert.Contains(t, ui.table.GetCell(0, 0).Text, "nested") | ||
|
||
handler( | ||
tcell.NewEventKey(tcell.KeyTAB, ' ', 0), func(p tview.Primitive) {}, | ||
) // switch focus to table | ||
ui.keyPressed(tcell.NewEventKey(tcell.KeyTAB, ' ', 0)) // switch back to input | ||
handler( | ||
tcell.NewEventKey(tcell.KeyEnter, ' ', 0), func(p tview.Primitive) {}, | ||
) // switch back to table | ||
|
||
ui.keyPressed(tcell.NewEventKey(tcell.KeyRight, 'l', 0)) // open nested dir | ||
|
||
assert.Contains(t, ui.table.GetCell(1, 0).Text, "subnested") | ||
assert.Empty(t, ui.filterValue) // filtering reset | ||
} | ||
|
||
func TestExitFiltering(t *testing.T) { | ||
fin := testdir.CreateTestDir() | ||
defer fin() | ||
|
||
app := testapp.CreateMockedApp(false) | ||
ui := CreateUI(app, true, true) | ||
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).UpdateDraws { | ||
f() | ||
} | ||
|
||
ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, '/', 0)) // open filtering input | ||
handler := ui.filteringInput.InputHandler() | ||
ui.filterValue = "xxx" | ||
ui.showDir() | ||
|
||
assert.Equal(t, ui.table.GetCell(0, 0).Text, "") // nothing is filtered | ||
|
||
handler( | ||
tcell.NewEventKey(tcell.KeyEsc, ' ', 0), func(p tview.Primitive) {}, | ||
) // exit filtering | ||
|
||
assert.Contains(t, ui.table.GetCell(0, 0).Text, "nested") | ||
assert.Empty(t, ui.filterValue) // filtering reset | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.