From e9c15ec95e1f36a0e5e307fa823a2cd95be2882c Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 15 Nov 2026 15:49:27 +0100 Subject: [PATCH] Ignore hyperlinks' escape sequences when wrapping text for a view WrapViewLinesToWidth only recognized CSI escape sequences, which lazygit uses for colors. It counted the OSC 8 escape sequences of hyperlinks, including their URLs, towards the width of a line. Recognize OSC sequences too; they end with BEL or with ESC \. Co-Authored-By: Claude Opus 5.5 --- pkg/utils/lines.go | 26 ++++++++++++++++++++++---- pkg/utils/lines_test.go | 3 --- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pkg/utils/lines.go b/pkg/utils/lines.go index 598dede7f..73ae8fd25 100644 --- a/pkg/utils/lines.go +++ b/pkg/utils/lines.go @@ -109,11 +109,14 @@ const ( notInEscapeSequence escapeSequenceState = iota afterEscapeChar inCSISequence + inOSCSequence + afterEscapeCharInOSCSequence ) // Recognizes ANSI escape sequences while iterating over the characters of a -// string. Only CSI sequences (ESC [ ... final byte) are recognized; these are -// the color and style codes that lazygit puts into view content. +// string. It recognizes CSI sequences (ESC [ ... final byte), which are the +// color and style codes that lazygit puts into view content, and OSC sequences +// (ESC ] ... BEL or ESC \), which lazygit uses for hyperlinks. type escapeSequenceTracker struct { state escapeSequenceState } @@ -123,9 +126,12 @@ type escapeSequenceTracker struct { func (self *escapeSequenceTracker) consume(chr rune) bool { switch self.state { case afterEscapeChar: - if chr == '[' { + switch chr { + case '[': self.state = inCSISequence - } else { + case ']': + self.state = inOSCSequence + default: self.state = notInEscapeSequence } return true @@ -136,6 +142,18 @@ func (self *escapeSequenceTracker) consume(chr rune) bool { self.state = notInEscapeSequence } return true + case inOSCSequence: + switch chr { + case '\x07': + self.state = notInEscapeSequence + case '\x1b': + self.state = afterEscapeCharInOSCSequence + } + return true + case afterEscapeCharInOSCSequence: + // The backslash of the string terminator (ESC \) + self.state = notInEscapeSequence + return true default: if chr == '\x1b' { self.state = afterEscapeChar diff --git a/pkg/utils/lines_test.go b/pkg/utils/lines_test.go index f3e5be296..376f5e687 100644 --- a/pkg/utils/lines_test.go +++ b/pkg/utils/lines_test.go @@ -492,9 +492,6 @@ func TestWrapViewLinesToWidthWithHyperlink(t *testing.T) { text := "Hello \x1b]8;;https://example.com\x1b\\big\x1b]8;;\x1b\\ world" wrappedLines, _, _ := WrapViewLinesToWidth(true, false, text, 9, 4) assert.Equal(t, - /* EXPECTED: []string{"Hello \x1b]8;;https://example.com\x1b\\big\x1b]8;;\x1b\\", "world"}, - ACTUAL: */ - []string{"Hello", "\x1b]8;;https:", "//example", ".com\x1b\\big\x1b]8;", ";\x1b\\ world"}, wrappedLines) }