Files
dotfiles/powerline/kitty_theme.py
2021-09-23 11:54:04 -07:00

88 lines
2.2 KiB
Python

#!/usr/bin/env python3
import re
import json
from pathlib import Path
INDENT = 2
SPACE = " "
NEWLINE = "\n"
def to_json(o, level=0):
ret = ""
if isinstance(o, dict):
ret += "{" + NEWLINE
comma = ""
for k, v in o.items():
ret += comma
comma = ",\n"
ret += SPACE * INDENT * (level+1)
ret += '"' + str(k) + '":' + SPACE
ret += to_json(v, level + 1)
ret += NEWLINE + SPACE * INDENT * level + "}"
elif isinstance(o, str):
ret += '"' + o + '"'
elif isinstance(o, list):
ret += "[" + ",".join([to_json(e, level+1) for e in o]) + "]"
elif isinstance(o, bool):
ret += "true" if o else "false"
elif isinstance(o, int):
ret += str(o)
elif isinstance(o, float):
ret += '%.7g' % o
elif o is None:
ret += 'null'
else:
raise TypeError("Unknown type '%s' for json serialization" % str(type(o)))
return ret
def cterm(cname: str) -> int:
if cname == "background":
return 0
if cname == "foreground":
return 15
if cname == "selection_background":
return 2
if cname == "selection_foreground":
return 15
if cname == "cursor":
return 2
if "color" in cname:
return int(cname.strip("color"))
def parse_kitty_theme(name: str, theme: str) -> dict:
d = {}
for line in theme.splitlines():
colorname, colorhex = line.split()
d[f"kitty:{colorname}"] = [cterm(colorname), colorhex[1:]]
return d
def get_kitty_theme() -> Path:
kitty_conf = Path.home() / ".config" / "kitty" / "kitty.conf"
txt = kitty_conf.read_text()
match = re.search(r"include (\./themes/.*.conf)", txt)
theme_name = match.groups()[0]
print("theme_name: ", theme_name)
theme_path = kitty_conf.parent / theme_name
print("theme_path: ", theme_path)
return theme_path
if __name__ == "__main__":
f = get_kitty_theme()
colors_file = Path(__file__).parent / "colors.json"
powerline_colors = json.loads(colors_file.read_text())
kitty_colors = parse_kitty_theme(f.stem, f.read_text())
powerline_colors["colors"].update(kitty_colors)
colors_file.write_text(to_json(powerline_colors))