mirror of
https://github.com/daylinmorgan/yartsu.git
synced 2024-11-12 17:23:15 -06:00
refactor: use a proper theme class
This commit is contained in:
parent
4c9360dd1e
commit
056ad588d1
1 changed files with 51 additions and 51 deletions
|
@ -14,6 +14,53 @@ from rich.terminal_theme import (
|
|||
from .term import term
|
||||
|
||||
|
||||
class YartsuTheme(TerminalTheme):
|
||||
def __init__(self, *args, src: str = None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.src = src
|
||||
|
||||
@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)
|
||||
|
||||
try:
|
||||
format = theme_json["format"]
|
||||
|
||||
if format == "rgb":
|
||||
background = theme_json["background"]
|
||||
foreground = theme_json["foreground"]
|
||||
colors = theme_json["colors"]
|
||||
if "bright_colors" in theme_json:
|
||||
bright_colors = theme_json["bright_colors"]
|
||||
else:
|
||||
bright_colors = colors
|
||||
|
||||
elif format == "hex":
|
||||
background = parse_rgb_hex(theme_json["background"])
|
||||
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"]
|
||||
]
|
||||
else:
|
||||
bright_colors = colors
|
||||
else:
|
||||
print("[ThemeError]: unknown color format type {color_fmt}")
|
||||
|
||||
except KeyError as e:
|
||||
term.print(
|
||||
f"[ThemeError]: error loading {name} theme. "
|
||||
f"Couldn't load {e} from theme json.",
|
||||
err=True,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
return cls(background, foreground, colors, bright_colors, src=src)
|
||||
|
||||
|
||||
def get_builtin_themes():
|
||||
return (
|
||||
resource.name.split(".")[0]
|
||||
|
@ -22,58 +69,11 @@ def get_builtin_themes():
|
|||
)
|
||||
|
||||
|
||||
def load_theme(name: str) -> TerminalTheme:
|
||||
theme_file = files("yartsu") / "themes" / f"{name}.json"
|
||||
with theme_file.open("r") as f:
|
||||
theme_json = json.load(f)
|
||||
|
||||
try:
|
||||
format = theme_json["format"]
|
||||
|
||||
if format == "rgb":
|
||||
background = theme_json["background"]
|
||||
foreground = theme_json["foreground"]
|
||||
colors = theme_json["colors"]
|
||||
if "bright_colors" in theme_json:
|
||||
bright_colors = theme_json["bright_colors"]
|
||||
else:
|
||||
bright_colors = colors
|
||||
|
||||
elif format == "hex":
|
||||
background = parse_rgb_hex(theme_json["background"])
|
||||
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"]]
|
||||
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. "
|
||||
f"Couldn't load {e} from theme json.",
|
||||
err=True,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
return theme
|
||||
|
||||
|
||||
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,
|
||||
|
|
Loading…
Reference in a new issue