item.go

 1package list
 2
 3import (
 4	"strings"
 5
 6	"github.com/charmbracelet/x/ansi"
 7)
 8
 9// Item represents a single item in the lazy-loaded list.
10type Item interface {
11	// Render returns the string representation of the item for the given
12	// width.
13	Render(width int) string
14}
15
16// Focusable represents an item that can be aware of focus state changes.
17type Focusable interface {
18	// SetFocused sets the focus state of the item.
19	SetFocused(focused bool)
20}
21
22// Highlightable represents an item that can highlight a portion of its content.
23type Highlightable interface {
24	// SetHighlight highlights the content from the given start to end
25	// positions. Use -1 for no highlight.
26	SetHighlight(startLine, startCol, endLine, endCol int)
27	// Highlight returns the current highlight positions within the item.
28	Highlight() (startLine, startCol, endLine, endCol int)
29}
30
31// MouseClickable represents an item that can handle mouse click events.
32type MouseClickable interface {
33	// HandleMouseClick processes a mouse click event at the given coordinates.
34	// It returns true if the event was handled, false otherwise.
35	HandleMouseClick(btn ansi.MouseButton, x, y int) bool
36}
37
38// SpacerItem is a spacer item that adds vertical space in the list.
39type SpacerItem struct {
40	Height int
41}
42
43// NewSpacerItem creates a new [SpacerItem] with the specified height.
44func NewSpacerItem(height int) *SpacerItem {
45	return &SpacerItem{
46		Height: max(0, height-1),
47	}
48}
49
50// Render implements the Item interface for [SpacerItem].
51func (s *SpacerItem) Render(width int) string {
52	return strings.Repeat("\n", s.Height)
53}