Skip to content

Commit

Permalink
fixes / tests for tabs_to_spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Mar 1, 2020
1 parent 6246b1d commit b74e5ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Text:
style (Union[str, Style], optional): Base style for text. Defaults to "".
justify (str, optional): Default alignment for text, "left", "center", "full" or "right". Defaults to None.
end (str, optional): Character to end text with. Defaults to "\n".
tab_size (int, optional): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to None.
tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to 8.
"""

def __init__(
Expand All @@ -101,7 +101,7 @@ def __init__(
style: Union[str, Style] = "",
justify: "JustifyValues" = None,
end: str = "\n",
tab_size: int = None,
tab_size: int = 8,
) -> None:
self._text: List[str] = [text] if text else []
self.style = style
Expand Down Expand Up @@ -402,7 +402,7 @@ def join(self, lines: Iterable["Text"]) -> "Text":
append(self)
return new_text

def tabs_to_spaces(self, tab_size: int = 8) -> "Text":
def tabs_to_spaces(self, tab_size: int = None) -> "Text":
"""Get a new string with tabs converted to spaces.
Args:
Expand All @@ -415,6 +415,8 @@ def tabs_to_spaces(self, tab_size: int = 8) -> "Text":
return self.copy()
parts = self.split("\t", include_separator=True)
pos = 0
if tab_size is None:
tab_size = self.tab_size
result = Text(
style=self.style, justify=self.justify, end=self.end, tab_size=self.tab_size
)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,12 @@ def test_render():
output = console.export_text(styles=True)
expected = "\x1b[1;4mWhere\x1b[0m\x1b[4m there is \x1b[0m\n\x1b[0m\x1b[4ma \x1b[0m\x1b[3;4mWill\x1b[0m\x1b[4m, there \x1b[0m\n\x1b[0m\x1b[4mis a Way.\x1b[0m\n\x1b[0m"
assert output == expected


def test_tabs_to_spaces():
test = Text("\tHello\tWorld", tab_size=8)
assert test.tabs_to_spaces().text == " Hello World"
test = Text("\tHello\tWorld", tab_size=4)
assert test.tabs_to_spaces().text == " Hello World"
test = Text(".\t..\t...\t....\t", tab_size=4)
assert test.tabs_to_spaces().text == ". .. ... .... "

0 comments on commit b74e5ba

Please sign in to comment.