Skip to content

Commit

Permalink
Allow print_rich_object() to take a TableConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
rickwporter committed Jan 1, 2025
1 parent 38b114f commit d6f760c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
35 changes: 34 additions & 1 deletion tests/test_rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typer
import typer.completion
import yaml
from typer.rich_utils import OutputFormat, print_rich_object
from typer.rich_utils import OutputFormat, TableConfig, print_rich_object
from typer.testing import CliRunner

runner = CliRunner()
Expand Down Expand Up @@ -88,6 +88,21 @@ def main() -> None:
└──────┴────────────────┘
Found 3 items"""

CONFIGED_TEXT = """\
┏━━━━━━┳━━━━━━━━━━━━━━━━┓
┃ Name ┃ Inner ┃
┡━━━━━━╇━━━━━━━━━━━━━━━━┩
│ sna │ prop1 1 │
│ │ prop B None │
│ │ blah zay │
├──────┼────────────────┤
│ foo │ prop2 2 │
│ │ prop B True │
├──────┼────────────────┤
│ bar │ 1 inverse │
└──────┴────────────────┘
This shows 3 items"""


def non_ascii_dots(s: str) -> str:
"""
Expand Down Expand Up @@ -139,3 +154,21 @@ def print_rich_none(output_format: OutputFormat):
assert result.exit_code == 0
output = non_ascii_dots(result.stdout)
assert output.startswith(non_ascii_dots(expected))


def test_rich_object_config():
app = typer.Typer()

@app.command()
def print_rich_data():
config = TableConfig(
row_properties={"justify": "right", "no_wrap": True, "overflow": "ignore"},
properties_label="Inner",
items_caption="This shows {} items",
)
print_rich_object(DATA, config=config)

result = runner.invoke(app, [])
assert result.exit_code == 0
output = non_ascii_dots(result.stdout)
assert output.startswith(non_ascii_dots(CONFIGED_TEXT))
14 changes: 10 additions & 4 deletions typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from rich.text import Text
from rich.theme import Theme

from .rich_table import rich_table_factory
from .rich_table import TableConfig, rich_table_factory

if sys.version_info >= (3, 8):
from typing import Literal
Expand Down Expand Up @@ -746,6 +746,7 @@ def print_rich_object_for_console(
obj: Any,
out_fmt: OutputFormat = OutputFormat.TEXT,
indent: int = 2,
config: TableConfig = TableConfig(),
) -> None:
"""Print rich version of the provided object in the specified format using provided `Console`."""
if out_fmt == OutputFormat.JSON:
Expand All @@ -760,13 +761,18 @@ def print_rich_object_for_console(
console.print("Nothing found")
return

table = rich_table_factory(obj)
table = rich_table_factory(obj, config)
console.print(table)


def print_rich_object(
obj: Any, out_fmt: OutputFormat = OutputFormat.TEXT, indent: int = 2
obj: Any,
out_fmt: OutputFormat = OutputFormat.TEXT,
indent: int = 2,
config: TableConfig = TableConfig(),
) -> None:
"""Print rich version of the provided object in the specified format."""
console = _get_rich_console()
print_rich_object_for_console(console, obj, out_fmt=out_fmt, indent=indent)
print_rich_object_for_console(
console, obj, out_fmt=out_fmt, indent=indent, config=config
)

0 comments on commit d6f760c

Please sign in to comment.