Skip to content

Commit

Permalink
Merge pull request #2077 from Textualize/chop-cells
Browse files Browse the repository at this point in the history
Use `reversed` instead of `[::-1]` to reverse in chop_cells
  • Loading branch information
willmcgugan authored Mar 18, 2022
2 parents 6f5c6d5 + 23b266c commit b24aa54
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit b24aa54

Please sign in to comment.