Skip to content

fix(viewport): correct highlight offsets on ANSI-styled content - #1050

Open
rksharma-owg wants to merge 1 commit into
charmbracelet:mainfrom
rksharma-owg:fix/viewport-highlight-ansi
Open

rksharma-owg wants to merge 1 commit into
charmbracelet:mainfrom
rksharma-owg:fix/viewport-highlight-ansi

Conversation

@rksharma-owg

Copy link
Copy Markdown

Fixes #1049

Problem

viewport.SetHighlights misplaced highlight ranges when content contained ANSI escape sequences (e.g., color styling). The ranges would shift to the right by the number of ANSI escape bytes preceding the match, or disappear entirely.

Root cause: in viewport/highlight.go, parseMatches walked graphemes on ansi.Strip(content), but treated byteStart and byteEnd (which are measured against the raw content) as if they were stripped byte offsets. It also performed newline checks using content[bytePos] == '\n' where bytePos was indexed into the stripped string, leading to coordinate desynchronization.

Fix

  • Iterate through raw content using ansi.DecodeSequence. This accurately consumes escape sequence bytes in bytePos (tracking the raw content byte coordinates matching matches), while only adding visual cell widths to graphemePos.
  • Remove now-unused uniseg import from viewport/highlight.go.
  • Add test coverage in viewport/viewport_test.go with TestMatchesToHighlights_ANSI verifying single-line ANSI prefix, multi-line styled content, multi-line spanning, and the exact reproducer from issue viewport: SetHighlights misplaces ranges on ANSI-carrying content聽#1049.

Verification

  • go test -v ./viewport passes cleanly
  • go test ./... passes cleanly

Use ansi.DecodeSequence to iterate through raw content, counting escape sequence bytes in the raw byte offset while preserving true visual column widths.

Fixes charmbracelet#1049
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

viewport: SetHighlights misplaces ranges on ANSI-carrying content

1 participant