-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathtab-styles.py
More file actions
executable file
·173 lines (130 loc) · 4.83 KB
/
tab-styles.py
File metadata and controls
executable file
·173 lines (130 loc) · 4.83 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import colorsys
from typing import Tuple
import subprocess
import glob
import sys
import json
RELEVANT_CONFIGS = ("active_tab_foreground", "active_tab_background",
"inactive_tab_foreground", "inactive_tab_background")
CONFIG_HEADER = (
"# START_AUTOGENERATED_TAB_STYLE\n"
"# Feel free to update these colors manually and remove these comments.\n"
)
CONFIG_FOOTER = "# END_AUTOGENERATED_TAB_STYLE\n"
def hex_to_hls(hex_color) -> Tuple[float, float, float]:
"""Returns values from 0 to 1."""
hex_color = hex_color.lstrip('#')
r = int(hex_color[0:2], 16) / 255.0
g = int(hex_color[2:4], 16) / 255.0
b = int(hex_color[4:6], 16) / 255.0
return colorsys.rgb_to_hls(r, g, b)
def rgb_to_hex(r: float, g: float, b: float) -> str:
"""Returns the #FF00C3 format"""
r_int = int(round(r * 255))
g_int = int(round(g * 255))
b_int = int(round(b * 255))
return "#{:02x}{:02x}{:02x}".format(r_int, g_int, b_int)
def parse_themes(paths):
cp = subprocess.run(['kitten', '__parse_theme_metadata__'],
input='\n'.join(paths).encode('utf-8'), capture_output=True)
if cp.returncode != 0:
print(cp.stderr.decode('utf-8'), file=sys.stderr)
raise SystemExit(cp.returncode)
return json.loads(cp.stdout)
def should_update(theme) -> bool:
if theme["name"] == "Default":
return False
if not theme.get("is_dark"):
return False
with open(theme["file"], "r") as f:
content = f.read()
if CONFIG_HEADER in content:
# We want to update since maybe the update algorithm
# changed.
return True
# If any of those configs is set, we will assume the style is
# intentional and won't touch anything.
return not any(conf in content for conf in RELEVANT_CONFIGS)
def make_darker(hex_color: str) -> str:
hh, ll, ss = hex_to_hls(hex_color)
rr, gg, bb = colorsys.hls_to_rgb(hh, ll * 0.8, ss)
return rgb_to_hex(rr, gg, bb)
def make_lighter(hex_color: str) -> str:
hh, ll, ss = hex_to_hls(hex_color)
rr, gg, bb = colorsys.hls_to_rgb(hh, min(ll * 1.2, 1), ss)
return rgb_to_hex(rr, gg, bb)
def get_lightness(hex_color: str) -> float:
_, ll, _ = hex_to_hls(hex_color)
return ll
def update(theme) -> None:
print(f"Updating {theme['name']}...")
with open(theme["file"]) as f:
content = f.read()
lines = content.splitlines()
foreground = None
background = None
selection_background = None
# selection_foreground = None
# Extract relevant settings from the theme.
for line in lines:
line = line.strip()
if not line or line.startswith("#"):
continue
parts = line.split()
if len(parts) != 2:
continue
key, value = parts
if value == "none":
continue
match key:
case "foreground":
foreground = value
case "background":
background = value
case "selection_background":
selection_background = value
# case "selection_foreground":
# selection_foreground = value
if not foreground or not background:
print(f"!! Could not update {theme['name']} (no fg / bg).")
return
if selection_background:
active_tab_background = selection_background
else:
active_tab_background = make_lighter(background)
active_tab_foreground = "#eeeeee"
if get_lightness(active_tab_background) > 0.7:
active_tab_foreground = "#444444"
inactive_tab_foreground = foreground
inactive_tab_background = make_darker(background)
assert not (
active_tab_foreground == inactive_tab_foreground and
active_tab_background == inactive_tab_background
)
config_values = CONFIG_HEADER + f"""active_tab_foreground {active_tab_foreground}
active_tab_background {active_tab_background}
inactive_tab_foreground {inactive_tab_foreground}
inactive_tab_background {inactive_tab_background}
""" + CONFIG_FOOTER
if CONFIG_HEADER in content:
# We need to fix up the exiting stuff.
assert CONFIG_FOOTER in content
pre_content, _, config_post_content = content.partition(CONFIG_HEADER)
_, _, post_content = config_post_content.partition(CONFIG_FOOTER)
with open(theme["file"], "w") as f:
f.write(pre_content + config_values + post_content)
else:
# Just append to end of file.
with open(theme["file"], "a") as f:
f.write("\n" + config_values)
def main() -> None:
files = glob.glob('themes/*.conf')
parsed = parse_themes(files)
for theme, td in zip(files, parsed):
td['file'] = theme
for theme in parsed:
if should_update(theme):
update(theme)
print("\nDone :>")
if __name__ == "__main__":
main()