@@ -3,8 +3,6 @@ package list
9 "github.com/charmbracelet/bubbles/v2/key"
7 "github.com/charmbracelet/bubbles/v2/key"
10 "github.com/charmbracelet/bubbles/v2/textinput"
8 "github.com/charmbracelet/bubbles/v2/textinput"
@@ -28,7 +26,9 @@ type FilterableList[T FilterableItem] interface {
28 Cursor() *tea.Cursor
26 Cursor() *tea.Cursor
29 SetInputWidth(int)
27 SetInputWidth(int)
30 SetInputPlaceholder(string)
28 SetInputPlaceholder(string)
31 Filter(q string) tea.Cmd
30 Filter(q string) tea.Cmd
34type HasMatchIndexes interface {
34type HasMatchIndexes interface {
@@ -47,10 +47,11 @@ type filterableList[T FilterableItem] struct {
47 *filterableOptions
47 *filterableOptions
48 width, height int
48 width, height int
49 // stores all available items
49 // stores all available items
50− items []T
50+ items []T
51− input textinput.Model
52+ input textinput.Model
52− inputWidth int
53+ inputWidth int
53− query string
54+ query string
56type filterableListOption func(*filterableOptions)
57type filterableListOption func(*filterableOptions)
@@ -246,22 +247,18 @@ func (f *filterableList[T]) Filter(query string) tea.Cmd {
246 return f.list.SetItems(f.items)
247 return f.list.SetItems(f.items)
249− words := make([]string, len(f.items))
250− for i, item := range f.items {
251− words[i] = strings.ToLower(item.FilterValue())
254− matches := fuzzy.Find(query, words)
250+ matches := fuzzy.FindFrom(query, f)
256− sort.SliceStable(matches, func(i, j int) bool {
257− return matches[i].Score > matches[j].Score
260 var matchedItems []T
252 var matchedItems []T
253+ resultSize := len(matches)
254+ if f.resultsSize > 0 && resultSize > f.resultsSize {
255+ resultSize = f.resultsSize
261− for _, match := range matches {
257+ for i := range resultSize {
262 item := f.items[match.Index]
259 item := f.items[match.Index]
263− if i, ok := any(item).(HasMatchIndexes); ok {
260+ if it, ok := any(item).(HasMatchIndexes); ok {
264− i.MatchIndexes(match.MatchedIndexes)
261+ it.MatchIndexes(match.MatchedIndexes)
266 matchedItems = append(matchedItems, item)
263 matchedItems = append(matchedItems, item)
@@ -307,3 +304,15 @@ func (f *filterableList[T]) SetInputWidth(w int) {
307func (f *filterableList[T]) SetInputPlaceholder(ph string) {
304func (f *filterableList[T]) SetInputPlaceholder(ph string) {
308 f.placeholder = ph
305 f.placeholder = ph
308+func (f *filterableList[T]) SetResultsSize(size int) {
309+ f.resultsSize = size
312+func (f *filterableList[T]) String(i int) string {
313+ return f.items[i].FilterValue()
316+func (f *filterableList[T]) Len() int {