Fix wrapping issue

This commit is contained in:
Darren Burns 2022-05-25 16:25:10 +01:00
parent ca474c37af
commit 0dc3985ea8
No known key found for this signature in database
GPG key ID: B0939B45037DC345
4 changed files with 22 additions and 10 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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():