Skip to content

Commit

Permalink
Merge pull request #3471 from Textualize/fix-append-tokens
Browse files Browse the repository at this point in the history
Fixed exception in append_tokens
  • Loading branch information
willmcgugan authored Aug 26, 2024
2 parents e2a982d + e96ae13 commit 1b2dada
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
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"

0 comments on commit 1b2dada

Please sign in to comment.