Preserve trailing ANSI reset codes in ansi_trim_end - #957
Open
MsfPablo wants to merge 2 commits into
Open
Conversation
ansi_trim_end() measured the visible width of the trimmed row and then called truncate() with that width. truncate() stops as soon as the width budget is exhausted, so any escape sequence sitting after the trimmed padding was discarded along with the spaces. Rows are built by wrapping each padded column in apply_color(), which places the reset code at the very end of the styled span. Trimming therefore removed the reset that closed the last column, leaving an unterminated SGR sequence on every line: the final row's color bleeds into whatever is printed next (e.g. the shell prompt). Trim the trailing whitespace directly instead of going through truncate(), keeping every escape sequence that follows it. Verified with 'procs --color=always': no trailing whitespace, and no line ends with an unterminated SGR sequence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
While looking into #769 (extraneous trailing spaces with
--color=always) I found that the reported symptom is already fixed onmasterby bb7d47c (Fix double-spaced TTY output caused by ANSI-unaware trim_end, for #848) — colored output no longer has trailing spaces. #769 can probably be closed.That fix, however, left a related defect behind.
Problem
ansi_trim_end()computes the visible width of the trimmed row and then callstruncate()with that width.truncate()stops as soon as the width budget is exhausted, so any escape sequence that sits after the trimmed padding is discarded along with the spaces.Rows are assembled by wrapping each padded column in
apply_color(), which puts the reset code at the very end of the styled span. Trimming therefore removes the reset that closed the last column, leaving an unterminated SGR sequence on every line — the final row's color bleeds into whatever is printed next (typically the shell prompt).On
master:Reduced:
Fix
Trim the trailing whitespace directly rather than routing through
truncate(), keeping every escape sequence that follows the trimmed run. Behavior for the whitespace itself is unchanged, so the #848/#769 fix is preserved.Tests
Adds
src/util.rsunit tests foransi_trim_end: plain strings, padding inside a styled span, reset-code preservation, multiple styled columns, whitespace-only input, and wide/box-drawing characters.5 of the 6 fail on
masterand pass with this change:Verification
End-to-end assertion over
procs --color=alwaysoutput — 12 lines, 0 with trailing whitespace, 0 with an unterminated SGR sequence.Tested on macOS (aarch64).
Disclosure: this patch was written with AI assistance (Claude). I reviewed the diff, ran the test/fmt/clippy commands above myself, and isolated the pre-existing clippy findings against a clean baseline.