diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d3c0beab..ef65c7a78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/rich/text.py b/rich/text.py index 7b32967f7..69e19b645 100644 --- a/rich/text.py +++ b/rich/text.py @@ -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)) diff --git a/tests/test_text.py b/tests/test_text.py index 18c91cffa..704462b45 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -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"