refactor: use a proper theme class

This commit is contained in:
Daylin Morgan 2023-05-09 10:09:56 -05:00
parent 4c9360dd1e
commit 056ad588d1
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -14,15 +14,13 @@ from rich.terminal_theme import (
from .term import term from .term import term
def get_builtin_themes(): class YartsuTheme(TerminalTheme):
return ( def __init__(self, *args, src: str = None, **kwargs):
resource.name.split(".")[0] super().__init__(*args, **kwargs)
for resource in (files("yartsu") / "themes").iterdir() self.src = src
if resource.is_file()
)
@classmethod
def load_theme(name: str) -> TerminalTheme: def load_theme(cls, name: str, src: str):
theme_file = files("yartsu") / "themes" / f"{name}.json" theme_file = files("yartsu") / "themes" / f"{name}.json"
with theme_file.open("r") as f: with theme_file.open("r") as f:
theme_json = json.load(f) theme_json = json.load(f)
@ -44,13 +42,14 @@ def load_theme(name: str) -> TerminalTheme:
foreground = parse_rgb_hex(theme_json["foreground"]) foreground = parse_rgb_hex(theme_json["foreground"])
colors = [parse_rgb_hex(c) for c in theme_json["colors"]] colors = [parse_rgb_hex(c) for c in theme_json["colors"]]
if "bright_colors" in theme_json: if "bright_colors" in theme_json:
bright_colors = [parse_rgb_hex(c) for c in theme_json["bright_colors"]] bright_colors = [
parse_rgb_hex(c) for c in theme_json["bright_colors"]
]
else: else:
bright_colors = colors bright_colors = colors
else: else:
print("[ThemeError]: unknown color format type {color_fmt}") print("[ThemeError]: unknown color format type {color_fmt}")
theme = TerminalTheme(background, foreground, colors, bright_colors)
except KeyError as e: except KeyError as e:
term.print( term.print(
f"[ThemeError]: error loading {name} theme. " f"[ThemeError]: error loading {name} theme. "
@ -59,21 +58,22 @@ def load_theme(name: str) -> TerminalTheme:
) )
sys.exit(1) sys.exit(1)
return theme return cls(background, foreground, colors, bright_colors, src=src)
def get_builtin_themes():
return (
resource.name.split(".")[0]
for resource in (files("yartsu") / "themes").iterdir()
if resource.is_file()
)
THEMES = { THEMES = {
# **{ **{
# theme.name: TerminalTheme( name: YartsuTheme.load_theme(name, src="yartsu")
# theme.background, for name in get_builtin_themes()
# theme.foreground, },
# theme.colors,
# theme.bright_colors,
# )
# for theme in THEME_DEFINITIONS
#
# },
**{name: load_theme(name) for name in get_builtin_themes()},
**{ **{
"monokai": MONOKAI, "monokai": MONOKAI,
"dimmed_monokai": DIMMED_MONOKAI, "dimmed_monokai": DIMMED_MONOKAI,