Skip to content

Commit 6640057

Browse files
committed
fix fold
1 parent 7a5bc20 commit 6640057

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/textual/content.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -981,12 +981,12 @@ def fold(self, width: int) -> list[Content]:
981981
List of content instances.
982982
"""
983983
if not self:
984-
return []
984+
return [self]
985985
text = self.plain
986986
lines: list[Content] = []
987987
position = 0
988988
width = max(width, 2)
989-
while text:
989+
while True:
990990
snip = text[position : position + width]
991991
if not snip:
992992
break
@@ -998,7 +998,6 @@ def fold(self, width: int) -> list[Content]:
998998
if snip_cell_length == width:
999999
# Cell length is exactly width
10001000
lines.append(self[position : position + width])
1001-
text = text[len(snip) :]
10021001
position += len(snip)
10031002
continue
10041003
# TODO: Can this be more efficient?

tests/test_content.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,25 @@ def test_wrap() -> None:
385385
@pytest.mark.parametrize(
386386
"content, width, expected",
387387
[
388+
(
389+
Content("111222333"),
390+
3,
391+
[
392+
Content("111"),
393+
Content("222"),
394+
Content("333"),
395+
],
396+
),
397+
(
398+
Content("1112223334"),
399+
3,
400+
[
401+
Content("111"),
402+
Content("222"),
403+
Content("333"),
404+
Content("4"),
405+
],
406+
),
388407
(
389408
Content(""),
390409
10,
@@ -445,16 +464,16 @@ def test_wrap() -> None:
445464
[
446465
Content("💩H"),
447466
Content.from_markup("[b]ell"),
448-
Content.from_markup("[b]o[/]"),
467+
Content.from_markup("o"),
449468
],
450469
),
451470
(
452-
Content.from_markup("💩H[b]ell[/]💩"),
471+
Content.from_markup("💩H[b]ell[/]o💩"),
453472
3,
454473
[
455474
Content("💩H"),
456475
Content.from_markup("[b]ell"),
457-
Content.from_markup("[b]o[/]💩"),
476+
Content.from_markup("o💩"),
458477
],
459478
),
460479
(
@@ -548,5 +567,6 @@ def test_fold(content: Content, width: int, expected: list[Content]) -> None:
548567
"""
549568
result = content.fold(width)
550569
assert isinstance(result, list)
570+
assert len(result) == len(expected)
551571
for line, expected_line in zip(result, expected):
552572
assert line.is_same(expected_line)

0 commit comments

Comments
 (0)