1package dialog
2
3import (
4 "charm.land/lipgloss/v2"
5 "github.com/charmbracelet/catwalk/pkg/catwalk"
6 "github.com/charmbracelet/crush/internal/ui/common"
7 "github.com/charmbracelet/crush/internal/ui/styles"
8 "github.com/charmbracelet/x/ansi"
9 "github.com/sahilm/fuzzy"
10)
11
12// ModelGroup represents a group of model items.
13type ModelGroup struct {
14 Title string
15 Items []*ModelItem
16 configured bool
17 t *styles.Styles
18}
19
20// NewModelGroup creates a new ModelGroup.
21func NewModelGroup(t *styles.Styles, title string, configured bool, items ...*ModelItem) ModelGroup {
22 return ModelGroup{
23 Title: title,
24 Items: items,
25 t: t,
26 }
27}
28
29// AppendItems appends [ModelItem]s to the group.
30func (m *ModelGroup) AppendItems(items ...*ModelItem) {
31 m.Items = append(m.Items, items...)
32}
33
34// Render implements [list.Item].
35func (m *ModelGroup) Render(width int) string {
36 var configured string
37 if m.configured {
38 configuredIcon := m.t.ToolCallSuccess.Render()
39 configuredText := m.t.Subtle.Render("Configured")
40 configured = configuredIcon + " " + configuredText
41 }
42
43 title := " " + m.Title + " "
44 title = ansi.Truncate(title, max(0, width-lipgloss.Width(configured)-1), "…")
45
46 return common.Section(m.t, title, width, configured)
47}
48
49// ModelItem represents a list item for a model type.
50type ModelItem struct {
51 prov catwalk.Provider
52 model catwalk.Model
53
54 cache map[int]string
55 t *styles.Styles
56 m fuzzy.Match
57 focused bool
58}
59
60var _ ListItem = &ModelItem{}
61
62// NewModelItem creates a new ModelItem.
63func NewModelItem(t *styles.Styles, prov catwalk.Provider, model catwalk.Model) *ModelItem {
64 return &ModelItem{
65 prov: prov,
66 model: model,
67 t: t,
68 cache: make(map[int]string),
69 }
70}
71
72// Filter implements ListItem.
73func (m *ModelItem) Filter() string {
74 return m.model.Name
75}
76
77// ID implements ListItem.
78func (m *ModelItem) ID() string {
79 return modelKey(string(m.prov.ID), m.model.ID)
80}
81
82// Render implements ListItem.
83func (m *ModelItem) Render(width int) string {
84 return renderItem(m.t, m.model.Name, 0, m.focused, width, m.cache, &m.m)
85}
86
87// SetFocused implements ListItem.
88func (m *ModelItem) SetFocused(focused bool) {
89 m.focused = focused
90}
91
92// SetMatch implements ListItem.
93func (m *ModelItem) SetMatch(fm fuzzy.Match) {
94 m.m = fm
95}