Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Fixed `Text.from_ansi` CRLF handling https://github.com/Textualize/rich/issues/4090

## [15.0.0] - 2026-04-12

### Changed
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ The following people have contributed to the development of Rich:
- [Aaron Stephens](https://github.com/aaronst)
- [Karolina Surma](https://github.com/befeleme)
- [Gabriele N. Tornetta](https://github.com/p403n1x87)
- [Kevin Van Brunt](https://github.com/kmvanbrunt)
- [Nils Vu](https://github.com/nilsvu)
- [Arian Mollik Wasi](https://github.com/wasi-master)
- [Jan van Wijk](https://github.com/jdvanwijk)
Expand Down
2 changes: 1 addition & 1 deletion rich/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def decode(self, terminal_text: str) -> Iterable[Text]:
Text: Marked up Text.
"""
for line in re.split(r"(?<=\n)", terminal_text):
yield self.decode_line(line.rstrip("\n"))
yield self.decode_line(line.rstrip("\r\n"))

def decode_line(self, line: str) -> Text:
"""Decode a line containing ansi codes.
Expand Down
40 changes: 28 additions & 12 deletions tests/test_ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,35 @@ def test_strip_private_escape_sequences(code):
assert capture.get() == expected


def test_decode_newlines():
"""Test newlines are preserved.
@pytest.mark.parametrize("line_break", ["\n", "\r\n"])
def test_decode_line_breaks(line_break: str) -> None:
"""Test line breaks are preserved and converted to newlines.
Regression test for https://github.com/Textualize/rich/issues/3577
"""
assert Text.from_ansi("").plain == ""
assert Text.from_ansi("\n").plain == "\n"
assert Text.from_ansi("\n\n").plain == "\n\n"
assert Text.from_ansi(f"{line_break}").plain == "\n"
assert Text.from_ansi(f"{line_break}{line_break}").plain == "\n\n"
assert Text.from_ansi("Hello").plain == "Hello"
assert Text.from_ansi("\nHello").plain == "\nHello"
assert Text.from_ansi("Hello\n").plain == "Hello\n"
assert Text.from_ansi("Hello\n\n").plain == "Hello\n\n"
assert Text.from_ansi("Hello\nWorld").plain == "Hello\nWorld"
assert Text.from_ansi("Hello\n\nWorld").plain == "Hello\n\nWorld"
assert Text.from_ansi("Hello\nWorld\n").plain == "Hello\nWorld\n"
assert Text.from_ansi("Hello\nWorld\n\n").plain == "Hello\nWorld\n\n"
assert Text.from_ansi("\nHello\nWorld\n\n").plain == "\nHello\nWorld\n\n"
assert Text.from_ansi(f"{line_break}Hello").plain == "\nHello"
assert Text.from_ansi(f"Hello{line_break}").plain == "Hello\n"
assert Text.from_ansi(f"Hello{line_break}{line_break}").plain == "Hello\n\n"
assert Text.from_ansi(f"Hello{line_break}World").plain == "Hello\nWorld"
assert (
Text.from_ansi(f"Hello{line_break}{line_break}World").plain == "Hello\n\nWorld"
)
assert (
Text.from_ansi(f"Hello{line_break}World{line_break}").plain == "Hello\nWorld\n"
)
assert (
Text.from_ansi(f"Hello{line_break}World{line_break}{line_break}").plain
== "Hello\nWorld\n\n"
)
assert (
Text.from_ansi(
f"{line_break}Hello{line_break}World{line_break}{line_break}"
).plain
== "\nHello\nWorld\n\n"
)

# Include a mixture of line break types
assert Text.from_ansi(f"Hello{line_break}\n\r\nWorld").plain == "Hello\n\n\nWorld"