diff --git a/rich/_wrap.py b/rich/_wrap.py index 799451c6e..c45f193f7 100644 --- a/rich/_wrap.py +++ b/rich/_wrap.py @@ -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) diff --git a/rich/cells.py b/rich/cells.py index af8c7c8bd..834c37103 100644 --- a/rich/cells.py +++ b/rich/cells.py @@ -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 diff --git a/tests/test_layout.py b/tests/test_layout.py index b297123f8..46f496b0c 100644 --- a/tests/test_layout.py +++ b/tests/test_layout.py @@ -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 diff --git a/tests/test_text.py b/tests/test_text.py index 4696ff7a1..685bdcdda 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -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():