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	// Highlight highlights the content from the given start to end positions.
25	// Use -1 for no highlight.
26	Highlight(startLine, startCol, endLine, endCol int)
27}
28
29// MouseClickable represents an item that can handle mouse click events.
30type MouseClickable interface {
31	// HandleMouseClick processes a mouse click event at the given coordinates.
32	// It returns true if the event was handled, false otherwise.
33	HandleMouseClick(btn ansi.MouseButton, x, y int) bool
34}
35
36// SpacerItem is a spacer item that adds vertical space in the list.
37type SpacerItem struct {
38	Height int
39}
40
41// NewSpacerItem creates a new [SpacerItem] with the specified height.
42func NewSpacerItem(height int) *SpacerItem {
43	return &SpacerItem{
44		Height: max(0, height-1),
45	}
46}
47
48// Render implements the Item interface for [SpacerItem].
49func (s *SpacerItem) Render(width int) string {
50	return strings.Repeat("\n", s.Height)
51}