Skip to content

Commit

Permalink
Fix wrapping issue
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed May 25, 2022
1 parent ca474c3 commit 0dc3985
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
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
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

0 comments on commit 0dc3985

Please sign in to comment.