test double width, optimization

This commit is contained in:
Will McGugan 2023-07-28 19:23:51 +01:00
parent 29dfa0e5c8
commit fa0a62a72b
2 changed files with 31 additions and 6 deletions

View file

@ -821,28 +821,30 @@ class Text(JupyterMixin):
tab_size = self.tab_size
assert tab_size is not None
result = self.blank_copy()
append = result.append
new_text: list[Text] = []
append = new_text.append
for line in self.split("\n", include_separator=True):
cell_position = 0
if "\t" not in line.plain:
append(line)
else:
cell_position = 0
parts = line.split("\t", include_separator=True)
tab_parts: list[Text] = []
for part in parts:
if part.plain.endswith("\t"):
part._text[-1] = part._text[-1][:-1] + " "
cell_position += part.cell_len
tab_remainder = cell_position % tab_size
if tab_remainder:
spaces = tab_size - (cell_position % tab_size)
spaces = tab_size - tab_remainder
part.extend_style(spaces)
cell_position += spaces
else:
cell_position += part.cell_len
tab_parts.append(part)
append(Text("").join(tab_parts))
append(part)
result = Text("").join(new_text)
self._text = [result.plain]
self._length = len(self.plain)

View file

@ -600,6 +600,11 @@ def test_tabs_to_spaces():
text.expand_tabs()
assert text.plain == "No Tabs"
text = Text("No Tabs", style="bold")
text.expand_tabs()
assert text.plain == "No Tabs"
assert text.style == "bold"
@pytest.mark.parametrize(
"markup,tab_size,expected_text,expected_spans",
@ -648,6 +653,15 @@ def test_tabs_to_spaces():
Span(20, 23, "green"),
],
),
(
"[bold]X\tY",
8,
"X Y",
[
Span(0, 8, "bold"),
Span(8, 9, "bold"),
],
),
(
"[bold]💩\t💩",
8,
@ -657,6 +671,15 @@ def test_tabs_to_spaces():
Span(7, 8, "bold"),
],
),
(
"[bold]💩💩💩💩\t💩",
8,
"💩💩💩💩 💩",
[
Span(0, 12, "bold"),
Span(12, 13, "bold"),
],
),
],
)
def test_tabs_to_spaces_spans(