From 8422c49018116886ae23356c2101c9f7076c865b Mon Sep 17 00:00:00 2001 From: Piero Lescano Date: Thu, 25 Jul 2024 09:02:56 -0500 Subject: [PATCH] feat(filter): Add cyclic navigation (#483) --- filter/filter.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/filter/filter.go b/filter/filter.go index 1b827eb01..92b567708 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -255,29 +255,41 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m *model) CursorUp() { if m.reverse { - m.cursor = clamp(0, len(m.matches)-1, m.cursor+1) + m.cursor = (m.cursor + 1) % len(m.matches) if len(m.matches)-m.cursor <= m.viewport.YOffset { - m.viewport.SetYOffset(len(m.matches) - m.cursor - 1) + m.viewport.LineUp(1) + } + if len(m.matches)-m.cursor > m.viewport.Height+m.viewport.YOffset { + m.viewport.SetYOffset(len(m.matches) - m.viewport.Height) } } else { - m.cursor = clamp(0, len(m.matches)-1, m.cursor-1) + m.cursor = (m.cursor - 1 + len(m.matches)) % len(m.matches) if m.cursor < m.viewport.YOffset { - m.viewport.SetYOffset(m.cursor) + m.viewport.LineUp(1) + } + if m.cursor >= m.viewport.YOffset+m.viewport.Height { + m.viewport.SetYOffset(len(m.matches) - m.viewport.Height) } } } func (m *model) CursorDown() { if m.reverse { - m.cursor = clamp(0, len(m.matches)-1, m.cursor-1) + m.cursor = (m.cursor - 1 + len(m.matches)) % len(m.matches) if len(m.matches)-m.cursor > m.viewport.Height+m.viewport.YOffset { m.viewport.LineDown(1) } + if len(m.matches)-m.cursor <= m.viewport.YOffset { + m.viewport.GotoTop() + } } else { - m.cursor = clamp(0, len(m.matches)-1, m.cursor+1) + m.cursor = (m.cursor + 1) % len(m.matches) if m.cursor >= m.viewport.YOffset+m.viewport.Height { m.viewport.LineDown(1) } + if m.cursor < m.viewport.YOffset { + m.viewport.GotoTop() + } } }