Skip to content

feat(markdown): render trusted SGR styling and replay it across wraps - #4949

Open
rumpl wants to merge 1 commit into
rumpl/markdown-strip-terminal-controlsfrom
rumpl/markdown-trusted-sgr
Open

rumpl wants to merge 1 commit into
rumpl/markdown-strip-terminal-controlsfrom
rumpl/markdown-trusted-sgr

Conversation

@rumpl

@rumpl rumpl commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds AllowSGR to the fast and incremental Markdown renderers, for trusted input such as display transformer output (#4952). SGR sequences are kept; every other escape is still stripped. Inline parsing, link labels and width calculations skip embedded sequences.

Wrapping now tracks active styling as one value per SGR attribute, including compound (1;38;2;r;g;b) and colon-form (38:2::r:g:b) codes, and replays it as one sequence at each break. Replay stays bounded however many codes a span holds. This also fixes wraps that lost styling, all reproducible on main with renderer-generated styles:

  • the no-space fast path did not replay styles across breaks;
  • a wrapped word's own codes were replayed before it, so bold and strikethrough could be closed early;
  • style-only runs took a column and could wrap onto an otherwise empty row.

Second of a stack, on top of #4948:

  1. fix(markdown): strip terminal controls from rendered text #4948: strip terminal controls
  2. This PR: render trusted SGR styling
  3. feat: add display-only Markdown transformers #4952: display-only Markdown transformers
  4. feat(leantui): apply display-only Markdown transformers #4953: Markdown transformers in the lean TUI

AllowSGR has no caller until #4952.

Validation

  • task build; race tests for pkg/tui/... and pkg/leantui/...
  • golangci-lint reports no new issues

@rumpl
rumpl requested a review from a team as a code owner November 17, 2026 22:24
@aheritier aheritier added area/tui For features/issues/fixes related to the TUI kind/feat PR adds a new feature (maps to feat:). Use on PRs only. labels Nov 17, 2026
@rumpl
rumpl added this pull request to stack #4954 November 17, 2026 22:41
@rumpl
rumpl force-pushed the rumpl/markdown-trusted-sgr branch from 9f9c89d to 45be8ce Compare November 17, 2026 22:47
Copilot AI balanced review requested due to automatic review settings November 17, 2026 22:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active issues in SGR handling break fenced-code wrapping and tab alignment, lose styling across wraps and inline runs, and truncate styled URL hyperlink targets.

4 open issues
What changed in this PR

Adds opt-in trusted SGR styling to the Markdown renderers, preparing them for display-only transformers while continuing to strip other terminal controls.

Changes:

  • Adds AllowSGR to fast and incremental rendering, preserving renderer options across width changes.
  • Introduces bounded per-attribute style replay across wraps and makes inline parsing and width calculations escape-aware.
  • Adds regression tests for sanitization, style replay, wrapping, link labels, and incremental rendering.
File Description
pkg/​tui/​components/​markdown/​sgr.go Tracks SGR attributes and replays active styles at line breaks.
pkg/​tui/​components/​markdown/​sanitize.go Optionally preserves SGR while stripping other terminal controls.
pkg/​tui/​components/​markdown/​incremental.go Adds trusted styling and preserves options across width changes.
pkg/​tui/​components/​markdown/​fast_renderer.go Integrates trusted styling and updates inline parsing, measurement, and wrapping.
pkg/​tui/​components/​markdown/​ansi_test.go Tests embedded styling, sanitization, wrapping, and renderer consistency.

Review effort: Balanced

}

input = sanitizeForTerminal(input)
input = sanitizeForTerminal(input, r.allowSGR)
Comment on lines +1833 to +1836
// Transformer styling stays inside the run it decorates.
if text[i] == '\x1b' {
i, _ = scanAnsiSequence(text, i)
continue
Comment on lines +1833 to +1836
// Transformer styling stays inside the run it decorates.
if text[i] == '\x1b' {
i, _ = scanAnsiSequence(text, i)
continue
out.WriteByte('\n')
return
}
out.WriteString("\x1b[m\n\x1b[")
Add AllowSGR to the fast and incremental renderers, for trusted input
such as display transformer output: SGR sequences are kept, every other
escape is still stripped. Inline parsing, link labels and width
calculations skip embedded sequences.

Wrapping now tracks active styling as one value per SGR attribute,
including compound and colon-form codes, and replays it as a single
sequence at each break. This keeps replay bounded however many codes a
span holds, and fixes wraps that lost styling: the no-space fast path
did not replay styles, a word's own codes were replayed before it, and
style-only runs took a column and could wrap onto an empty row.

Signed-off-by: Djordje Lukic <[email protected]>
@rumpl
rumpl force-pushed the rumpl/markdown-trusted-sgr branch from 45be8ce to 8d26c16 Compare November 17, 2026 22:58
Copilot AI balanced review requested due to automatic review settings November 17, 2026 22:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Fenced-code wrapping can split and corrupt preserved SGR escapes, while inline style transitions and incomplete SGR state tracking cause valid styling to be lost.

5 open issues

Review effort: Balanced

// Fast path: check if text contains any markdown characters or URLs
// If not, apply the restore style directly and return
firstMarker := strings.IndexAny(text, inlineMarkdownChars)
firstMarker := indexInlineMarker(text)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tui For features/issues/fixes related to the TUI kind/feat PR adds a new feature (maps to feat:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants