diff --git a/CHANGELOG.md b/CHANGELOG.md index fda5b5199..57b14cadb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 `IndexError` in `Text.append_tokens` when passed a text that contains control codes https://github.com/Textualize/rich/issues/3014 + ## [13.5.3] - 2023-09-17 ### Fixed @@ -31,7 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed Text.expand_tabs not expanding spans. - Fixed TimeElapsedColumn from showing negative. - Fix for escaping strings with a trailing backslash https://github.com/Textualize/rich/issues/2987 -- Fixed exception in Markdown with partial table https://github.com/Textualize/rich/issues/3053 +- Fixed exception in Markdown with partial table https://github.com/Textualize/rich/issues/3053 - Fixed the HTML export template so that the `` tag comes before the `` tag https://github.com/Textualize/rich/issues/3021 - Fixed issue with custom classes overwriting `__eq__` https://github.com/Textualize/rich/issues/2875 - Fix rich.pretty.install breakage in iPython https://github.com/Textualize/rich/issues/3013 @@ -2004,10 +2010,10 @@ Major version bump for a breaking change to `Text.stylize signature`, which corr [13.5.3]: https://github.com/textualize/rich/compare/v13.5.2...v13.5.3 [13.5.2]: https://github.com/textualize/rich/compare/v13.5.1...v13.5.2 [13.5.1]: https://github.com/textualize/rich/compare/v13.5.0...v13.5.1 -[13.5.0]: https://github.com/textualize/rich/compare/v13.4.2...v13.5.0 -[13.4.2]: https://github.com/textualize/rich/compare/v13.4.1...v13.4.2 -[13.4.1]: https://github.com/textualize/rich/compare/v13.4.0...v13.4.1 -[13.4.0]: https://github.com/textualize/rich/compare/v13.3.5...v13.4.0 +[13.5.0]: https://github.com/textualize/rich/compare/v13.4.2...v13.5.0 +[13.4.2]: https://github.com/textualize/rich/compare/v13.4.1...v13.4.2 +[13.4.1]: https://github.com/textualize/rich/compare/v13.4.0...v13.4.1 +[13.4.0]: https://github.com/textualize/rich/compare/v13.3.5...v13.4.0 [13.3.5]: https://github.com/textualize/rich/compare/v13.3.4...v13.3.5 [13.3.4]: https://github.com/textualize/rich/compare/v13.3.3...v13.3.4 [13.3.3]: https://github.com/textualize/rich/compare/v13.3.2...v13.3.3 diff --git a/rich/text.py b/rich/text.py index 90b1cb14b..313741513 100644 --- a/rich/text.py +++ b/rich/text.py @@ -1036,10 +1036,11 @@ def append_tokens( _Span = Span offset = len(self) for content, style in tokens: - append_text(content) + sanitized_content = strip_control_codes(content) + append_text(sanitized_content) if style: - append_span(_Span(offset, offset + len(content), style)) - offset += len(content) + append_span(_Span(offset, offset + len(sanitized_content), style)) + offset += len(sanitized_content) self._length = offset return self diff --git a/tests/test_text.py b/tests/test_text.py index 22c34e442..9e8cfb750 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -315,6 +315,22 @@ def test_append_text(): assert text._spans == [Span(3, 6, "bold")] +def test_append_tokens(): + text = Text().append_tokens( + [("long text that will be wrapped with a control code \r\n.", "bold")] + ) + assert str(text) == "long text that will be wrapped with a control code \n." + assert text._spans == [Span(0, 53, "bold")] + + # Make sure it does not crash (Issue #3014) + console = Console(width=40, file=StringIO()) + console.print(text) + assert ( + console.file.getvalue() + == "long text that will be wrapped with a \ncontrol code \n.\n" + ) + + def test_end(): console = Console(width=20, file=StringIO()) text = Group(Text.from_markup("foo", end=" "), Text.from_markup("bar"))