on main help sign in
fix(ui): implement Highlightable interface for message items
commitd5b83c0dbbaa228bf3f783495d73e6db84489e2d authorAyman Bagabas <ayman.bagabas@gmail.com> dateWed 21 Jan 2026, 11:43 (9 months ago) parent1ff0a53
4 files changed+64−15
internal/ui/chat/messages.go+9−2 internal/ui/list/highlight.go+49−9 internal/ui/list/item.go+5−3 internal/ui/model/chat.go+1−1
internal/ui/chat/messages.go+9−2
@@ -77,6 +77,8 @@ type highlightableMessageItem struct {
7777 highlighter list.Highlighter
7878}
7979
80+var _ list.Highlightable = (*highlightableMessageItem)(nil)
81+
8082// isHighlighted returns true if the item has a highlight range set.
8183func (h *highlightableMessageItem) isHighlighted() bool {
8284 return h.startLine != -1 || h.endLine != -1
@@ -91,8 +93,8 @@ func (h *highlightableMessageItem) renderHighlighted(content string, width, heig
9193 return list.Highlight(content, area, h.startLine, h.startCol, h.endLine, h.endCol, h.highlighter)
9294}
9395
94−// Highlight implements MessageItem.
95−func (h *highlightableMessageItem) Highlight(startLine int, startCol int, endLine int, endCol int) {
96+// SetHighlight implements [MessageItem].
97+func (h *highlightableMessageItem) SetHighlight(startLine int, startCol int, endLine int, endCol int) {
9698 // Adjust columns for the style's left inset (border + padding) since we
9799 // highlight the content only.
98100 offset := messageLeftPaddingTotal
@@ -106,6 +108,11 @@ func (h *highlightableMessageItem) Highlight(startLine int, startCol int, endLin
106108 }
107109}
108110
111+// Highlight implements [MessageItem].
112+func (h *highlightableMessageItem) Highlight() (startLine int, startCol int, endLine int, endCol int) {
113+ return h.startLine, h.startCol, h.endLine, h.endCol
114+}
115+
109116func defaultHighlighter(sty *styles.Styles) *highlightableMessageItem {
110117 return &highlightableMessageItem{
111118 startLine: -1,
internal/ui/list/highlight.go+49−9
@@ -2,25 +2,60 @@ package list
22
33import (
44 "image"
5+ "strings"
56
67 "charm.land/lipgloss/v2"
78 uv "github.com/charmbracelet/ultraviolet"
89)
910
1011// DefaultHighlighter is the default highlighter function that applies inverse style.
11−var DefaultHighlighter Highlighter = func(s uv.Style) uv.Style {
12− s.Attrs |= uv.AttrReverse
13− return s
12+var DefaultHighlighter Highlighter = func(x, y int, c *uv.Cell) *uv.Cell {
13+ if c == nil {
14+ return c
15+ }
16+ c.Style.Attrs |= uv.AttrReverse
17+ return c
1418}
1519
1620// Highlighter represents a function that defines how to highlight text.
17−type Highlighter func(uv.Style) uv.Style
21+type Highlighter func(x, y int, c *uv.Cell) *uv.Cell
22+
23+// HighlightContent returns the content with highlighted regions based on the specified parameters.
24+func HighlightContent(content string, area image.Rectangle, startLine, startCol, endLine, endCol int) string {
25+ var sb strings.Builder
26+ pos := image.Pt(-1, -1)
27+ HighlightBuffer(content, area, startLine, startCol, endLine, endCol, func(x, y int, c *uv.Cell) *uv.Cell {
28+ pos.X = x
29+ if pos.Y == -1 {
30+ pos.Y = y
31+ } else if y > pos.Y {
32+ sb.WriteString(strings.Repeat("\n", y-pos.Y))
33+ pos.Y = y
34+ }
35+ sb.WriteString(c.Content)
36+ return c
37+ })
38+ if sb.Len() > 0 {
39+ sb.WriteString("\n")
40+ }
41+ return sb.String()
42+}
1843
1944// Highlight highlights a region of text within the given content and region.
2045func Highlight(content string, area image.Rectangle, startLine, startCol, endLine, endCol int, highlighter Highlighter) string {
21− if startLine < 0 || startCol < 0 {
46+ buf := HighlightBuffer(content, area, startLine, startCol, endLine, endCol, highlighter)
47+ if buf == nil {
2248 return content
2349 }
50+ return buf.Render()
51+}
52+
53+// HighlightBuffer highlights a region of text within the given content and
54+// region, returning a [uv.ScreenBuffer].
55+func HighlightBuffer(content string, area image.Rectangle, startLine, startCol, endLine, endCol int, highlighter Highlighter) *uv.ScreenBuffer {
56+ if startLine < 0 || startCol < 0 {
57+ return nil
58+ }
2459
2560 if highlighter == nil {
2661 highlighter = DefaultHighlighter
@@ -87,17 +122,22 @@ func Highlight(content string, area image.Rectangle, startLine, startCol, endLin
87122 continue
88123 }
89124 cell := line.At(x)
90− cell.Style = highlighter(cell.Style)
125+ if cell != nil {
126+ line.Set(x, highlighter(x, y, cell))
127+ }
91128 }
92129 }
93130
94− return buf.Render()
131+ return &buf
95132}
96133
97134// ToHighlighter converts a [lipgloss.Style] to a [Highlighter].
98135func ToHighlighter(lgStyle lipgloss.Style) Highlighter {
99− return func(uv.Style) uv.Style {
100− return ToStyle(lgStyle)
136+ return func(_ int, _ int, c *uv.Cell) *uv.Cell {
137+ if c != nil {
138+ c.Style = ToStyle(lgStyle)
139+ }
140+ return c
101141 }
102142}
103143
internal/ui/list/item.go+5−3
@@ -21,9 +21,11 @@ type Focusable interface {
2121
2222// Highlightable represents an item that can highlight a portion of its content.
2323type 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)
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)
2729}
2830
2931// MouseClickable represents an item that can handle mouse click events.
internal/ui/model/chat.go+1−1
@@ -515,7 +515,7 @@ func (m *Chat) applyHighlightRange(idx, selectedIdx int, item list.Item) list.It
515515 }
516516 }
517517
518− hi.Highlight(sLine, sCol, eLine, eCol)
518+ hi.SetHighlight(sLine, sCol, eLine, eCol)
519519 return hi.(list.Item)
520520 }
521521