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

Strip control codes in Text.append_tokens #3015

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 11 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 `<html>` tag comes before the `<head>` 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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down