Skip to content

Commit

Permalink
Automatic backup 2024-11-11
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Nov 12, 2024
1 parent fbeee8b commit 2ac1ab9
Show file tree
Hide file tree
Showing 10 changed files with 1,271 additions and 1,709 deletions.
7 changes: 5 additions & 2 deletions rich_tables/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def mapping_view_table() -> NewTable:
table = new_table(border_style="cyan", style="cyan", box=box.MINIMAL, expand=False)
table.add_column(justify="right", style="bold misty_rose1")
table.add_column()
table.show_header = False
return table


Expand Down Expand Up @@ -171,7 +172,9 @@ def _json_dict(data: JSONDict) -> RenderableType:
continue

value = flexitable(content, key)
if isinstance(value, Generator):
if len(str(content)) < MAX_DICT_LENGTH:
table.add_row(key, value)
elif isinstance(value, Generator):
cols.append(border_panel(Group(*value), title=key))
elif isinstance(value, (NewTable, Text, dict, Columns)):
cols.append(border_panel(value, title=key))
Expand Down Expand Up @@ -281,7 +284,7 @@ def getval(value: Any, key: str) -> RenderableType:
if isinstance(transformed_value, str):
return f"{header}: {transformed_value}"

print(f"{transformed_value=}, {type(transformed_value)=}")
# print(f"{transformed_value=}, {type(transformed_value)=}")
if isinstance(transformed_value, (Panel, NewTable)):
transformed_value = new_tree([transformed_value], header)
elif isinstance(transformed_value, Tree):
Expand Down
8 changes: 5 additions & 3 deletions rich_tables/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,12 @@ def pr_state(self) -> str:
@property
def info(self) -> Panel:
fields = "author", "dates", "headRefName", "participants", "reviewRequests"
pairs = [(f, getattr(self, f)) for f in fields]
field_rows = [flexitable({f: v}) for f, v in pairs if v]
pairs = {f: v for f in fields if (v := getattr(self, f))}
field_rows = flexitable(pairs)
return border_panel(
new_table(rows=[*field_rows, [md_panel(self.body)], [self.files_commits]]),
new_table(
rows=[list(field_rows), [md_panel(self.body)], [self.files_commits]]
),
title=f"{self.name} @ {self.repo}",
box=box.DOUBLE_EDGE,
border_style=COLOR_BY_STATE[self.pr_state],
Expand Down
29 changes: 20 additions & 9 deletions rich_tables/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,29 @@ class NewTable(Table):
def __init__(self, *args, **kwargs) -> None:
table_kwarg_names = set(Table.__init__.__code__.co_varnames)
column_kwarg_names = kwargs.keys() - table_kwarg_names
column_kwargs = {k: kwargs.pop(k) for k in column_kwarg_names}
self.column_kwargs = {k: kwargs.pop(k) for k in column_kwarg_names}

super().__init__(**kwargs)
for arg in args:
self.add_column(arg, **column_kwargs)
super().__init__(*args, **kwargs)

def add_column(self, *args, **kwargs) -> None:
kwargs.setdefault("overflow", "fold")
kwargs.setdefault("justify", "left")
kwargs.setdefault("vertical", "middle")
for k, v in self.column_kwargs.items():
kwargs.setdefault(k, v)

self.show_header = True
super().add_column(*args, **kwargs)

def add_row(self, *args: RenderableType | None, **kwargs) -> None:
rends = list(args)
if (overflow := self.column_kwargs.get("overflow")) and (
max_width := self.column_kwargs.get("max_width")
):
rends = [(Text.from_markup(a) if isinstance(a, str) else a) for a in args]
for r in rends:
if isinstance(r, Text):
r.truncate(max_width, overflow=overflow)

return super().add_row(*rends, **kwargs)

def add_rows(self, rows: Iterable[Iterable[RenderableType]]) -> None:
"""Add multiple rows to the table."""
for row in rows:
Expand All @@ -273,6 +283,7 @@ def add_dict_row(
existing_cols = set(self.colnames)
for field in (f for f in data if f not in existing_cols):
self.add_column(field)
self.columns[-1]._cells = [""] * self.row_count

values = [data.get(c, "") for c in self.colnames]
self.add_row(
Expand Down Expand Up @@ -595,7 +606,7 @@ def _(before: dict, after: dict) -> Any:
return data


def pretty_diff(before: Any, after: Any) -> Text:
def pretty_diff(before: Any, after: Any, **kwargs) -> Text:
result = diff(to_hashable(before), to_hashable(after))
if not isinstance(result, str):
result = (
Expand All @@ -605,4 +616,4 @@ def pretty_diff(before: Any, after: Any) -> Text:
.replace("\\\\", "\\")
)

return Text.from_markup(result)
return Text.from_markup(result, **kwargs)
Loading

0 comments on commit 2ac1ab9

Please sign in to comment.