From 616c7a8fcc5d7a65f0f71743dbbcb8bbd3ba5d43 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Fri, 21 Jan 2022 11:20:40 -0300 Subject: [PATCH 1/4] feat: allow custom filter functions Signed-off-by: Carlos A Becker --- list/list.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/list/list.go b/list/list.go index 156c54a9..a9714881 100644 --- a/list/list.go +++ b/list/list.go @@ -71,6 +71,34 @@ func (f filteredItems) items() []Item { // message should be routed to Update for processing. type FilterMatchesMsg []filteredItem +// Filter takes a term and a list of strings to search through +// (defined by Item#FilterValue). +// It should return a sorted list of ranks. +type Filter func(string, []string) []Rank + +// Rank defines a rank for a given item. +type Rank struct { + // The index of the item in the original input. + Index int + // Indices of the actual word that were matched against the filter term. + MatchedIndexes []int +} + +// DefaultFilter uses the sahilm/fuzzy to filter through the list. +// This is set by default. +func DefaultFilter(term string, targets []string) []Rank { + var ranks fuzzy.Matches = fuzzy.Find(term, targets) + sort.Stable(ranks) + result := make([]Rank, len(ranks)) + for i, r := range ranks { + result[i] = Rank{ + Index: r.Index, + MatchedIndexes: r.MatchedIndexes, + } + } + return result +} + type statusMessageTimeoutMsg struct{} // FilterState describes the current filtering state on the model. @@ -107,6 +135,8 @@ type Model struct { // Key mappings for navigating the list. KeyMap KeyMap + Filter Filter + disableQuitKeybindings bool // Additional key mappings for the short and full help views. This allows @@ -173,6 +203,7 @@ func New(items []Item, delegate ItemDelegate, width, height int) Model { showHelp: true, filteringEnabled: true, KeyMap: DefaultKeyMap(), + Filter: DefaultFilter, Styles: styles, Title: "List", FilterInput: filterInput, @@ -1133,11 +1164,8 @@ func filterItems(m Model) tea.Cmd { targets = append(targets, t.FilterValue()) } - var ranks fuzzy.Matches = fuzzy.Find(m.FilterInput.Value(), targets) - sort.Stable(ranks) - filterMatches := []filteredItem{} - for _, r := range ranks { + for _, r := range m.Filter(m.FilterInput.Value(), targets) { filterMatches = append(filterMatches, filteredItem{ item: items[r.Index], matches: r.MatchedIndexes, From 66379bd53b615c7f3631ccc25bdb90a86b988e9d Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Fri, 21 Jan 2022 14:47:12 -0300 Subject: [PATCH 2/4] fix: type name Signed-off-by: Carlos A Becker --- list/list.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/list/list.go b/list/list.go index a9714881..9b03bfd1 100644 --- a/list/list.go +++ b/list/list.go @@ -71,10 +71,10 @@ func (f filteredItems) items() []Item { // message should be routed to Update for processing. type FilterMatchesMsg []filteredItem -// Filter takes a term and a list of strings to search through +// FilterFunc takes a term and a list of strings to search through // (defined by Item#FilterValue). // It should return a sorted list of ranks. -type Filter func(string, []string) []Rank +type FilterFunc func(string, []string) []Rank // Rank defines a rank for a given item. type Rank struct { @@ -135,7 +135,7 @@ type Model struct { // Key mappings for navigating the list. KeyMap KeyMap - Filter Filter + Filter FilterFunc disableQuitKeybindings bool From 58074510e92031bc92362cfad8db921e1f9f960b Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Fri, 21 Jan 2022 14:47:52 -0300 Subject: [PATCH 3/4] docs: godoc Signed-off-by: Carlos A Becker --- list/list.go | 1 + 1 file changed, 1 insertion(+) diff --git a/list/list.go b/list/list.go index 9b03bfd1..86099832 100644 --- a/list/list.go +++ b/list/list.go @@ -135,6 +135,7 @@ type Model struct { // Key mappings for navigating the list. KeyMap KeyMap + // Filter used to filter the list. Filter FilterFunc disableQuitKeybindings bool From 1355ff643a7517039a82e79bad64e56c5eb4a19e Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Wed, 30 Mar 2022 15:17:23 -0400 Subject: [PATCH 4/4] docs(list): fix typo in doc comment --- list/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/list/list.go b/list/list.go index 86099832..4e2ec2d2 100644 --- a/list/list.go +++ b/list/list.go @@ -135,7 +135,7 @@ type Model struct { // Key mappings for navigating the list. KeyMap KeyMap - // Filter used to filter the list. + // Filter is used to filter the list. Filter FilterFunc disableQuitKeybindings bool