From 1586c3e628f7b3e87d37f87d0b03a8b1710a0790 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 17 Mar 2022 15:56:12 +0000 Subject: [PATCH 1/2] Use `reversed` instead of `[::-1]` to reverse in chop_cells --- rich/cells.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rich/cells.py b/rich/cells.py index dd4d5e9d0..d7adf5a04 100644 --- a/rich/cells.py +++ b/rich/cells.py @@ -113,14 +113,12 @@ 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 @@ -128,6 +126,7 @@ def chop_cells(text: str, max_size: int, position: int = 0) -> List[str]: else: total_size += size append(character) + return ["".join(line) for line in lines] From 23b266cda944be94677c1adaf21b4b76a48a55a1 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 17 Mar 2022 16:04:36 +0000 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f1a7e64f..d96c6f10a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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