▍ humdrum codex / glint v1.0.2
license AGPL-3.0
998 B raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package editor

import (
	"strings"

	"github.com/charmbracelet/lipgloss"
)

// Span is a run of text with a single Lipgloss style.
type Span struct {
	Text  string
	Style lipgloss.Style
}

// renderSpans concatenates styled spans into a single string.
func renderSpans(spans []Span) string {
	var b strings.Builder
	for _, s := range spans {
		b.WriteString(s.Style.Render(s.Text))
	}
	return b.String()
}

// renderSpansCursor renders spans rune-by-rune, drawing the cursor cell at the
// given rune column with cursorStyle. A cursor at or past the end of the line
// is drawn as a styled space.
func renderSpansCursor(spans []Span, col int, cursorStyle lipgloss.Style) string {
	var b strings.Builder
	idx := 0
	for _, s := range spans {
		for _, r := range s.Text {
			if idx == col {
				b.WriteString(cursorStyle.Render(string(r)))
			} else {
				b.WriteString(s.Style.Render(string(r)))
			}
			idx++
		}
	}
	if col >= idx {
		b.WriteString(cursorStyle.Render(" "))
	}
	return b.String()
}