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// RawRenderable represents an item that can provide a raw rendering
17// without additional styling.
18type RawRenderable interface {
19	// RawRender returns the raw rendered string without any additional
20	// styling.
21	RawRender(width int) string
22}
23
24// Focusable represents an item that can be aware of focus state changes.
25type Focusable interface {
26	// SetFocused sets the focus state of the item.
27	SetFocused(focused bool)
28}
29
30// Highlightable represents an item that can highlight a portion of its content.
31type Highlightable interface {
32	// SetHighlight highlights the content from the given start to end
33	// positions. Use -1 for no highlight.
34	SetHighlight(startLine, startCol, endLine, endCol int)
35	// Highlight returns the current highlight positions within the item.
36	Highlight() (startLine, startCol, endLine, endCol int)
37}
38
39// MouseClickable represents an item that can handle mouse click events.
40type MouseClickable interface {
41	// HandleMouseClick processes a mouse click event at the given coordinates.
42	// It returns true if the event was handled, false otherwise.
43	HandleMouseClick(btn ansi.MouseButton, x, y int) bool
44}
45
46// SpacerItem is a spacer item that adds vertical space in the list.
47type SpacerItem struct {
48	Height int
49}
50
51// NewSpacerItem creates a new [SpacerItem] with the specified height.
52func NewSpacerItem(height int) *SpacerItem {
53	return &SpacerItem{
54		Height: max(0, height-1),
55	}
56}
57
58// Render implements the Item interface for [SpacerItem].
59func (s *SpacerItem) Render(width int) string {
60	return strings.Repeat("\n", s.Height)
61}