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

Use reversed instead of [::-1] to reverse in chop_cells #2077

Merged
merged 3 commits into from Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Improve performance of cell_length https://github.com/Textualize/rich/pull/2061
- Improve performance of chop_cells https://github.com/Textualize/rich/pull/2077

## [12.0.1] - 2022-03-14

Expand Down
7 changes: 3 additions & 4 deletions rich/cells.py
Expand Up @@ -113,21 +113,20 @@ def chop_cells(text: str, max_size: int, position: int = 0) -> List[str]:
_get_character_cell_size = get_character_cell_size
characters = [
(character, _get_character_cell_size(character)) for character in text
][::-1]
]
total_size = position
lines: List[List[str]] = [[]]
append = lines[-1].append

pop = characters.pop
while characters:
character, size = pop()
for character, size in reversed(characters):
if total_size + size > max_size:
lines.append([character])
append = lines[-1].append
total_size = size
else:
total_size += size
append(character)

return ["".join(line) for line in lines]


Expand Down