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