Skip to content

Commit 1b2dada

Browse files
authored
Merge pull request #3471 from Textualize/fix-append-tokens
Fixed exception in append_tokens
2 parents e2a982d + e96ae13 commit 1b2dada

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Fixed BrokenPipeError writing an error message https://github.com/Textualize/rich/pull/3468
2222
- Fixed superfluous space above Markdown tables https://github.com/Textualize/rich/pull/3469
2323
- Fixed issue with record and capture interaction https://github.com/Textualize/rich/pull/3470
24+
- Fixed control codes breaking in `append_tokens` https://github.com/Textualize/rich/pull/3471
2425

2526
### Changed
2627

rich/text.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@ def append_tokens(
10411041
_Span = Span
10421042
offset = len(self)
10431043
for content, style in tokens:
1044+
content = strip_control_codes(content)
10441045
append_text(content)
10451046
if style:
10461047
append_span(_Span(offset, offset + len(content), style))

tests/test_text.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,3 +981,23 @@ def test_extend_style():
981981
text.extend_style(2)
982982
assert text.plain == "foo bar "
983983
assert text.spans == [Span(0, 3, "red"), Span(4, 9, "bold")]
984+
985+
986+
def test_append_tokens() -> None:
987+
"""Regression test for https://github.com/Textualize/rich/issues/3014"""
988+
989+
console = Console()
990+
t = Text().append_tokens(
991+
[
992+
(
993+
"long text that will be wrapped with a control code \r\n",
994+
"red",
995+
),
996+
]
997+
)
998+
with console.capture() as capture:
999+
console.print(t, width=40)
1000+
1001+
output = capture.get()
1002+
print(repr(output))
1003+
assert output == "long text that will be wrapped with a \ncontrol code \n\n"

0 commit comments

Comments
 (0)