feat: add user theme loader

This commit is contained in:
Daylin Morgan 2023-05-11 08:08:24 -05:00
parent fd677e8743
commit fa5e7c6a74
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -1,7 +1,9 @@
import json import json
import os import os
import platform
import sys import sys
from importlib.resources import files from importlib.resources import files
from pathlib import Path
from rich import box from rich import box
from rich.color import parse_rgb_hex from rich.color import parse_rgb_hex
@ -23,9 +25,11 @@ class YartsuTheme(TerminalTheme):
self.src = src self.src = src
@classmethod @classmethod
def load_theme(cls, name: str, src: str): def load_theme(cls, themepath: Path, 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: name = themepath.stem
with themepath.open("r") as f:
theme_json = json.load(f) theme_json = json.load(f)
try: try:
@ -69,6 +73,7 @@ class ThemeDB:
self.default = os.getenv("YARTSU_THEME", "cat-mocha") self.default = os.getenv("YARTSU_THEME", "cat-mocha")
self.selected = self.default self.selected = self.default
self.themes = { self.themes = {
**self._load_user_themes(),
**self._load_yartsu_themes(), **self._load_yartsu_themes(),
**{ **{
"dimmed_monokai": DIMMED_MONOKAI, "dimmed_monokai": DIMMED_MONOKAI,
@ -76,18 +81,39 @@ class ThemeDB:
"night-owlish": NIGHT_OWLISH, "night-owlish": NIGHT_OWLISH,
"rich-default": SVG_EXPORT_THEME, "rich-default": SVG_EXPORT_THEME,
}, },
**self._load_user_themes(),
} }
def _user_themes_location(self):
if platform.system() == "Windows":
theme_dir = os.getenv("APPDATA") / "yarstu" / "themes"
else:
if XDG_CONFIG_DIR := os.getenv("XDG_CONFIG_DIR"):
theme_dir = Path(XDG_CONFIG_DIR) / "yartsu" / "themes"
else:
theme_dir = Path.home() / ".config" / "yartsu" / "themes"
return theme_dir
def _load_yartsu_themes(self): def _load_yartsu_themes(self):
return { return {
name: YartsuTheme.load_theme(name, src="yartsu") themepath.stem: YartsuTheme.load_theme(themepath, src="yartsu")
for name in sorted( for themepath in sorted(
resource.name.split(".")[0] resource
for resource in (files("yartsu") / "themes").iterdir() for resource in (files("yartsu") / "themes").iterdir()
if resource.is_file() if resource.is_file()
) )
} }
def _load_user_themes(self):
if (themes_dir := self._user_themes_location()).is_dir():
return {
p.stem: YartsuTheme.load_theme(p, src="user")
for p in themes_dir.glob("*.json")
}
else:
return {}
def list(self): def list(self):
table = Table(title="Available Themes", box=box.MINIMAL) table = Table(title="Available Themes", box=box.MINIMAL)
table.add_column("name") table.add_column("name")
@ -96,5 +122,4 @@ class ThemeDB:
for name, theme in self.themes.items(): for name, theme in self.themes.items():
source = theme.src if isinstance(theme, YartsuTheme) else "rich" source = theme.src if isinstance(theme, YartsuTheme) else "rich"
table.add_row(name, source) table.add_row(name, source)
term.print(table) term.print(table)