/
table.py
51 lines (39 loc) · 1.51 KB
/
table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from typing import Any, List
from rich.console import Console
from rich.table import Table
from tackle import BaseHook, Field
class TableHook(BaseHook):
"""Hook for creating tables with rich - github.com/willmcgugan/rich."""
hook_name = 'table'
column_names: List = Field([], description="List of column names")
sort: bool = Field(False, description="Boolean to sort contents or contents_split")
contents: Any = Field(None, description="List of lists to put into columns / rows")
contents_split: List = Field(
None,
description="List of strings to separate into columns based on `separator`",
)
separator: str = Field(
None, description="A string to separate the strings in the contents"
)
def exec(self):
if self.sort:
if self.contents:
self.contents.sort()
if self.contents_split:
self.contents_split.sort()
console = Console()
if len(self.column_names) > 0:
table = Table(show_header=True, header_style="bold red")
for c in self.column_names:
table.add_column(c)
else:
table = Table()
if self.contents_split:
for i in self.contents_split:
table.add_row(
*i.split(self.separator)[0 : len(self.column_names)] # noqa
)
if self.contents:
for i in self.contents:
table.add_row(*[str(j) for j in i])
console.print(table)