Skip to content

Commit

Permalink
Automatic backup 2024-11-10
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Nov 11, 2024
1 parent e77ef66 commit fbeee8b
Show file tree
Hide file tree
Showing 14 changed files with 2,250 additions and 1,706 deletions.
6 changes: 3 additions & 3 deletions rich_tables/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class Reaction:
content: str

def __str__(self) -> str:
return f':{self.content.lower()}: {get_val(self, "user")}'
return f":{self.content.lower()}: {get_val(self, 'user')}"


class CreatedMixin(Protocol):
Expand Down Expand Up @@ -460,7 +460,7 @@ def make_info_subpanel(self, attr: str) -> Panel:
title=wrap(attr, "b"),
title_align="center",
expand=True,
align="center",
vertical_align="middle",
)

@property
Expand Down Expand Up @@ -495,7 +495,7 @@ def info(self) -> Panel:
"b",
)
),
align="center",
vertical_align="middle",
title_align="center",
subtitle_align="center",
)
Expand Down
8 changes: 5 additions & 3 deletions rich_tables/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from .utils import (
NewTable,
border_panel,
sortgroup_by,
new_table,
predictably_random_color,
simple_panel,
sortgroup_by,
wrap,
)

Expand All @@ -38,7 +38,7 @@
"skips",
"helicopta",
"hidden",
"lyrics"
"lyrics",
]
ALBUM_IGNORE = set(TRACK_FIELDS) | {
"album_color",
Expand Down Expand Up @@ -178,7 +178,9 @@ def album_panel(tracks: List[JSONDict]) -> Panel:
Group(
album["album_title"],
Align.center(
simple_panel(comments, style="grey54", expand=True, align="center")
simple_panel(
comments, style="grey54", expand=True, vertical_align="middle"
)
)
if comments
else "",
Expand Down
70 changes: 49 additions & 21 deletions rich_tables/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import sqlparse
from multimethod import multimethod
from rich import box
from rich.align import Align
from rich.align import Align, VerticalAlignMethod
from rich.bar import Bar
from rich.console import Console, RenderableType
from rich.console import Console, ConsoleRenderable, RenderableType, RichCast
from rich.errors import MarkupError
from rich.markdown import Markdown
from rich.panel import Panel
Expand Down Expand Up @@ -244,21 +244,45 @@ def make_console(**kwargs) -> SafeConsole:


class NewTable(Table):
def __init__(self, *args: str, **kwargs: Any) -> None:
ckwargs = {
"overflow": kwargs.pop("overflow", "fold"),
"justify": kwargs.pop("justify", "left"),
"vertical": kwargs.pop("vertical", "middle"),
}
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}

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

def add_column(self, *args, **kwargs) -> None:
kwargs.setdefault("overflow", "fold")
kwargs.setdefault("justify", "left")
kwargs.setdefault("vertical", "middle")
self.show_header = True
super().add_column(*args, **kwargs)

def add_rows(self, rows: Iterable[Iterable[RenderableType]]) -> None:
"""Add multiple rows to the table."""
for row in rows:
self.add_row(*row)

def add_dict_row(
self, data: JSONDict, ignore_extra_fields: bool = False, **kwargs
) -> None:
"""Add a row to the table from a dictionary."""
if not ignore_extra_fields:
existing_cols = set(self.colnames)
for field in (f for f in data if f not in existing_cols):
self.add_column(field)

values = [data.get(c, "") for c in self.colnames]
self.add_row(
*(
(v if isinstance(v, (ConsoleRenderable, RichCast, str)) else str(v))
for v in values
),
**kwargs,
)

@property
def colnames(self) -> List[str]:
"""Provide a mapping between columns names / ids and columns."""
Expand All @@ -268,21 +292,22 @@ def colnames(self) -> List[str]:
def new_table(
*headers: str, rows: Iterable[Iterable[RenderableType]] | None = None, **kwargs
) -> NewTable:
if headers:
kwargs.setdefault("header_style", "bold misty_rose1")
kwargs.setdefault("show_header", True)
kwargs.setdefault("box", box.SIMPLE_HEAVY)
else:
kwargs.setdefault("show_header", False)
kwargs.setdefault("box", box.ROUNDED)

kwargs.setdefault("show_edge", False)
kwargs.setdefault("show_header", False)
kwargs.setdefault("pad_edge", False)
kwargs.setdefault("highlight", True)
kwargs.setdefault("row_styles", ["white"])
kwargs.setdefault("expand", False)
kwargs.setdefault("title_justify", "left")
kwargs.setdefault("style", "black")
kwargs.setdefault("border_style", "black")
kwargs.setdefault("box", box.ROUNDED)

if headers:
kwargs.setdefault("header_style", "bold misty_rose1")
kwargs.setdefault("show_header", True)
kwargs.setdefault("box", box.SIMPLE_HEAVY)

table = NewTable(*headers, **kwargs)
if rows:
Expand Down Expand Up @@ -339,16 +364,19 @@ def fmt_pred_color(m: Match[str]) -> str:
return f"{predictably_random_color(m.group(2))}]{m.group(2)}"


def simple_panel(content: RenderableType, **kwargs) -> Panel:
def simple_panel(
content: RenderableType, vertical_align: VerticalAlignMethod | None = None, **kwargs
) -> Panel:
kwargs.setdefault("title_align", "left")
kwargs.setdefault("subtitle_align", "right")
kwargs.setdefault("box", box.SIMPLE)
kwargs.setdefault("expand", False)
kwargs.setdefault("border_style", "red")
# if "title" in kwargs:
# kwargs["title"] = wrap(kwargs["title"], "b")
if kwargs.pop("align", "") == "center":
content = Align.center(content, vertical="middle")
if "title" in kwargs:
kwargs["title"] = wrap(kwargs["title"], "b")

if vertical_align:
content = Align.center(content, vertical=vertical_align)
return Panel(content, **kwargs)


Expand Down
Loading

0 comments on commit fbeee8b

Please sign in to comment.