Skip to content

Fixed issue where Text.from_ansi() was removing ending line break. #3793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed extraction of recursive exceptions https://github.com/Textualize/rich/pull/3772
- Fixed padding applied to Syntax https://github.com/Textualize/rich/pull/3782
- Fixed `Panel` title missing the panel background style https://github.com/Textualize/rich/issues/3569
- Fixed issue where `Text.from_ansi()` was removing ending line break. https://github.com/Textualize/rich/issues/3577

### Added

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ The following people have contributed to the development of Rich:
- [Jonathan Helmus](https://github.com/jjhelmus)
- [Brandon Capener](https://github.com/bcapener)
- [Alex Zheng](https://github.com/alexzheng111)
- [Kevin Van Brunt](https://github.com/kmvanbrunt)
20 changes: 20 additions & 0 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,26 @@ def from_ansi(
)
decoder = AnsiDecoder()
result = joiner.join(line for line in decoder.decode(text))

# AnsiDecoder.decode() uses str.splitlines(), which discards trailing line break characters.
# If 'text' ends with one, restore the missing newline to 'result'.
# Note: '\r\n' is handled as its last character is '\n'.
# Source: https://docs.python.org/3/library/stdtypes.html#str.splitlines
line_break_chars = {
"\n", # Line Feed
"\r", # Carriage Return
"\v", # Vertical Tab
"\f", # Form Feed
"\x1c", # File Separator
"\x1d", # Group Separator
"\x1e", # Record Separator
"\x85", # Next Line (NEL)
"\u2028", # Line Separator
"\u2029", # Paragraph Separator
}
if text and text[-1] in line_break_chars:
result.append("\n")

return result

@classmethod
Expand Down
41 changes: 39 additions & 2 deletions tests/test_ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,43 @@ def test_decode():
assert lines == expected


def test_from_ansi_ending_newline():
"""Test that ending line breaks are not removed but are restored as newlines."""
# Line break characters recognized by str.splitlines()
# Source: https://docs.python.org/3/library/stdtypes.html#str.splitlines
line_break_chars = {
"\n", # Line Feed
"\r", # Carriage Return
"\v", # Vertical Tab
"\f", # Form Feed
"\x1c", # File Separator
"\x1d", # Group Separator
"\x1e", # Record Separator
"\x85", # Next Line (NEL)
"\u2028", # Line Separator
"\u2029", # Paragraph Separator
}

# Test single-character line breaks
for c in line_break_chars:
input_string = f"Text{c}"
expected_output = input_string.replace(c, "\n")
assert Text.from_ansi(input_string).plain == expected_output

# Test '\r\n'
input_string = "Text\r\n"
expected_output = input_string.replace("\r\n", "\n")
assert Text.from_ansi(input_string).plain == expected_output

# Test string without ending line break
input_string = "No line break"
assert Text.from_ansi(input_string).plain == input_string

# Test empty string
input_string = ""
assert Text.from_ansi(input_string).plain == input_string


def test_decode_example():
ansi_bytes = b"\x1b[01m\x1b[KC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:\x1b[m\x1b[K In function '\x1b[01m\x1b[Kmain\x1b[m\x1b[K':\n\x1b[01m\x1b[KC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:3:5:\x1b[m\x1b[K \x1b[01;35m\x1b[Kwarning: \x1b[m\x1b[Kunused variable '\x1b[01m\x1b[Ka\x1b[m\x1b[K' [\x1b[01;35m\x1b[K-Wunused-variable\x1b[m\x1b[K]\n 3 | int \x1b[01;35m\x1b[Ka\x1b[m\x1b[K=1;\n | \x1b[01;35m\x1b[K^\x1b[m\x1b[K\n"
ansi_text = ansi_bytes.decode("utf-8")
Expand All @@ -45,7 +82,7 @@ def test_decode_example():
console.print(text)
result = capture.get()
print(repr(result))
expected = "\x1b[1mC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:\x1b[0m In function '\x1b[1mmain\x1b[0m':\n\x1b[1mC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:3:5:\x1b[0m \x1b[1;35mwarning: \x1b[0munused variable '\x1b[1ma\x1b[0m' \n[\x1b[1;35m-Wunused-variable\x1b[0m]\n 3 | int \x1b[1;35ma\x1b[0m=1;\n | \x1b[1;35m^\x1b[0m\n"
expected = "\x1b[1mC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:\x1b[0m In function '\x1b[1mmain\x1b[0m':\n\x1b[1mC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:3:5:\x1b[0m \x1b[1;35mwarning: \x1b[0munused variable '\x1b[1ma\x1b[0m' \n[\x1b[1;35m-Wunused-variable\x1b[0m]\n 3 | int \x1b[1;35ma\x1b[0m=1;\n | \x1b[1;35m^\x1b[0m\n\n"
assert result == expected


Expand All @@ -55,7 +92,7 @@ def test_decode_example():
# https://github.com/Textualize/rich/issues/2688
(
b"\x1b[31mFound 4 errors in 2 files (checked 18 source files)\x1b(B\x1b[m\n",
"Found 4 errors in 2 files (checked 18 source files)",
"Found 4 errors in 2 files (checked 18 source files)\n",
),
# https://mail.python.org/pipermail/python-list/2007-December/424756.html
(b"Hallo", "Hallo"),
Expand Down