refactor: start transition to json-based themes

This commit is contained in:
Daylin Morgan 2023-05-09 08:17:15 -05:00
parent 2e4a0454fc
commit 4c9360dd1e
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
10 changed files with 73 additions and 216 deletions

View file

@ -29,6 +29,9 @@ dev = [
"mypy>=0.961", "mypy>=0.961",
] ]
[tool.pdm.build]
includes = ["src", "src/yartsu/themes"]
[build-system] [build-system]
requires = ["pdm-pep517>=0.12.0"] requires = ["pdm-pep517>=0.12.0"]
build-backend = "pdm.pep517.api" build-backend = "pdm.pep517.api"

View file

@ -1,4 +1,6 @@
from collections import namedtuple import json
import sys
from importlib.resources import files
from rich.color import parse_rgb_hex from rich.color import parse_rgb_hex
from rich.terminal_theme import ( from rich.terminal_theme import (
@ -9,225 +11,69 @@ from rich.terminal_theme import (
TerminalTheme, TerminalTheme,
) )
Colors = namedtuple("Colors", "black, red, green, yellow, blue, magenta, cyan, white") from .term import term
Theme = namedtuple("Theme", "name background, foreground, colors, bright_colors")
THEME_DEFINITIONS = [
Theme( def get_builtin_themes():
name="cat-mocha", return (
background=(30, 30, 46), resource.name.split(".")[0]
foreground=(198, 208, 245), for resource in (files("yartsu") / "themes").iterdir()
colors=Colors( if resource.is_file()
black=(179, 188, 223), )
cyan=(148, 226, 213),
yellow=(249, 226, 175),
blue=(135, 176, 249), def load_theme(name: str) -> TerminalTheme:
red=(243, 139, 168), theme_file = files("yartsu") / "themes" / f"{name}.json"
white=(86, 89, 112), with theme_file.open("r") as f:
green=(166, 227, 161), theme_json = json.load(f)
magenta=(245, 194, 231),
), try:
bright_colors=Colors( format = theme_json["format"]
black=(161, 168, 201),
cyan=(148, 226, 213), if format == "rgb":
yellow=(249, 226, 175), background = theme_json["background"]
blue=(135, 176, 249), foreground = theme_json["foreground"]
red=(243, 139, 168), colors = theme_json["colors"]
white=(67, 70, 90), if "bright_colors" in theme_json:
green=(166, 227, 161), bright_colors = theme_json["bright_colors"]
magenta=(245, 194, 231), else:
), bright_colors = colors
),
Theme( elif format == "hex":
name="cat-frappe", background = parse_rgb_hex(theme_json["background"])
background=(48, 52, 70), foreground = parse_rgb_hex(theme_json["foreground"])
foreground=(198, 206, 239), colors = [parse_rgb_hex(c) for c in theme_json["colors"]]
colors=Colors( if "bright_colors" in theme_json:
black=(179, 188, 223), bright_colors = [parse_rgb_hex(c) for c in theme_json["bright_colors"]]
cyan=(148, 226, 213), else:
yellow=(249, 226, 175), bright_colors = colors
blue=(135, 176, 249), else:
red=(243, 139, 168), print("[ThemeError]: unknown color format type {color_fmt}")
white=(86, 89, 112),
green=(166, 227, 161), theme = TerminalTheme(background, foreground, colors, bright_colors)
magenta=(245, 194, 231), except KeyError as e:
), term.print(
bright_colors=Colors( f"[ThemeError]: error loading {name} theme. "
black=(161, 168, 201), f"Couldn't load {e} from theme json.",
cyan=(148, 226, 213), err=True,
yellow=(249, 226, 175), )
blue=(135, 176, 249), sys.exit(1)
red=(243, 139, 168),
white=(67, 70, 90), return theme
green=(166, 227, 161),
magenta=(245, 194, 231),
),
),
Theme(
name="cat-macchiato",
background=(36, 39, 58),
foreground=(197, 207, 245),
colors=Colors(
black=(179, 188, 223),
cyan=(148, 226, 213),
yellow=(249, 226, 175),
blue=(135, 176, 249),
red=(243, 139, 168),
white=(86, 89, 112),
green=(166, 227, 161),
magenta=(245, 194, 231),
),
bright_colors=Colors(
black=(161, 168, 201),
cyan=(148, 226, 213),
yellow=(249, 226, 175),
blue=(135, 176, 249),
red=(243, 139, 168),
white=(67, 70, 90),
green=(166, 227, 161),
magenta=(245, 194, 231),
),
),
Theme(
name="cat-latte",
background=(239, 241, 245),
foreground=(76, 79, 105),
colors=Colors(
black=(179, 188, 223),
cyan=(148, 226, 213),
yellow=(249, 226, 175),
blue=(135, 176, 249),
red=(243, 139, 168),
white=(86, 89, 112),
green=(166, 227, 161),
magenta=(245, 194, 231),
),
bright_colors=Colors(
black=(161, 168, 201),
cyan=(148, 226, 213),
yellow=(249, 226, 175),
blue=(135, 176, 249),
red=(243, 139, 168),
white=(67, 70, 90),
green=(166, 227, 161),
magenta=(245, 194, 231),
),
),
Theme(
name="dracula",
background=parse_rgb_hex("282a36"),
foreground=parse_rgb_hex("f8f8f2"),
colors=Colors(
black=parse_rgb_hex("21222c"),
cyan=parse_rgb_hex("8be9fd"),
yellow=parse_rgb_hex("f1fa8c"),
blue=parse_rgb_hex("bd93f9"),
red=parse_rgb_hex("ff5555"),
green=parse_rgb_hex("50fa7b"),
magenta=parse_rgb_hex("ff79c6"),
white=parse_rgb_hex("f8f8f2"),
),
bright_colors=Colors(
black=parse_rgb_hex("6272a4"),
cyan=parse_rgb_hex("a4ffff"),
red=parse_rgb_hex("ff6e6e"),
yellow=parse_rgb_hex("ffffa5"),
blue=parse_rgb_hex("d6acff"),
green=parse_rgb_hex("69ff94"),
magenta=parse_rgb_hex("ff92df"),
white=parse_rgb_hex("ffffff"),
),
),
Theme(
name="nord",
background=parse_rgb_hex("2e3440"),
foreground=parse_rgb_hex("d8dee9"),
colors=Colors(
black=parse_rgb_hex("3b4252"),
red=parse_rgb_hex("bf616a"),
green=parse_rgb_hex("a3be8c"),
yellow=parse_rgb_hex("ebcb8b"),
blue=parse_rgb_hex("81a1c1"),
magenta=parse_rgb_hex("b48ead"),
cyan=parse_rgb_hex("88c0d0"),
white=parse_rgb_hex("e5e9f0"),
),
bright_colors=Colors(
black=parse_rgb_hex("4c566a"),
red=parse_rgb_hex("bf616a"),
green=parse_rgb_hex("a3be8c"),
yellow=parse_rgb_hex("ebcb8b"),
blue=parse_rgb_hex("81a1c1"),
magenta=parse_rgb_hex("b48ead"),
cyan=parse_rgb_hex("8fbcbb"),
white=parse_rgb_hex("eceff4"),
),
),
Theme(
name="gruvbox-dark",
## dark
background=parse_rgb_hex("282828"),
foreground=parse_rgb_hex("d5c4a1"),
# Normal color"),
colors=Colors(
black=parse_rgb_hex("282828"),
red=parse_rgb_hex("fb4934"),
green=parse_rgb_hex("b8bb26"),
yellow=parse_rgb_hex("fabd2f"),
blue=parse_rgb_hex("83a598"),
magenta=parse_rgb_hex("d3869b"),
cyan=parse_rgb_hex("8ec07c"),
white=parse_rgb_hex("d5c4a1"),
),
bright_colors=Colors(
black=parse_rgb_hex("665c54"),
red=parse_rgb_hex("fe8019"),
green=parse_rgb_hex("3c3836"),
yellow=parse_rgb_hex("504945"),
blue=parse_rgb_hex("bdae93"),
magenta=parse_rgb_hex("ebdbb2"),
cyan=parse_rgb_hex("d65d0e"),
white=parse_rgb_hex("fbf1c7"),
),
),
Theme(
name="gruvbox-light",
background=parse_rgb_hex("fbf1c7"),
foreground=parse_rgb_hex("504945"),
colors=Colors(
black=parse_rgb_hex("fbf1c7"),
red=parse_rgb_hex("9d0006"),
green=parse_rgb_hex("79740e"),
yellow=parse_rgb_hex("b57614"),
blue=parse_rgb_hex("076678"),
magenta=parse_rgb_hex("8f3f71"),
cyan=parse_rgb_hex("427b58"),
white=parse_rgb_hex("504945"),
),
bright_colors=Colors(
black=parse_rgb_hex("bdae93"),
red=parse_rgb_hex("af3a03"),
green=parse_rgb_hex("ebdbb2"),
yellow=parse_rgb_hex("d5c4a1"),
blue=parse_rgb_hex("665c54"),
magenta=parse_rgb_hex("3c3836"),
cyan=parse_rgb_hex("d65d0e"),
white=parse_rgb_hex("282828"),
),
),
]
THEMES = { THEMES = {
**{ # **{
theme.name: TerminalTheme( # theme.name: TerminalTheme(
theme.background, # theme.background,
theme.foreground, # theme.foreground,
theme.colors, # theme.colors,
theme.bright_colors, # theme.bright_colors,
) # )
for theme in THEME_DEFINITIONS # for theme in THEME_DEFINITIONS
}, #
# },
**{name: load_theme(name) for name in get_builtin_themes()},
**{ **{
"monokai": MONOKAI, "monokai": MONOKAI,
"dimmed_monokai": DIMMED_MONOKAI, "dimmed_monokai": DIMMED_MONOKAI,

View file

@ -0,0 +1 @@
{"name": "cat-frappe", "format": "rgb", "foreground": [198, 206, 239], "background": [48, 52, 70], "colors": [[179, 188, 223], [243, 139, 168], [166, 227, 161], [249, 226, 175], [135, 176, 249], [245, 194, 231], [148, 226, 213], [86, 89, 112]], "bright_colors": [[161, 168, 201], [243, 139, 168], [166, 227, 161], [249, 226, 175], [135, 176, 249], [245, 194, 231], [148, 226, 213], [67, 70, 90]]}

View file

@ -0,0 +1 @@
{"name": "cat-latte", "format": "rgb", "foreground": [76, 79, 105], "background": [239, 241, 245], "colors": [[179, 188, 223], [243, 139, 168], [166, 227, 161], [249, 226, 175], [135, 176, 249], [245, 194, 231], [148, 226, 213], [86, 89, 112]], "bright_colors": [[161, 168, 201], [243, 139, 168], [166, 227, 161], [249, 226, 175], [135, 176, 249], [245, 194, 231], [148, 226, 213], [67, 70, 90]]}

View file

@ -0,0 +1 @@
{"name": "cat-macchiato", "format": "rgb", "foreground": [197, 207, 245], "background": [36, 39, 58], "colors": [[179, 188, 223], [243, 139, 168], [166, 227, 161], [249, 226, 175], [135, 176, 249], [245, 194, 231], [148, 226, 213], [86, 89, 112]], "bright_colors": [[161, 168, 201], [243, 139, 168], [166, 227, 161], [249, 226, 175], [135, 176, 249], [245, 194, 231], [148, 226, 213], [67, 70, 90]]}

View file

@ -0,0 +1 @@
{"name": "cat-mocha", "format": "rgb", "foreground": [198, 208, 245], "background": [30, 30, 46], "colors": [[179, 188, 223], [243, 139, 168], [166, 227, 161], [249, 226, 175], [135, 176, 249], [245, 194, 231], [148, 226, 213], [86, 89, 112]], "bright_colors": [[161, 168, 201], [243, 139, 168], [166, 227, 161], [249, 226, 175], [135, 176, 249], [245, 194, 231], [148, 226, 213], [67, 70, 90]]}

View file

@ -0,0 +1 @@
{"name": "dracula", "format": "rgb", "foreground": [248, 248, 242], "background": [40, 42, 54], "colors": [[33, 34, 44], [255, 85, 85], [80, 250, 123], [241, 250, 140], [189, 147, 249], [255, 121, 198], [139, 233, 253], [248, 248, 242]], "bright_colors": [[98, 114, 164], [255, 110, 110], [105, 255, 148], [255, 255, 165], [214, 172, 255], [255, 146, 223], [164, 255, 255], [255, 255, 255]]}

View file

@ -0,0 +1 @@
{"name": "gruvbox-dark", "format": "rgb", "foreground": [213, 196, 161], "background": [40, 40, 40], "colors": [[40, 40, 40], [251, 73, 52], [184, 187, 38], [250, 189, 47], [131, 165, 152], [211, 134, 155], [142, 192, 124], [213, 196, 161]], "bright_colors": [[102, 92, 84], [254, 128, 25], [60, 56, 54], [80, 73, 69], [189, 174, 147], [235, 219, 178], [214, 93, 14], [251, 241, 199]]}

View file

@ -0,0 +1 @@
{"name": "gruvbox-light", "format": "rgb", "foreground": [80, 73, 69], "background": [251, 241, 199], "colors": [[251, 241, 199], [157, 0, 6], [121, 116, 14], [181, 118, 20], [7, 102, 120], [143, 63, 113], [66, 123, 88], [80, 73, 69]], "bright_colors": [[189, 174, 147], [175, 58, 3], [235, 219, 178], [213, 196, 161], [102, 92, 84], [60, 56, 54], [214, 93, 14], [40, 40, 40]]}

View file

@ -0,0 +1 @@
{"name": "nord", "format": "rgb", "foreground": [216, 222, 233], "background": [46, 52, 64], "colors": [[59, 66, 82], [191, 97, 106], [163, 190, 140], [235, 203, 139], [129, 161, 193], [180, 142, 173], [136, 192, 208], [229, 233, 240]], "bright_colors": [[76, 86, 106], [191, 97, 106], [163, 190, 140], [235, 203, 139], [129, 161, 193], [180, 142, 173], [143, 188, 187], [236, 239, 244]]}