on main help sign in
feat(ui): models dialog: filter by provider and model name
f541590Ayman Bagabas
internal/ui/dialog/models_list.go+30−14
@@ -1,7 +1,10 @@
1package dialog
1package dialog
2
2
3import (
3import (
4+ "fmt"
4 "slices"
5 "slices"
6+ "sort"
7+ "strings"
5
8
6 "github.com/charmbracelet/crush/internal/ui/list"
9 "github.com/charmbracelet/crush/internal/ui/list"
7 "github.com/charmbracelet/crush/internal/ui/styles"
10 "github.com/charmbracelet/crush/internal/ui/styles"
@@ -186,16 +189,21 @@ func (f *ModelsList) VisibleItems() []list.Item {
186 }
189 }
187 }
190 }
188
191
189− matches := fuzzy.FindFrom(f.query, list.FilterableItemsSource(filterableItems))
192+ filterValue := func(itm *ModelItem) string {
190− for _, match := range matches {
193+ return strings.ToLower(fmt.Sprintf("%s %s", itm.prov.Name, itm.model.Name))
191− item := filterableItems[match.Index]
194+ }
192− if ms, ok := item.(list.MatchSettable); ok {
195+
193− ms.SetMatch(match)
196+ names := make([]string, len(filterableItems))
194− item = ms.(list.FilterableItem)
197+ for i, item := range filterableItems {
195− }
198+ ms := item.(*ModelItem)
196− filterableItems = append(filterableItems, item)
199+ names[i] = filterValue(ms)
197 }
200 }
198
201
202+ matches := fuzzy.Find(f.query, names)
203+ sort.SliceStable(matches, func(i, j int) bool {
204+ return matches[i].Score > matches[j].Score
205+ })
206+
199 items := []list.Item{}
207 items := []list.Item{}
200 visitedGroups := map[int]bool{}
208 visitedGroups := map[int]bool{}
201
209
@@ -203,19 +211,27 @@ func (f *ModelsList) VisibleItems() []list.Item {
203 // Find which group this item belongs to
211 // Find which group this item belongs to
204 for gi, g := range f.groups {
212 for gi, g := range f.groups {
205 addedCount := 0
213 addedCount := 0
214+ name := g.Title + " "
206 for _, match := range matches {
215 for _, match := range matches {
207− item := filterableItems[match.Index]
216+ item := filterableItems[match.Index].(*ModelItem)
217+ idxs := []int{}
218+ for _, idx := range match.MatchedIndexes {
219+ // Adjusts removing provider name highlights
220+ if idx < len(name) {
221+ continue
222+ }
223+ idxs = append(idxs, idx-len(name))
224+ }
225+
226+ match.MatchedIndexes = idxs
208− if slices.Contains(g.Items, item.(*ModelItem)) {
227+ if slices.Contains(g.Items, item) {
209 if !visitedGroups[gi] {
228 if !visitedGroups[gi] {
210 // Add section header
229 // Add section header
211 items = append(items, &g)
230 items = append(items, &g)
212 visitedGroups[gi] = true
231 visitedGroups[gi] = true
213 }
232 }
214 // Add the matched item
233 // Add the matched item
215− if ms, ok := item.(list.MatchSettable); ok {
216− ms.SetMatch(match)
234+ item.SetMatch(match)
217− item = ms.(list.FilterableItem)
218− }
219 items = append(items, item)
235 items = append(items, item)
220 addedCount++
236 addedCount++
221 }
237 }