mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-11-18 03:49:50 -06:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
dfa77684ff
commit
e9c15ec95e
2 files changed
+22
-7
No files matched your search
+22
-4
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user