From 804399699b1985480c2dea3a752201ed6b82b6d3 Mon Sep 17 00:00:00 2001 From: Garrett Martin Date: Tue, 10 Sep 2024 09:25:52 -0700 Subject: [PATCH] Add SetFilterText and SetFilterState methods --- list/list.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/list/list.go b/list/list.go index 17e6e155..6c830c14 100644 --- a/list/list.go +++ b/list/list.go @@ -258,6 +258,34 @@ func (m *Model) SetFilteringEnabled(v bool) { m.updateKeybindings() } +// SetFilterText explicitly sets the filter text without relying on user input. +// It also sets the filterState to a sane default of FilterApplied, but this +// can be changed with SetFilterState +func (m *Model) SetFilterText(filter string) { + m.filterState = Filtering + m.FilterInput.SetValue(filter) + cmd := filterItems(*m) + msg := cmd() + fmm, _ := msg.(FilterMatchesMsg) + m.filteredItems = filteredItems(fmm) + m.filterState = FilterApplied + m.Paginator.Page = 0 + m.cursor = 0 + m.FilterInput.CursorEnd() + m.updatePagination() + m.updateKeybindings() +} + +// Helper method for setting the filtering state manually +func (m *Model) SetFilterState(state FilterState) { + m.Paginator.Page = 0 + m.cursor = 0 + m.filterState = state + m.FilterInput.CursorEnd() + m.FilterInput.Focus() + m.updateKeybindings() +} + // FilteringEnabled returns whether or not filtering is enabled. func (m Model) FilteringEnabled() bool { return m.filteringEnabled