Skip to content

Commit 6df976f

Browse files
committed
Introduces table output
1 parent 3b6e032 commit 6df976f

5 files changed

Lines changed: 86 additions & 2 deletions

File tree

.devcli/example.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def text():
3838
cmd.info("This is a cmd.info(msg)")
3939
cmd.warn("This is a cmd.warn(msg)")
4040
cmd.error("This is a cmd.error(msg)")
41+
# Simple table
42+
table = [
43+
["This is", "a simple table"],
44+
["With one value", "and another"],
45+
]
46+
cmd.table(table)
4147

4248

4349
@cli.command()

devcli/framework/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
from .base import new, stop, logger
22

3-
from .console import echo, info, warn, error, json
3+
from .console import echo, info, warn, error, json, table
44

5-
__all__ = ["new", "echo", "json", "warn", "error", "echo", "info", "stop", "logger"]
5+
__all__ = [
6+
# from console
7+
"echo",
8+
"json",
9+
"warn",
10+
"error",
11+
"echo",
12+
"info",
13+
"table",
14+
# from base
15+
"new",
16+
"stop",
17+
"logger",
18+
]

devcli/framework/console.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from typing import List
2+
3+
from rich.table import Table
4+
from rich.box import SQUARE
15
from rich import print
26
from rich import json as rjson
37

@@ -37,3 +41,39 @@ def info(msg: str):
3741
Prints a message in cyan
3842
"""
3943
print(f"[cyan]{msg}[/cyan]")
44+
45+
46+
def table(table: List[List[str]] = [[]], as_grid: bool = False):
47+
"""
48+
Prints a simple table, for complex tables use rich.Table directly.
49+
50+
First element will be used as column and the the rest as rows.
51+
For example:
52+
[
53+
["Col A", "Col B"],
54+
["Row 1A", "Row 1B"],
55+
["Row 2A", "Row 2B"]
56+
]
57+
"""
58+
rich_table = None
59+
table_copy = table.copy() # avoid altering param
60+
61+
# do nothing with an empty table
62+
if len(table_copy) == 0:
63+
return
64+
elif as_grid:
65+
rich_table = Table(show_lines=True, show_header=False, box=SQUARE)
66+
else:
67+
rich_table = Table(show_lines=True, box=SQUARE)
68+
69+
if not as_grid:
70+
# use the cols given, also remove it
71+
cols = table_copy.pop(0)
72+
for c in cols:
73+
rich_table.add_column(c)
74+
75+
# only rows should be remaining
76+
for r in table_copy:
77+
rich_table.add_row(*r)
78+
79+
echo(rich_table)

tests/test_console.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,23 @@ def test_warn(mock_print):
3737
def test_error(mock_print):
3838
console.error(MESSAGE)
3939
assert_print(mock_print, color="red")
40+
41+
42+
def test_simple_grid(mock_print):
43+
grid = [["Row A", "Row B"]]
44+
console.table(grid)
45+
46+
argument = mock_print.call_args.args[0]
47+
full_name = ".".join([argument.__module__, argument.__class__.__name__])
48+
49+
assert full_name == "rich.table.Table"
50+
51+
52+
def test_simple_table(mock_print):
53+
grid = [["Col A", "Col B"], ["Row A", "Row B"]]
54+
console.table(grid)
55+
56+
argument = mock_print.call_args.args[0]
57+
full_name = ".".join([argument.__module__, argument.__class__.__name__])
58+
59+
assert full_name == "rich.table.Table"

tests/test_example.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def test_hello_text_example():
3131
"This is a cmd.info(msg)\n"
3232
"This is a cmd.warn(msg)\n"
3333
"This is a cmd.error(msg)\n"
34+
"┌────────────────┬────────────────┐\n"
35+
"│ This is │ a simple table │\n"
36+
"├────────────────┼────────────────┤\n"
37+
"│ With one value │ and another │\n"
38+
"└────────────────┴────────────────┘\n"
3439
)
3540
assert result.output in expected_output
3641

0 commit comments

Comments
 (0)