Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/tui/components/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

// ansiRegexp matches ANSI escape sequences so they can be removed when
// computing layout measurements.
var ansiRegexp = regexp.MustCompile(`\x1b\[[0-9;]*[A-Za-z]`)
var ansiRegexp = regexp.MustCompile(`\x1b\[[0-9;]*[A-Za-z]|\x1b]8;[^\x07]*\x07`)

const (
// maxInlinePasteLines is the maximum number of lines for inline paste.
Expand Down
66 changes: 63 additions & 3 deletions pkg/tui/components/markdown/fast_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ func (s ansiStyle) renderTo(b *strings.Builder, text string) {
b.WriteString(s.suffix)
}

const (
osc8Open = "\x1b]8;;"
osc8Close = "\x07"
)

// writeOSC8Link wraps styled visible text in OSC 8 hyperlink escape sequences,
// making it clickable in terminals that support OSC 8.
func writeOSC8Link(out *strings.Builder, url, visibleText string, style ansiStyle) {
out.WriteString(osc8Open)
out.WriteString(url)
out.WriteString(osc8Close)
style.renderTo(out, visibleText)
out.WriteString(osc8Open)
out.WriteString(osc8Close)
}

// skipOSC8Sequence checks if position i in s starts an OSC sequence (\x1b]...\x1b\\ or \x1b]...\x07).
// Returns the index after the String Terminator, or -1 if not an OSC sequence.
func skipOSC8Sequence(s string, i int) int {
if i+1 < len(s) && s[i] == '\x1b' && s[i+1] == ']' {
for j := i + 2; j < len(s); j++ {
if s[j] == '\x07' {
return j + 1
}
if s[j] == '\x1b' && j+1 < len(s) && s[j+1] == '\\' {
return j + 2
}
}
}
return -1
}

// withBold returns a new style with bold formatting added
// Bold is applied first, then the parent color, to prevent "bright bold" terminals
// from overriding the color when bold is enabled.
Expand Down Expand Up @@ -1722,16 +1754,21 @@ func (p *parser) renderInlineWithStyleTo(out *strings.Builder, text string, rest
if closeParen != -1 {
url := rest[:closeParen]
if linkText != url {
p.styles.ansiLinkText.renderTo(out, linkText)
writeOSC8Link(out, url, linkText, p.styles.ansiLinkText)
out.WriteByte(' ')
out.WriteString(osc8Open)
out.WriteString(url)
out.WriteString(osc8Close)
out.WriteString(p.styles.ansiLink.prefix)
out.WriteByte('(')
out.WriteString(url)
out.WriteByte(')')
out.WriteString(p.styles.ansiLink.suffix)
out.WriteString(osc8Open)
out.WriteString(osc8Close)
width += textWidth(linkText) + 1 + textWidth(url) + 2 // +1 for space, +2 for parens
} else {
p.styles.ansiLink.renderTo(out, linkText)
writeOSC8Link(out, url, linkText, p.styles.ansiLink)
width += textWidth(linkText)
}
i = i + closeBracket + 2 + closeParen + 1
Expand Down Expand Up @@ -2078,6 +2115,11 @@ func ansiStringWidth(s string) int {
width := 0
for i := 0; i < len(s); {
if s[i] == '\x1b' {
// Skip OSC sequences (e.g., \x1b]8;;...\x1b\\) - zero visual width
if end := skipOSC8Sequence(s, i); end != -1 {
i = end
continue
}
// Skip CSI sequences (e.g., \x1b[...m)
if i+1 < len(s) && s[i+1] == '[' {
i += 2
Expand Down Expand Up @@ -2410,7 +2452,15 @@ func splitWordsWithStyles(text string) []styledWord {

for i := 0; i < len(text); {
if text[i] == '\x1b' {
// Start of ANSI sequence
// Skip OSC sequences entirely (they're part of the word but have zero width)
if end := skipOSC8Sequence(text, i); end != -1 {
if wordStart == -1 {
wordStart = i
}
i = end
continue
}
// Start of CSI ANSI sequence
if wordStart == -1 {
wordStart = i
}
Expand Down Expand Up @@ -2469,6 +2519,10 @@ func splitWordsWithStyles(text string) []styledWord {
// updateActiveStyles updates the list of active ANSI styles based on new codes
func updateActiveStyles(active, newCodes []string) []string {
for _, code := range newCodes {
// Skip OSC sequences - hyperlinks should not persist across wrapped lines
if strings.HasPrefix(code, "\x1b]") {
continue
}
// Check if this is a reset sequence
if code == "\x1b[m" || code == "\x1b[0m" {
// Clear all active styles
Expand All @@ -2494,6 +2548,12 @@ func breakWord(word string, maxWidth int) []string {

for i := 0; i < len(word); {
if word[i] == '\x1b' {
// Skip OSC sequences entirely (pass through without counting width)
if end := skipOSC8Sequence(word, i); end != -1 {
current.WriteString(word[i:end])
i = end
continue
}
inAnsi = true
ansiSeq.WriteByte(word[i])
i++
Expand Down
55 changes: 54 additions & 1 deletion pkg/tui/components/markdown/fast_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// stripANSI removes ANSI escape sequences from a string.
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m|\x1b]8;[^\x07]*\x07`)

func stripANSI(s string) string {
return ansiRegex.ReplaceAllString(s, "")
Expand Down Expand Up @@ -393,6 +393,59 @@ func TestFastRendererLinks(t *testing.T) {
plain := stripANSI(result)
assert.Contains(t, plain, "this link")
assert.Contains(t, plain, "example.com")

// Verify OSC 8 hyperlink sequences are present in raw output
assert.Contains(t, result, "\x1b]8;;https://example.com\x07", "should contain OSC 8 open sequence")
assert.Contains(t, result, "\x1b]8;;\x07", "should contain OSC 8 close sequence")
}

func TestFastRendererOSC8Links(t *testing.T) {
t.Parallel()

t.Run("text differs from url", func(t *testing.T) {
t.Parallel()
r := NewFastRenderer(80)
result, err := r.Render("[Click here](https://example.com)")
require.NoError(t, err)

plain := stripANSI(result)
assert.Contains(t, plain, "Click here")
assert.Contains(t, plain, "(https://example.com)")

// Both link text and URL portion should be wrapped in OSC 8
assert.Contains(t, result, "\x1b]8;;https://example.com\x07")
assert.Contains(t, result, "\x1b]8;;\x07")
})

t.Run("text equals url", func(t *testing.T) {
t.Parallel()
r := NewFastRenderer(80)
result, err := r.Render("[https://example.com](https://example.com)")
require.NoError(t, err)

plain := stripANSI(result)
assert.Contains(t, plain, "https://example.com")

assert.Contains(t, result, "\x1b]8;;https://example.com\x07")
assert.Contains(t, result, "\x1b]8;;\x07")
})

t.Run("width calculation excludes OSC 8 sequences", func(t *testing.T) {
t.Parallel()
r := NewFastRenderer(80)
result, err := r.Render("[link](https://example.com)")
require.NoError(t, err)

lines := strings.Split(result, "\n")
for _, line := range lines {
if strings.TrimSpace(stripANSI(line)) == "" {
continue
}
lineWidth := ansiStringWidth(line)
plainWidth := runewidth.StringWidth(stripANSI(line))
assert.Equal(t, plainWidth, lineWidth, "ansiStringWidth should match plain text width for line: %q", stripANSI(line))
}
})
}

func TestFastRendererUnorderedLists(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/tui/components/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (mv *messageModel) GetSize() (width, height int) {
return mv.width, mv.height
}

var ansiEscape = regexp.MustCompile("\x1b\\[[0-9;]*m")
var ansiEscape = regexp.MustCompile("\x1b\\[[0-9;]*m|\x1b]8;[^\x07]*\x07")

func stripANSI(s string) string {
return ansiEscape.ReplaceAllString(s, "")
Expand Down