Skip to content

Commit

Permalink
added wrapping feature and flag plus some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant-dash committed Dec 21, 2023
1 parent 755037a commit 52d06b1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
36 changes: 27 additions & 9 deletions rtab/base_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class BaseToRichTable:

def __init__(self, **kwargs) -> None:
"""Initialize input dict as class attributes."""
self.table = None
self.wrap = False
for k, v in kwargs.items():
setattr(self, k, v)

Expand All @@ -29,7 +31,24 @@ def load(self, data) -> dict: # pylint: disable=missing-type-doc
def create_table(self) -> Table:
"""Create a rich table object."""
# pylint: disable=maybe-no-member
return Table(box=box.ROUNDED, highlight=not self.quiet)
self.table = Table(box=box.ROUNDED, highlight=not self.quiet)

def add_column(self, column: str = ""):
"""Add a column to a rich table object.
:param column: A string to add to the table as a column header
"""
# pylint: disable=maybe-no-member
(self.table).add_column(column, overflow="fold" if self.wrap else "ellipsis")

def add_row(self, row: list = None):
"""Add a row to a rich table object.
:param row: A list to add to the table as a row
"""
# highlighter = ContextHighlight(self.rules)
# formatted_row = highlighter.apply_highlights(row)
(self.table).add_row(*row)

def pre_run(self, stdin_data, skip_load: bool = False): # pylint: disable=missing-type-doc
"""Do some basic checks and return loaded data.
Expand Down Expand Up @@ -57,25 +76,24 @@ def run(self, stdin_data, skip_load: bool = False) -> int: # pylint: disable=mi
return 1

# Initialize table object
table = self.create_table()
self.create_table()

if data_type == dict:
# Show Logic
table.add_column("Key", overflow="fold")
table.add_column("Value", overflow="fold")
self.add_column("Key")
self.add_column("Value")
for k, v in data.items():
table.add_row(k, str(v))
self.add_row([k, str(v)])
elif data_type == list:
# List Logic
columns = list(data[0].keys())
for c in columns:
table.add_column(c, overflow="fold")
self.add_column(c)
for r in data:
temp_row = [str(v) for v in r.values()]
table.add_row(*temp_row)
self.add_row([str(v) for v in r.values()])
else:
console.print(f"Unsupported type {data_type}")
return 1

console.print(table)
console.print(self.table)
return 0
11 changes: 11 additions & 0 deletions rtab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ def main( # pylint: disable=too-many-arguments
show_default=False,
),
] = None,
wrap: Annotated[
bool,
typer.Option(
"-w",
"--wrap",
help="Allow wrapping of text in rows, helpful if row is long",
rich_help_panel="Modifiers",
),
] = False,
rules: Annotated[
str,
typer.Option(
Expand Down Expand Up @@ -109,6 +118,7 @@ def main( # pylint: disable=too-many-arguments
:param quiet: Suppress highlighting
:param separator: Specify a separator, only applies with table input '-t'
:param rules: Add special highlighting ruels such as openstack, juju, etc...
:param wrap: Allow wrapping of text in rows, helpful if row is long
:param file: Specify a file (file extension matters!)
:raises Exit: 1 if no defining input is provided
"""
Expand All @@ -117,6 +127,7 @@ def main( # pylint: disable=too-many-arguments
"quiet": quiet,
"separator": separator,
"rules": rules,
"wrap": wrap,
}
if file:
obj = FileToRichTable(**extra_options)
Expand Down
9 changes: 4 additions & 5 deletions rtab/csv_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def load(self, data) -> list: # type: ignore[override] # pylint: disable=missin
:param data: Can be either a string or file handler, used to load data from
"""
# @TODO try playing with Dictreader?
loaded_data = []
try:
# pylint: disable=maybe-no-member
Expand Down Expand Up @@ -48,16 +47,16 @@ def run(self, stdin_data, skip_load: bool = False) -> int: # pylint: disable=mi
return 1

# Initialize table object
table = self.create_table()
self.create_table()

if data_type == list:
for column in list(data[0]):
table.add_column(column, overflow="fold")
self.add_column(column)
for row in data[1:]:
table.add_row(*row)
self.add_row(row)
else:
console.print(f"Csv reader can't handle {data_type}")
return 1

console.print(table)
console.print(self.table)
return 0
4 changes: 2 additions & 2 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

def test_create_table():
obj = BaseToRichTable(**{"quiet": False})
data = obj.create_table()
assert type(data) is Table
obj.create_table()
assert type(obj.table) is Table


def test_pre_run_success():
Expand Down

0 comments on commit 52d06b1

Please sign in to comment.