fix(ansi): handle CRLF line endings in Text.from_ansi#4099
Closed
Alm0stSurely wants to merge 1 commit intoTextualize:masterfrom
Closed
fix(ansi): handle CRLF line endings in Text.from_ansi#4099Alm0stSurely wants to merge 1 commit intoTextualize:masterfrom
Alm0stSurely wants to merge 1 commit intoTextualize:masterfrom
Conversation
Fixes Textualize#4090 The AnsiDecoder.decode() method splits input on \n and then calls decode_line() which strips \n and uses rsplit('\r', 1)[-1] to handle carriage returns. When the input contains CRLF (\r\n), the split preserves the \r in the line content, and then rsplit('\r', 1)[-1] takes the empty string after the final \r, discarding all text. This fix normalizes \r\n to \n before splitting, ensuring that CRLF sequences are treated as plain line endings rather than terminal carriage returns that overwrite content. Added test_decode_crlf to cover CRLF, mixed line endings, and consecutive CRLF sequences.
Member
|
Your PR has been closed due to a AI policy violation. Please read the following before submitting further PRs. https://github.com/Textualize/textual/blob/main/AI_POLICY.md |
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.
Problem
Fixes #4090.
When
Text.from_ansiis given input with CRLF (\r\n) line endings, all text content is lost and only empty lines remain.Reproduction:
Analysis
The root cause is an interaction between line splitting and carriage-return handling:
AnsiDecoder.decode()splits on\nusingre.split(r"(?<=\n)", ...), keeping the\nattached to the preceding line.\n, a line that originally ended with\r\nbecomes...\r.decode_line()then callsline.rsplit("\r", 1)[-1]to simulate terminal carriage-return behavior (where\rmoves the cursor to the start of the line, effectively overwriting prior content).\r(from CRLF),rsplit("\r", 1)[-1]returns the empty string after the final\r, discarding all content.Solution
Normalize
\r\nto\nindecode()before splitting. This treats CRLF as a plain line-ending sequence rather than a terminal carriage-return that overwrites content.The fix is a single line:
This preserves the existing behavior of standalone
\r(used in progress bars and terminal updates) while correctly handling CRLF line endings common on Windows and in network protocols.Benchmarks / Verification
test_decode_crlfcovering:"Hello\r\nWorld\r\n""\r\nHello\r\n""Hello\r\n\r\nWorld""Hello\nWorld\r\n"and"Hello\r\nWorld\n"Notes
\r(without following\n) continues to work as before, since the replace only targets the\r\nsequence.