Skip to content
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

Fixed exception in append_tokens #3471

Merged
merged 2 commits into from
Aug 26, 2024
Merged
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 @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed BrokenPipeError writing an error message https://github.com/Textualize/rich/pull/3468
- Fixed superfluous space above Markdown tables https://github.com/Textualize/rich/pull/3469
- Fixed issue with record and capture interaction https://github.com/Textualize/rich/pull/3470
- Fixed control codes breaking in `append_tokens` https://github.com/Textualize/rich/pull/3471

### Changed

Expand Down
1 change: 1 addition & 0 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ def append_tokens(
_Span = Span
offset = len(self)
for content, style in tokens:
content = strip_control_codes(content)
append_text(content)
if style:
append_span(_Span(offset, offset + len(content), style))
Expand Down
20 changes: 20 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,3 +981,23 @@ def test_extend_style():
text.extend_style(2)
assert text.plain == "foo bar "
assert text.spans == [Span(0, 3, "red"), Span(4, 9, "bold")]


def test_append_tokens() -> None:
"""Regression test for https://github.com/Textualize/rich/issues/3014"""

console = Console()
t = Text().append_tokens(
[
(
"long text that will be wrapped with a control code \r\n",
"red",
),
]
)
with console.capture() as capture:
console.print(t, width=40)

output = capture.get()
print(repr(output))
assert output == "long text that will be wrapped with a \ncontrol code \n\n"
Loading