Skip to content

Commit 0835280

Browse files
authored
Merge pull request #6038 from Textualize/expand-tabs-fix
fix for expand tabs
2 parents d4cf716 + a8e8480 commit 0835280

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

src/textual/content.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,7 @@ def styled(
334334
"""
335335
if not text:
336336
return Content("")
337-
span_length = cell_len(text) if cell_length is None else cell_length
338-
new_content = cls(
339-
text, [Span(0, span_length, style)] if style else None, span_length
340-
)
337+
new_content = cls(text, [Span(0, len(text), style)] if style else None)
341338
return new_content
342339

343340
@classmethod
@@ -844,6 +841,8 @@ def iter_content() -> Iterable[Content]:
844841
total_cell_length: int | None = self._cell_length
845842

846843
for content in iter_content():
844+
if not content:
845+
continue
847846
extend_text(content._text)
848847
extend_spans(
849848
_Span(offset + start, offset + end, style)
@@ -1437,6 +1436,9 @@ def expand_tabs(self, tab_size: int = 8) -> Content:
14371436
if "\t" not in self.plain:
14381437
return self
14391438

1439+
if not self._spans:
1440+
return Content(self.plain.expandtabs(tab_size))
1441+
14401442
new_text: list[Content] = []
14411443
append = new_text.append
14421444

@@ -1449,7 +1451,7 @@ def expand_tabs(self, tab_size: int = 8) -> Content:
14491451
for part in parts:
14501452
if part.plain.endswith("\t"):
14511453
part = Content(
1452-
part._text[-1][:-1] + " ", part._spans, part._cell_length
1454+
part._text[:-1] + " ", part._spans, part._cell_length
14531455
)
14541456
cell_position += part.cell_length
14551457
tab_remainder = cell_position % tab_size

src/textual/style.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
)
4242

4343

44-
@rich.repr.auto(angular=True)
44+
@rich.repr.auto()
4545
@dataclass(frozen=True)
4646
class Style:
4747
"""Represents a style in the Visual interface (color and other attributes).

tests/test_content.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,47 @@ def test_simplify():
294294
assert content.spans == [Span(0, 3, "bold"), Span(3, 6, "bold")]
295295
content.simplify()
296296
assert content.spans == [Span(0, 6, "bold")]
297+
298+
299+
@pytest.mark.parametrize(
300+
["input", "tab_width", "expected"],
301+
[
302+
(Content(""), 8, Content("")),
303+
(Content("H"), 8, Content("H")),
304+
(Content("Hello"), 8, Content("Hello")),
305+
(Content("\t"), 8, Content(" " * 8)),
306+
(Content("A\t"), 8, Content("A" + " " * 7)),
307+
(Content("ABCD\t"), 8, Content("ABCD" + " " * 4)),
308+
(Content("ABCDEFG\t"), 8, Content("ABCDEFG ")),
309+
(Content("ABCDEFGH\t"), 8, Content("ABCDEFGH" + " " * 8)),
310+
(Content("Hel\tlo!"), 4, Content("Hel lo!")),
311+
(Content("\t\t"), 4, Content(" " * 8)),
312+
(Content("FO\t\t"), 4, Content("FO ")),
313+
(Content("FO\tOB\t"), 4, Content("FO OB ")),
314+
(
315+
Content("FOO", spans=[Span(0, 3, "red")]),
316+
4,
317+
Content("FOO", spans=[Span(0, 3, "red")]),
318+
),
319+
(
320+
Content("FOO\tBAR", spans=[Span(0, 3, "red")]),
321+
8,
322+
Content("FOO BAR", spans=[Span(0, 3, "red")]),
323+
),
324+
(
325+
Content("FOO\tBAR", spans=[Span(0, 3, "red"), Span(4, 8, "blue")]),
326+
8,
327+
Content("FOO BAR", spans=[Span(0, 3, "red"), Span(8, 11, "blue")]),
328+
),
329+
(
330+
Content("foo\tbar\nbaz", spans=[Span(0, 11, "red")]),
331+
8,
332+
Content("foo bar\nbaz", spans=[Span(0, 15, "red")]),
333+
),
334+
],
335+
)
336+
def test_expand_tabs(input: Content, tab_width: int, expected: Content):
337+
output = input.expand_tabs(tab_width).simplify()
338+
print(repr(output))
339+
assert output.plain == expected.plain
340+
assert output._spans == expected._spans

0 commit comments

Comments
 (0)