refactor: use themedb

This commit is contained in:
Daylin Morgan 2023-05-11 07:29:55 -05:00
parent 056ad588d1
commit fd677e8743
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
3 changed files with 43 additions and 27 deletions

View file

@ -2,7 +2,7 @@
THEME_MD="./docs/themes.md"
themes=$(yartsu --list-themes | tail -n +2 | awk '{print $2}')
themes=$(yartsu --list-themes | tail -n +5 | cut -d' ' -f 3)
newline() {
echo >>"$THEME_MD"

View file

@ -1,4 +1,3 @@
import os
import sys
import textwrap
from argparse import SUPPRESS, FileType
@ -13,9 +12,9 @@ from ._version import __version__
from .argparse import ArgumentParser
from .console import Console
from .term import term
from .themes import THEMES
from .themes import ThemeDB
DEFAULT_THEME = os.getenv("YARTSU_THEME", "cat-mocha")
themes = ThemeDB()
def get_parser() -> ArgumentParser:
@ -56,7 +55,7 @@ def get_parser() -> ArgumentParser:
"--theme",
help="theme to use for highlighting [default: %(default)s]",
type=str,
default=DEFAULT_THEME,
default=themes.default,
)
parser.add_argument(
"--list-themes", help="list available themes", action="store_true"
@ -71,8 +70,7 @@ def main() -> None:
console = Console(record=True)
if args.list_themes:
term.print("Available themes:")
term.print("\n".join([" - " + theme for theme in THEMES]))
themes.list()
sys.exit(0)
if args.cmd and args.input or not (args.cmd or args.input or args.demo):
@ -85,7 +83,8 @@ def main() -> None:
parser.print_help()
sys.exit(1)
if args.theme not in THEMES:
# TODO: move this error somewhere else
if args.theme not in themes.themes:
term.print(f"[ThemeError]: {args.theme} is not a valid theme", err=True)
sys.exit(1)
@ -126,7 +125,7 @@ def main() -> None:
console.save_svg(
args.output,
title=title,
theme=THEMES[args.theme],
theme=themes.themes[args.theme],
code_format=CONSOLE_SVG_FORMAT,
)

View file

@ -1,8 +1,11 @@
import json
import os
import sys
from importlib.resources import files
from rich import box
from rich.color import parse_rgb_hex
from rich.table import Table
from rich.terminal_theme import (
DIMMED_MONOKAI,
MONOKAI,
@ -61,23 +64,37 @@ class YartsuTheme(TerminalTheme):
return cls(background, foreground, colors, bright_colors, src=src)
def get_builtin_themes():
return (
class ThemeDB:
def __init__(self):
self.default = os.getenv("YARTSU_THEME", "cat-mocha")
self.selected = self.default
self.themes = {
**self._load_yartsu_themes(),
**{
"dimmed_monokai": DIMMED_MONOKAI,
"monokai": MONOKAI,
"night-owlish": NIGHT_OWLISH,
"rich-default": SVG_EXPORT_THEME,
},
}
def _load_yartsu_themes(self):
return {
name: YartsuTheme.load_theme(name, src="yartsu")
for name in sorted(
resource.name.split(".")[0]
for resource in (files("yartsu") / "themes").iterdir()
if resource.is_file()
)
}
def list(self):
table = Table(title="Available Themes", box=box.MINIMAL)
table.add_column("name")
table.add_column("source")
THEMES = {
**{
name: YartsuTheme.load_theme(name, src="yartsu")
for name in get_builtin_themes()
},
**{
"monokai": MONOKAI,
"dimmed_monokai": DIMMED_MONOKAI,
"night-owlish": NIGHT_OWLISH,
"rich-default": SVG_EXPORT_THEME,
},
}
for name, theme in self.themes.items():
source = theme.src if isinstance(theme, YartsuTheme) else "rich"
table.add_row(name, source)
term.print(table)