mirror of
https://github.com/daylinmorgan/yartsu.git
synced 2024-12-22 04:20:44 -06:00
refactor: use themedb
This commit is contained in:
parent
056ad588d1
commit
fd677e8743
3 changed files with 43 additions and 27 deletions
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
THEME_MD="./docs/themes.md"
|
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() {
|
newline() {
|
||||||
echo >>"$THEME_MD"
|
echo >>"$THEME_MD"
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
from argparse import SUPPRESS, FileType
|
from argparse import SUPPRESS, FileType
|
||||||
|
@ -13,9 +12,9 @@ from ._version import __version__
|
||||||
from .argparse import ArgumentParser
|
from .argparse import ArgumentParser
|
||||||
from .console import Console
|
from .console import Console
|
||||||
from .term import term
|
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:
|
def get_parser() -> ArgumentParser:
|
||||||
|
@ -56,7 +55,7 @@ def get_parser() -> ArgumentParser:
|
||||||
"--theme",
|
"--theme",
|
||||||
help="theme to use for highlighting [default: %(default)s]",
|
help="theme to use for highlighting [default: %(default)s]",
|
||||||
type=str,
|
type=str,
|
||||||
default=DEFAULT_THEME,
|
default=themes.default,
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--list-themes", help="list available themes", action="store_true"
|
"--list-themes", help="list available themes", action="store_true"
|
||||||
|
@ -71,8 +70,7 @@ def main() -> None:
|
||||||
console = Console(record=True)
|
console = Console(record=True)
|
||||||
|
|
||||||
if args.list_themes:
|
if args.list_themes:
|
||||||
term.print("Available themes:")
|
themes.list()
|
||||||
term.print("\n".join([" - " + theme for theme in THEMES]))
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if args.cmd and args.input or not (args.cmd or args.input or args.demo):
|
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()
|
parser.print_help()
|
||||||
sys.exit(1)
|
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)
|
term.print(f"[ThemeError]: {args.theme} is not a valid theme", err=True)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
@ -126,7 +125,7 @@ def main() -> None:
|
||||||
console.save_svg(
|
console.save_svg(
|
||||||
args.output,
|
args.output,
|
||||||
title=title,
|
title=title,
|
||||||
theme=THEMES[args.theme],
|
theme=themes.themes[args.theme],
|
||||||
code_format=CONSOLE_SVG_FORMAT,
|
code_format=CONSOLE_SVG_FORMAT,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
from importlib.resources import files
|
from importlib.resources import files
|
||||||
|
|
||||||
|
from rich import box
|
||||||
from rich.color import parse_rgb_hex
|
from rich.color import parse_rgb_hex
|
||||||
|
from rich.table import Table
|
||||||
from rich.terminal_theme import (
|
from rich.terminal_theme import (
|
||||||
DIMMED_MONOKAI,
|
DIMMED_MONOKAI,
|
||||||
MONOKAI,
|
MONOKAI,
|
||||||
|
@ -61,23 +64,37 @@ class YartsuTheme(TerminalTheme):
|
||||||
return cls(background, foreground, colors, bright_colors, src=src)
|
return cls(background, foreground, colors, bright_colors, src=src)
|
||||||
|
|
||||||
|
|
||||||
def get_builtin_themes():
|
class ThemeDB:
|
||||||
return (
|
def __init__(self):
|
||||||
resource.name.split(".")[0]
|
self.default = os.getenv("YARTSU_THEME", "cat-mocha")
|
||||||
for resource in (files("yartsu") / "themes").iterdir()
|
self.selected = self.default
|
||||||
if resource.is_file()
|
self.themes = {
|
||||||
)
|
**self._load_yartsu_themes(),
|
||||||
|
|
||||||
|
|
||||||
THEMES = {
|
|
||||||
**{
|
**{
|
||||||
name: YartsuTheme.load_theme(name, src="yartsu")
|
|
||||||
for name in get_builtin_themes()
|
|
||||||
},
|
|
||||||
**{
|
|
||||||
"monokai": MONOKAI,
|
|
||||||
"dimmed_monokai": DIMMED_MONOKAI,
|
"dimmed_monokai": DIMMED_MONOKAI,
|
||||||
|
"monokai": MONOKAI,
|
||||||
"night-owlish": NIGHT_OWLISH,
|
"night-owlish": NIGHT_OWLISH,
|
||||||
"rich-default": SVG_EXPORT_THEME,
|
"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")
|
||||||
|
|
||||||
|
for name, theme in self.themes.items():
|
||||||
|
source = theme.src if isinstance(theme, YartsuTheme) else "rich"
|
||||||
|
table.add_row(name, source)
|
||||||
|
|
||||||
|
term.print(table)
|
||||||
|
|
Loading…
Reference in a new issue