fixing table output and moving it to types.RollTable
This commit is contained in:
parent
d88d20fbbd
commit
2b3e82114f
|
@ -51,13 +51,9 @@ def create(
|
||||||
if output == OUTPUT_FORMATS.yaml:
|
if output == OUTPUT_FORMATS.yaml:
|
||||||
print(rt.as_yaml())
|
print(rt.as_yaml())
|
||||||
elif output == OUTPUT_FORMATS.markdown:
|
elif output == OUTPUT_FORMATS.markdown:
|
||||||
print(rt.as_markdown)
|
print(rt.as_markdown())
|
||||||
else:
|
else:
|
||||||
rows = rt.rows if collapsed else rt.expanded_rows
|
print(rt.as_table(width=width, expanded=not collapsed))
|
||||||
table = Table(*rows[0], width=width)
|
|
||||||
for row in rows[1:]:
|
|
||||||
table.add_row(*row)
|
|
||||||
print(table)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -4,6 +4,8 @@ from collections.abc import Iterable
|
||||||
from typing import Optional, List, Union
|
from typing import Optional, List, Union
|
||||||
from random_sets.datasources import DataSource
|
from random_sets.datasources import DataSource
|
||||||
|
|
||||||
|
import rich.table
|
||||||
|
|
||||||
|
|
||||||
class RollTable:
|
class RollTable:
|
||||||
"""
|
"""
|
||||||
|
@ -40,16 +42,6 @@ class RollTable:
|
||||||
self._generated_values = None
|
self._generated_values = None
|
||||||
self._config()
|
self._config()
|
||||||
|
|
||||||
def as_yaml(self, expanded=False) -> dict:
|
|
||||||
struct = {}
|
|
||||||
for row in self.rows[1:]:
|
|
||||||
struct[row[0]] = {}
|
|
||||||
# pad rows with empty cols as necessary
|
|
||||||
cols = row[1:] + [''] * (len(self.headers) - len(row[1:]))
|
|
||||||
for idx, col in enumerate(cols):
|
|
||||||
struct[row[0]][self.headers[idx] if idx < len(self.headers) else '_'] = col
|
|
||||||
return yaml.dump(struct, sort_keys=False)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def datasources(self) -> List:
|
def datasources(self) -> List:
|
||||||
return self._data
|
return self._data
|
||||||
|
@ -106,10 +98,26 @@ class RollTable:
|
||||||
self._rows.append(self._column_filter([f'd{face+1}'] + row))
|
self._rows.append(self._column_filter([f'd{face+1}'] + row))
|
||||||
return self._rows
|
return self._rows
|
||||||
|
|
||||||
@property
|
|
||||||
def as_markdown(self) -> str:
|
def as_markdown(self) -> str:
|
||||||
return Table(self.rows).markdown()
|
return Table(self.rows).markdown()
|
||||||
|
|
||||||
|
def as_yaml(self, expanded: bool = False) -> dict:
|
||||||
|
struct = {}
|
||||||
|
for row in self.rows[1:]:
|
||||||
|
struct[row[0]] = {}
|
||||||
|
# pad rows with empty cols as necessary
|
||||||
|
cols = row[1:] + [''] * (len(self.headers) - len(row[1:]))
|
||||||
|
for idx, col in enumerate(cols):
|
||||||
|
struct[row[0]][self.headers[idx] if idx < len(self.headers) else '_'] = col
|
||||||
|
return yaml.dump(struct, sort_keys=False)
|
||||||
|
|
||||||
|
def as_table(self, width: int = 120, expanded: bool = False) -> str:
|
||||||
|
rows = self.expanded_rows if expanded else self.rows
|
||||||
|
table = rich.table.Table(*rows[0], width=width)
|
||||||
|
for row in rows[1:]:
|
||||||
|
table.add_row(*row)
|
||||||
|
return table
|
||||||
|
|
||||||
def _config(self):
|
def _config(self):
|
||||||
"""
|
"""
|
||||||
Parse data sources, generate headers, and create the column filters
|
Parse data sources, generate headers, and create the column filters
|
||||||
|
|
Loading…
Reference in New Issue
Block a user