Skip to content

Commit

Permalink
table fixes, sep/end tests (broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Mar 1, 2020
1 parent b74e5ba commit 608ff22
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
15 changes: 8 additions & 7 deletions rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,26 +683,26 @@ def _collect_renderables(

def check_text() -> None:
if text:
if end:
append_text(end_text)
append(sep_text.join(text))
all_text = sep_text.join(text)
all_text.end = end
append(all_text)
del text[:]

for renderable in objects:
rich_cast = getattr(renderable, "__rich__", None)
if rich_cast:
renderable = rich_cast()
if isinstance(renderable, ConsoleRenderable):
check_text()
append(renderable)
elif isinstance(renderable, str):
if isinstance(renderable, str):
renderable_str = renderable
if emoji:
renderable_str = _emoji_replace(renderable_str)
render_text = self.render_str(renderable_str)
append_text(_highlighter(render_text))
elif isinstance(renderable, Text):
append_text(renderable)
elif isinstance(renderable, ConsoleRenderable):
check_text()
append(renderable)
elif isinstance(renderable, (Mapping, Sequence)):
check_text()
append(Pretty(renderable, highlighter=_highlighter))
Expand Down Expand Up @@ -756,6 +756,7 @@ def print(
renderables = self._collect_renderables(
objects, sep=sep, end=end, emoji=emoji, highlight=highlight,
)
print(repr(renderables))

render_options = self.options
extend = self.buffer.extend
Expand Down
2 changes: 2 additions & 0 deletions rich/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def __console_width__(self, max_width: int) -> "RenderWidth":
dimensions = [
RenderWidth.get(renderable, max_width) for renderable in self._renderables
]
if not dimensions:
return RenderWidth(1, 1)
_min = max(dimension.minimum for dimension in dimensions)
_max = max(dimension.maximum for dimension in dimensions)
return RenderWidth(_min, _max)
Expand Down
16 changes: 9 additions & 7 deletions rich/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,16 @@ def render_annotation(
render_text = console.render_str(text, style=style)
return render_text.wrap(table_width, "center")

yield render_annotation(
self.title or "", style=Style.pick_first(self.title_style, "table.title")
)
if self.title:
yield render_annotation(
self.title, style=Style.pick_first(self.title_style, "table.title")
)
yield from self._render(console, options, widths)
yield render_annotation(
self.caption or "",
style=Style.pick_first(self.caption_style, "table.caption"),
)
if self.caption:
yield render_annotation(
self.caption,
style=Style.pick_first(self.caption_style, "table.caption"),
)

def _calculate_column_widths(self, max_width: int) -> List[int]:
"""Calculate the widths of each column."""
Expand Down
5 changes: 0 additions & 5 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,6 @@ def __console__(
tab_size = console.tab_size # type: ignore
else:
tab_size = self.tab_size

# if self.tab_size is None:
# tab_size = console.tab_size
# else:
# tab_size = self.tab_size
lines = self.wrap(
options.max_width,
justify=self.justify or options.justify,
Expand Down
27 changes: 27 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from io import StringIO
import pytest

from rich.console import Console
Expand Down Expand Up @@ -274,10 +275,36 @@ def test_render():
assert output == expected


@pytest.mark.parametrize(
"print_text,result",
[
(("."), ".\n"),
((".", "."), ". .\n"),
(("Hello", "World", "!"), "Hello World !\n"),
],
)
def test_print(print_text, result):
console = Console(record=True)
console.print(*print_text)
assert console.export_text(styles=False) == result


@pytest.mark.parametrize(
"print_text,result",
[(("."), ".X"), ((".", "."), "..X"), (("Hello", "World", "!"), "HelloWorld!X"),],
)
def test_print_sep_end(print_text, result):
console = Console(record=True, file=StringIO())
console.print(*print_text, sep="", end="X")
assert console.file.getvalue() == result


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 608ff22

Please sign in to comment.