fix(tui): restore filters on cancel
This commit is contained in:
parent
4db82d63ac
commit
483fd779fb
10
tui/tui.go
10
tui/tui.go
@ -429,6 +429,7 @@ type model struct {
|
||||
width int
|
||||
height int
|
||||
query string
|
||||
savedQuery string
|
||||
filterMode bool
|
||||
focus paneFocus
|
||||
contextOffset int
|
||||
@ -650,7 +651,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch typed.String() {
|
||||
case "ctrl+c", "ctrl+d", "q":
|
||||
return m, tea.Quit
|
||||
case "enter", "esc":
|
||||
case "enter":
|
||||
m.filterMode = false
|
||||
case "esc":
|
||||
if m.query != m.savedQuery {
|
||||
m.query = m.savedQuery
|
||||
m.applyFilter()
|
||||
}
|
||||
m.filterMode = false
|
||||
case "backspace":
|
||||
if len(m.query) > 0 {
|
||||
@ -1149,6 +1156,7 @@ func (m *model) runMenuAction(action menuAction) tea.Cmd {
|
||||
|
||||
func (m *model) startFilter() {
|
||||
m.closeMenu()
|
||||
m.savedQuery = m.query
|
||||
m.filterMode = true
|
||||
}
|
||||
|
||||
|
||||
@ -313,6 +313,34 @@ func TestQQuitsFromMenuAndFilterModes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterEscRestoresPreviousQuery(t *testing.T) {
|
||||
m := newModel(Options{
|
||||
Title: "archive",
|
||||
Items: []Item{
|
||||
{Title: "alpha"},
|
||||
{Title: "beta"},
|
||||
{Title: "alphabet"},
|
||||
},
|
||||
})
|
||||
m.query = "alpha"
|
||||
m.applyFilter()
|
||||
if len(m.filtered) != 2 {
|
||||
t.Fatalf("initial filtered rows = %d, want 2", len(m.filtered))
|
||||
}
|
||||
updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'/'}})
|
||||
m = updated.(model)
|
||||
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'b'}})
|
||||
m = updated.(model)
|
||||
if m.query != "alphab" || len(m.filtered) != 1 {
|
||||
t.Fatalf("draft query/filter = %q/%d", m.query, len(m.filtered))
|
||||
}
|
||||
updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEsc})
|
||||
m = updated.(model)
|
||||
if m.filterMode || m.query != "alpha" || len(m.filtered) != 2 {
|
||||
t.Fatalf("esc should restore query/filter, mode=%v query=%q filtered=%d", m.filterMode, m.query, len(m.filtered))
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitialTerminalSizeCanUseTallPane(t *testing.T) {
|
||||
m := newModel(Options{Title: "archive", Items: []Item{{Title: "alpha"}}})
|
||||
m.width = 84
|
||||
|
||||
Loading…
Reference in New Issue
Block a user