fix(ui): incorrect help while filtering

Ayman Bagabas created

Change summary

ui/pages/selection/selection.go | 76 +++++++++++++++++++---------------
ui/ui.go                        | 35 +++++++++------
2 files changed, 64 insertions(+), 47 deletions(-)

Detailed changes

ui/pages/selection/selection.go 🔗

@@ -82,7 +82,7 @@ func (s *Selection) getMargins() (wm, hm int) {
 	wm = 0
 	hm = s.common.Styles.Tabs.GetVerticalFrameSize() +
 		s.common.Styles.Tabs.GetHeight()
-	if s.activePane == selectorPane && s.FilterState() == list.Filtering {
+	if s.activePane == selectorPane && s.IsFiltering() {
 		// hide tabs when filtering
 		hm = 0
 	}
@@ -103,6 +103,11 @@ func (s *Selection) SetSize(width, height int) {
 	s.readme.SetSize(width-wm, height-hm-1) // -1 for readme status line
 }
 
+// IsFiltering returns true if the selector is currently filtering.
+func (s *Selection) IsFiltering() bool {
+	return s.FilterState() == list.Filtering
+}
+
 // ShortHelp implements help.KeyMap.
 func (s *Selection) ShortHelp() []key.Binding {
 	k := s.selector.KeyMap
@@ -126,49 +131,54 @@ func (s *Selection) ShortHelp() []key.Binding {
 
 // FullHelp implements help.KeyMap.
 func (s *Selection) FullHelp() [][]key.Binding {
+	b := [][]key.Binding{
+		{
+			s.common.KeyMap.Section,
+		},
+	}
 	switch s.activePane {
 	case readmePane:
 		k := s.readme.KeyMap
-		return [][]key.Binding{
-			{
-				k.PageDown,
-				k.PageUp,
-			},
-			{
-				k.HalfPageDown,
-				k.HalfPageUp,
-			},
-			{
-				k.Down,
-				k.Up,
-			},
-		}
+		b = append(b, []key.Binding{
+			k.PageDown,
+			k.PageUp,
+		})
+		b = append(b, []key.Binding{
+			k.HalfPageDown,
+			k.HalfPageUp,
+		})
+		b = append(b, []key.Binding{
+			k.Down,
+			k.Up,
+		})
 	case selectorPane:
 		copyKey := s.common.KeyMap.Copy
 		copyKey.SetHelp("c", "copy command")
 		k := s.selector.KeyMap
-		return [][]key.Binding{
-			{
+		if !s.IsFiltering() {
+			b[0] = append(b[0],
 				s.common.KeyMap.Select,
 				copyKey,
-				k.CursorUp,
-				k.CursorDown,
-			},
-			{
-				k.NextPage,
-				k.PrevPage,
-				k.GoToStart,
-				k.GoToEnd,
-			},
-			{
-				k.Filter,
-				k.ClearFilter,
-				k.CancelWhileFiltering,
-				k.AcceptWhileFiltering,
-			},
+			)
 		}
+		b = append(b, []key.Binding{
+			k.CursorUp,
+			k.CursorDown,
+		})
+		b = append(b, []key.Binding{
+			k.NextPage,
+			k.PrevPage,
+			k.GoToStart,
+			k.GoToEnd,
+		})
+		b = append(b, []key.Binding{
+			k.Filter,
+			k.ClearFilter,
+			k.CancelWhileFiltering,
+			k.AcceptWhileFiltering,
+		})
 	}
-	return [][]key.Binding{}
+	return b
 }
 
 // Init implements tea.Model.

ui/ui.go 🔗

@@ -95,10 +95,10 @@ func (ui *UI) ShortHelp() []key.Binding {
 	case loadedState:
 		b = append(b, ui.pages[ui.activePage].ShortHelp()...)
 	}
-	b = append(b,
-		ui.common.KeyMap.Quit,
-		ui.common.KeyMap.Help,
-	)
+	if !ui.IsFiltering() {
+		b = append(b, ui.common.KeyMap.Quit)
+	}
+	b = append(b, ui.common.KeyMap.Help)
 	return b
 }
 
@@ -111,10 +111,13 @@ func (ui *UI) FullHelp() [][]key.Binding {
 	case loadedState:
 		b = append(b, ui.pages[ui.activePage].FullHelp()...)
 	}
-	b = append(b, []key.Binding{
-		ui.common.KeyMap.Quit,
+	h := []key.Binding{
 		ui.common.KeyMap.Help,
-	})
+	}
+	if !ui.IsFiltering() {
+		h = append(h, ui.common.KeyMap.Quit)
+	}
+	b = append(b, h)
 	return b
 }
 
@@ -156,6 +159,16 @@ func (ui *UI) Init() tea.Cmd {
 	return tea.Batch(cmds...)
 }
 
+// IsFiltering returns true if the selection page is filtering.
+func (ui *UI) IsFiltering() bool {
+	if ui.activePage == selectionPage {
+		if s, ok := ui.pages[selectionPage].(*selection.Selection); ok && s.FilterState() == list.Filtering {
+			return true
+		}
+	}
+	return false
+}
+
 // Update implements tea.Model.
 func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	cmds := make([]tea.Cmd, 0)
@@ -181,13 +194,7 @@ func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 			case key.Matches(msg, ui.common.KeyMap.Help):
 				cmds = append(cmds, footer.ToggleFooterCmd)
 			case key.Matches(msg, ui.common.KeyMap.Quit):
-				switch {
-				case ui.activePage == selectionPage:
-					if s, ok := ui.pages[selectionPage].(*selection.Selection); ok && s.FilterState() == list.Filtering {
-						break
-					}
-					fallthrough
-				default:
+				if !ui.IsFiltering() {
 					// Stop bubblezone background workers.
 					ui.common.Zone.Close()
 					return ui, tea.Quit