@@ -3,8 +3,6 @@ package list
33import (
44 "regexp"
55 "slices"
6− "sort"
7− "strings"
86
97 "github.com/charmbracelet/bubbles/v2/key"
108 "github.com/charmbracelet/bubbles/v2/textinput"
@@ -28,7 +26,9 @@ type FilterableList[T FilterableItem] interface {
2826 Cursor() *tea.Cursor
2927 SetInputWidth(int)
3028 SetInputPlaceholder(string)
29+ SetResultsSize(int)
3130 Filter(q string) tea.Cmd
31+ fuzzy.Source
3232}
3333
3434type HasMatchIndexes interface {
@@ -47,10 +47,11 @@ type filterableList[T FilterableItem] struct {
4747 *filterableOptions
4848 width, height int
4949 // stores all available items
50− items []T
51− input textinput.Model
52− inputWidth int
53− query string
50+ items []T
51+ resultsSize int
52+ input textinput.Model
53+ inputWidth int
54+ query string
5455}
5556
5657type filterableListOption func(*filterableOptions)
@@ -246,22 +247,18 @@ func (f *filterableList[T]) Filter(query string) tea.Cmd {
246247 return f.list.SetItems(f.items)
247248 }
248249
249− words := make([]string, len(f.items))
250− for i, item := range f.items {
251− words[i] = strings.ToLower(item.FilterValue())
252− }
253−
254− matches := fuzzy.Find(query, words)
255−
256− sort.SliceStable(matches, func(i, j int) bool {
257− return matches[i].Score > matches[j].Score
258− })
250+ matches := fuzzy.FindFrom(query, f)
259251
260252 var matchedItems []T
261− for _, match := range matches {
253+ resultSize := len(matches)
254+ if f.resultsSize > 0 && resultSize > f.resultsSize {
255+ resultSize = f.resultsSize
256+ }
257+ for i := range resultSize {
258+ match := matches[i]
262259 item := f.items[match.Index]
263− if i, ok := any(item).(HasMatchIndexes); ok {
264− i.MatchIndexes(match.MatchedIndexes)
260+ if it, ok := any(item).(HasMatchIndexes); ok {
261+ it.MatchIndexes(match.MatchedIndexes)
265262 }
266263 matchedItems = append(matchedItems, item)
267264 }
@@ -307,3 +304,15 @@ func (f *filterableList[T]) SetInputWidth(w int) {
307304func (f *filterableList[T]) SetInputPlaceholder(ph string) {
308305 f.placeholder = ph
309306}
307+
308+func (f *filterableList[T]) SetResultsSize(size int) {
309+ f.resultsSize = size
310+}
311+
312+func (f *filterableList[T]) String(i int) string {
313+ return f.items[i].FilterValue()
314+}
315+
316+func (f *filterableList[T]) Len() int {
317+ return len(f.items)
318+}