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