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 1 commit
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
4 changes: 2 additions & 2 deletions rich/_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def divide_line(text: str, width: int, fold: bool = True) -> List[int]:
if line_position + word_length > width:
if word_length > width:
if fold:
chopped_words = chop_cells(word, width, position=line_position)
for last, line in loop_last(reversed(chopped_words)):
chopped_words = chop_cells(word, max_size=width, position=0)
for last, line in loop_last(chopped_words):
if start:
append(start)

Expand Down
2 changes: 1 addition & 1 deletion rich/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def chop_cells(text: str, max_size: int, position: int = 0) -> List[str]:
characters = [
(character, _get_character_cell_size(character)) for character in text
]
total_size = position + 1
total_size = position
darrenburns marked this conversation as resolved.
Show resolved Hide resolved
lines: List[List[str]] = [[]]
append = lines[-1].append

Expand Down
2 changes: 1 addition & 1 deletion tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,5 @@ def test_refresh_screen():
result = capture.get()
print()
print(repr(result))
expected = "\x1b[1;1H\x1b[34m╭─\x1b[0m\x1b[34m \x1b[0m\x1b[32m'foo'\x1b[0m\x1b[34m─╮\x1b[0m\x1b[2;1H\x1b[34m│\x1b[0m \x1b[1;35mLa\x1b[0m \x1b[34m│\x1b[0m\x1b[3;1H\x1b[34m│\x1b[0m \x1b[1;35myout\x1b[0m\x1b[1m(\x1b[0m \x1b[34m│\x1b[0m\x1b[4;1H\x1b[34m│\x1b[0m \x1b[34m│\x1b[0m\x1b[5;1H\x1b[34m╰────────╯\x1b[0m"
expected = "\x1b[1;1H\x1b[34m╭─\x1b[0m\x1b[34m \x1b[0m\x1b[32m'foo'\x1b[0m\x1b[34m─╮\x1b[0m\x1b[2;1H\x1b[34m│\x1b[0m \x1b[1;35mLayout\x1b[0m \x1b[34m│\x1b[0m\x1b[3;1H\x1b[34m│\x1b[0m \x1b[1m(\x1b[0m \x1b[34m│\x1b[0m\x1b[4;1H\x1b[34m│\x1b[0m \x1b[33mna\x1b[0m \x1b[34m│\x1b[0m\x1b[5;1H\x1b[34m╰────────╯\x1b[0m"
assert result == expected
24 changes: 18 additions & 6 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,26 @@ def test_wrap_overflow_long():


def test_wrap_long_words():
text = Text("X 123456789")
text = Text("XX 12345678912")
lines = text.wrap(Console(), 4)

assert len(lines) == 4
assert lines[0] == Text("X ")
assert lines[1] == Text("1234")
assert lines[2] == Text("5678")
assert lines[3] == Text("9")
assert lines._lines == [
Text("XX "),
Text("1234"),
Text("5678"),
Text("912"),
]


def test_wrap_long_words_2():
# https://github.com/Textualize/rich/issues/2273
text = Text("Hello, World...123")
lines = text.wrap(Console(), 10)
assert lines._lines == [
Text("Hello, "),
Text("World...12"),
Text("3"),
]


def test_wrap_long_words_justify_left():
Expand Down