commands_item.go

 1package dialog
 2
 3import (
 4	"github.com/charmbracelet/crush/internal/ui/styles"
 5	"github.com/charmbracelet/crush/internal/uicmd"
 6	"github.com/sahilm/fuzzy"
 7)
 8
 9// CommandItem wraps a uicmd.Command to implement the ListItem interface.
10type CommandItem struct {
11	Cmd     uicmd.Command
12	t       *styles.Styles
13	m       fuzzy.Match
14	cache   map[int]string
15	focused bool
16}
17
18var _ ListItem = &CommandItem{}
19
20// NewCommandItem creates a new CommandItem.
21func NewCommandItem(t *styles.Styles, cmd uicmd.Command) *CommandItem {
22	return &CommandItem{
23		Cmd: cmd,
24		t:   t,
25	}
26}
27
28// Filter implements ListItem.
29func (c *CommandItem) Filter() string {
30	return c.Cmd.Title
31}
32
33// ID implements ListItem.
34func (c *CommandItem) ID() string {
35	return c.Cmd.ID
36}
37
38// SetFocused implements ListItem.
39func (c *CommandItem) SetFocused(focused bool) {
40	if c.focused != focused {
41		c.cache = nil
42	}
43	c.focused = focused
44}
45
46// SetMatch implements ListItem.
47func (c *CommandItem) SetMatch(m fuzzy.Match) {
48	c.cache = nil
49	c.m = m
50}
51
52// Render implements ListItem.
53func (c *CommandItem) Render(width int) string {
54	return renderItem(c.t, c.Cmd.Title, c.Cmd.Shortcut, c.focused, width, c.cache, &c.m)
55}