commands_item.go

 1package dialog
 2
 3import (
 4	"github.com/charmbracelet/crush/internal/ui/styles"
 5	"github.com/sahilm/fuzzy"
 6)
 7
 8// CommandItem wraps a uicmd.Command to implement the ListItem interface.
 9type CommandItem struct {
10	id       string
11	title    string
12	shortcut string
13	action   Action
14	t        *styles.Styles
15	m        fuzzy.Match
16	cache    map[int]string
17	focused  bool
18}
19
20var _ ListItem = &CommandItem{}
21
22// NewCommandItem creates a new CommandItem.
23func NewCommandItem(t *styles.Styles, id, title, shortcut string, action Action) *CommandItem {
24	return &CommandItem{
25		id:       id,
26		t:        t,
27		title:    title,
28		shortcut: shortcut,
29		action:   action,
30	}
31}
32
33// Filter implements ListItem.
34func (c *CommandItem) Filter() string {
35	return c.title
36}
37
38// ID implements ListItem.
39func (c *CommandItem) ID() string {
40	return c.id
41}
42
43// SetFocused implements ListItem.
44func (c *CommandItem) SetFocused(focused bool) {
45	if c.focused != focused {
46		c.cache = nil
47	}
48	c.focused = focused
49}
50
51// SetMatch implements ListItem.
52func (c *CommandItem) SetMatch(m fuzzy.Match) {
53	c.cache = nil
54	c.m = m
55}
56
57// Action returns the action associated with the command item.
58func (c *CommandItem) Action() Action {
59	return c.action
60}
61
62// Shortcut returns the shortcut associated with the command item.
63func (c *CommandItem) Shortcut() string {
64	return c.shortcut
65}
66
67// Render implements ListItem.
68func (c *CommandItem) Render(width int) string {
69	styles := ListItemStyles{
70		ItemBlurred:     c.t.Dialog.NormalItem,
71		ItemFocused:     c.t.Dialog.SelectedItem,
72		InfoTextBlurred: c.t.Base,
73		InfoTextFocused: c.t.Base,
74	}
75	return renderItem(styles, c.title, c.shortcut, c.focused, width, c.cache, &c.m)
76}