-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_colorschemes.py
88 lines (66 loc) · 2.1 KB
/
generate_colorschemes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from dataclasses import dataclass
from pathlib import Path
import tomllib
def main() -> None:
palette = parse_palette(Path("templates/palette.toml"))
for name in ("couleurs", "gris"):
highlights = parse_colorscheme(Path(f"templates/{name}.toml"))
write_colorscheme(name, palette, highlights)
@dataclass
class Palette:
dark: dict[str, str]
light: dict[str, str]
@dataclass
class Highlight:
name: str
link: str = ""
fg: str = ""
bg: str = ""
gui: str = ""
guisp: str = ""
def parse_palette(path: Path) -> Palette:
with path.open(mode="rb") as f:
data = tomllib.load(f)
dark, light = data["dark"], data["light"]
dark["NONE"] = light["NONE"] = "NONE"
return Palette(dark=dark, light=light)
def parse_colorscheme(path: Path) -> list[Highlight]:
with path.open("rb") as f:
data = tomllib.load(f)
return [Highlight(name, **attrs) for name, attrs in data.items()]
def hl_cmd(colors: dict[str, str], hl: Highlight) -> str:
if hl.link:
return f"hi! link {hl.name} {hl.link}"
attrs = {
"guifg": colors.get(hl.fg),
"guibg": colors.get(hl.bg),
"gui": hl.gui,
"guisp": colors.get(hl.guisp),
}
cmd = f"hi {hl.name}"
for k, v in attrs.items():
if v:
cmd += f" {k}={v}"
return cmd
def write_colorscheme(name: str, palette: Palette, highlights: list[Highlight]) -> None:
preamble = [
"hi clear",
"if exists('syntax_on')",
" syntax reset",
"endif",
f"let g:colors_name='{name}'",
]
dark_hl_cmds = [hl_cmd(palette.dark, hl) for hl in highlights]
light_hl_cmds = [hl_cmd(palette.light, hl) for hl in highlights]
with open(f"colors/{name}.vim", "w", encoding="utf-8") as f:
for line in preamble:
f.write(f"{line}\n")
f.write("if &background ==# 'dark'\n")
for line in dark_hl_cmds:
f.write(f" {line}\n")
f.write("else\n")
for line in light_hl_cmds:
f.write(f" {line}\n")
f.write("endif\n")
if __name__ == "__main__":
main()