@@ -2,25 +2,60 @@ package list
6 "charm.land/lipgloss/v2"
7 "charm.land/lipgloss/v2"
7 uv "github.com/charmbracelet/ultraviolet"
8 uv "github.com/charmbracelet/ultraviolet"
10// DefaultHighlighter is the default highlighter function that applies inverse style.
11// DefaultHighlighter is the default highlighter function that applies inverse style.
11−var DefaultHighlighter Highlighter = func(s uv.Style) uv.Style {
12+var DefaultHighlighter Highlighter = func(x, y int, c *uv.Cell) *uv.Cell {
12− s.Attrs |= uv.AttrReverse
16+ c.Style.Attrs |= uv.AttrReverse
16// Highlighter represents a function that defines how to highlight text.
20// 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
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 {
31+ } else if y > pos.Y {
32+ sb.WriteString(strings.Repeat("\n", y-pos.Y))
35+ sb.WriteString(c.Content)
19// Highlight highlights a region of text within the given content and region.
44// Highlight highlights a region of text within the given content and region.
20func Highlight(content string, area image.Rectangle, startLine, startCol, endLine, endCol int, highlighter Highlighter) string {
45func 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)
22 return content
48 return content
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 {
25 if highlighter == nil {
60 if highlighter == nil {
26 highlighter = DefaultHighlighter
61 highlighter = DefaultHighlighter
@@ -87,17 +122,22 @@ func Highlight(content string, area image.Rectangle, startLine, startCol, endLin
89 cell := line.At(x)
124 cell := line.At(x)
90− cell.Style = highlighter(cell.Style)
126+ line.Set(x, highlighter(x, y, cell))
94− return buf.Render()
131+ return &buf
97// ToHighlighter converts a [lipgloss.Style] to a [Highlighter].
134// ToHighlighter converts a [lipgloss.Style] to a [Highlighter].
98func ToHighlighter(lgStyle lipgloss.Style) Highlighter {
135func ToHighlighter(lgStyle lipgloss.Style) Highlighter {
99− return func(uv.Style) uv.Style {
136+ return func(_ int, _ int, c *uv.Cell) *uv.Cell {
100− return ToStyle(lgStyle)
138+ c.Style = ToStyle(lgStyle)