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

Text wrapping edgecase #2296

Merged
merged 18 commits into from
May 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix text wrapping edge case https://github.com/Textualize/rich/pull/2296
- Allow exceptions that are raised while a Live is rendered to be displayed and/or processed https://github.com/Textualize/rich/pull/2305
- Fix crashes that can happen with `inspect` when docstrings contain some special control codes https://github.com/Textualize/rich/pull/2294

Expand Down
11 changes: 6 additions & 5 deletions rich/_wrap.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import re
from typing import Iterable, List, Tuple

from .cells import cell_len, chop_cells
from ._loop import loop_last
from .cells import cell_len, chop_cells

re_word = re.compile(r"\s*\S+\s*")

Expand All @@ -27,14 +27,15 @@ def divide_line(text: str, width: int, fold: bool = True) -> List[int]:
if line_position + word_length > width:
if word_length > width:
if fold:
for last, line in loop_last(
chop_cells(word, width, position=line_position)
):
chopped_words = chop_cells(word, max_size=width, position=0)
for last, line in loop_last(chopped_words):
if start:
append(start)

if last:
line_position = _cell_len(line)
else:
start += len(line)
append(start)
else:
if start:
append(start)
Expand Down
3 changes: 2 additions & 1 deletion rich/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def set_cell_size(text: str, total: int) -> str:
# TODO: This is inefficient
# TODO: This might not work with CWJ type characters
def chop_cells(text: str, max_size: int, position: int = 0) -> List[str]:
"""Break text in to equal (cell) length strings."""
"""Break text in to equal (cell) length strings, returning the characters in reverse
order"""
_get_character_cell_size = get_character_cell_size
characters = [
(character, _get_character_cell_size(character)) for character in text
Expand Down
Loading