Skip to content

Commit

Permalink
feat(filter): allow to focus out of filter (#776)
Browse files Browse the repository at this point in the history
- esc focus out of the filter
- esc with filter blurred quits
- g/G/j/k navigation when filter is blurred

closes #201
  • Loading branch information
caarlos0 authored Dec 13, 2024
1 parent 64d69eb commit f230a3d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
1 change: 0 additions & 1 deletion choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func defaultKeymap() keymap {
),
End: key.NewBinding(
key.WithKeys("G", "end"),
key.WithHelp("G", "end"),
),
ToggleAll: key.NewBinding(
key.WithKeys("a", "A", "ctrl+a"),
Expand Down
49 changes: 47 additions & 2 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func defaultKeymap() keymap {
Up: key.NewBinding(
key.WithKeys("up", "ctrl+k", "ctrl+p"),
),
NDown: key.NewBinding(
key.WithKeys("j"),
),
NUp: key.NewBinding(
key.WithKeys("k"),
),
Home: key.NewBinding(
key.WithKeys("g", "home"),
),
End: key.NewBinding(
key.WithKeys("G", "end"),
),
ToggleAndNext: key.NewBinding(
key.WithKeys("tab"),
key.WithHelp("tab", "toggle"),
Expand All @@ -50,6 +62,14 @@ func defaultKeymap() keymap {
key.WithHelp("ctrl+a", "select all"),
key.WithDisabled(),
),
FocusInSearch: key.NewBinding(
key.WithKeys("/"),
key.WithHelp("/", "search"),
),
FocusOutSearch: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "blur search"),
),
Quit: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "quit"),
Expand All @@ -66,8 +86,14 @@ func defaultKeymap() keymap {
}

type keymap struct {
FocusInSearch,
FocusOutSearch,
Down,
Up,
NDown,
NUp,
Home,
End,
ToggleAndNext,
ToggleAndPrevious,
ToggleAll,
Expand All @@ -87,6 +113,8 @@ func (k keymap) ShortHelp() []key.Binding {
key.WithKeys("up", "down"),
key.WithHelp("↓↑", "navigate"),
),
k.FocusInSearch,
k.FocusOutSearch,
k.ToggleAndNext,
k.ToggleAll,
k.Submit,
Expand Down Expand Up @@ -262,6 +290,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
km := m.keymap
switch {
case key.Matches(msg, km.FocusInSearch):
m.textinput.Focus()
case key.Matches(msg, km.FocusOutSearch):
m.textinput.Blur()
case key.Matches(msg, km.Quit):
m.quitting = true
return m, tea.Quit
Expand All @@ -272,10 +304,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.quitting = true
m.submitted = true
return m, tea.Quit
case key.Matches(msg, km.Down):
case key.Matches(msg, km.Down, km.NDown):
m.CursorDown()
case key.Matches(msg, km.Up):
case key.Matches(msg, km.Up, km.NUp):
m.CursorUp()
case key.Matches(msg, km.Home):
m.cursor = 0
m.viewport.GotoTop()
case key.Matches(msg, km.End):
m.cursor = len(m.choices) - 1
m.viewport.GotoBottom()
case key.Matches(msg, km.ToggleAndNext):
if m.limit == 1 {
break // no op
Expand Down Expand Up @@ -344,6 +382,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

m.keymap.FocusInSearch.SetEnabled(!m.textinput.Focused())
m.keymap.FocusOutSearch.SetEnabled(m.textinput.Focused())
m.keymap.NUp.SetEnabled(!m.textinput.Focused())
m.keymap.NDown.SetEnabled(!m.textinput.Focused())
m.keymap.Home.SetEnabled(!m.textinput.Focused())
m.keymap.End.SetEnabled(!m.textinput.Focused())

// It's possible that filtering items have caused fewer matches. So, ensure
// that the selected index is within the bounds of the number of matches.
m.cursor = clamp(0, len(m.matches)-1, m.cursor)
Expand Down

0 comments on commit f230a3d

Please sign in to comment.