-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviolet.py
548 lines (494 loc) · 19.5 KB
/
violet.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/usr/bin/env python3
"""Provide utility functions for Tmux option reading and writing."""
import shlex
import subprocess
import yaml
from loguru import logger
from yaml import Loader
import palette
UTF_8 = "utf-8"
EMPTY = ""
# tmux options
STYLE_START = "#["
STYLE_END = "]"
def get(_dict, key, default):
"""'Rewrite' get method of dict.
When value is None or Empty, use default.
Should be used in theme configuration only.
Lower theme configuration could reuse upper level them configuration, then
it's possible to configure less items.
However, user configuration should be able to overwrite theme
configuration.
For example, if theme defined icons, but user don't want to use icon, then
they can set icon as Empty.
"""
value = _dict.get(key)
if value is None or value.strip() == EMPTY:
value = default
return value
class Theme(dict):
"""Wrapper for theme configuration."""
def __init__(self, theme_config: dict):
self.status_line = theme_config.get("status_line")
self.status_left = ThemeStatusLeft(theme_config)
self.active_window = ThemeWindow(
self.status_line, theme_config.get("window").get("active")
)
self.inactive_window = ThemeWindow(
self.status_line, theme_config.get("window").get("inactive")
)
self.window = {
"active": self.active_window,
"inactive": self.inactive_window,
}
self.status_right = ThemeStatusRight(theme_config)
super().__init__(
status_line=self.status_line,
status_left=self.status_left,
window=self.window,
status_right=self.status_right,
)
class ThemeStatusLeft(dict):
"""Wrapper for status_left configuration."""
def __init__(self, theme_config):
"""Constructor"""
status_line = theme_config.get("status_line")
status_left = theme_config.get("status_left")
self.fg_option = get(
status_left, "fg_option", status_line.get("foreground")
)
self.bg_option = get(
status_left, "bg_option", status_line.get("background")
)
self.fg_icon = get(
status_left, "fg_icon", status_line.get("foreground")
)
self.bg_icon = get(
status_left, "bg_icon", status_line.get("background")
)
self.fg_decorator = status_left.get(
"fg_decorator", status_line.get("foreground")
)
self.bg_decorator = get(
status_left, "bg_decorator", status_line.get("background")
)
self.icon = get(status_left, "icon", status_line.get("left_icon"))
self.decorator = status_left.get(
"decorator", status_line.get("left_decorator")
)
self.style = get(status_left, "style", status_line.get("style"))
super().__init__(
fg_option=self.fg_option,
bg_option=self.bg_option,
fg_icon=self.fg_icon,
bg_icon=self.bg_icon,
fg_decorator=self.fg_decorator,
bg_decorator=self.bg_decorator,
icon=self.icon,
decorator=self.decorator,
style=self.style,
)
class ThemeWindow(dict):
"""Wrapper for window configuration."""
def __init__(self, status_line_theme_config, window_theme_config):
"""Constructor"""
self.fg_window = get(
window_theme_config,
"fg_window",
status_line_theme_config.get("foreground"),
)
self.bg_window = get(
window_theme_config,
"bg_window",
status_line_theme_config.get("background"),
)
self.fg_window_index = get(
window_theme_config,
"fg_window_index",
status_line_theme_config.get("foreground"),
)
self.bg_window_index = get(
window_theme_config,
"bg_window_index",
status_line_theme_config.get("background"),
)
self.fg_icon = get(
window_theme_config,
"fg_icon",
status_line_theme_config.get("foreground"),
)
self.bg_icon = get(
window_theme_config,
"bg_icon",
status_line_theme_config.get("background"),
)
self.fg_decorator = get(
window_theme_config,
"fg_decorator",
status_line_theme_config.get("foreground"),
)
self.bg_decorator = get(
window_theme_config,
"bg_decorator",
status_line_theme_config.get("background"),
)
self.icon = get(
window_theme_config,
"icon",
status_line_theme_config.get("left_icon"),
)
self.decorator = get(
window_theme_config,
"decorator",
status_line_theme_config.get("left_decorator"),
)
self.style = get(
window_theme_config, "style", status_line_theme_config.get("style")
)
super().__init__(
fg_window=self.fg_window,
bg_window=self.bg_window,
fg_window_index=self.fg_window_index,
bg_window_index=self.bg_window_index,
fg_icon=self.fg_icon,
bg_icon=self.bg_icon,
fg_decorator=self.fg_decorator,
bg_decorator=self.bg_decorator,
icon=self.icon,
decorator=self.decorator,
style=self.style,
)
class ThemeStatusRight(dict):
"""Wrapper for status_right configuration."""
def __init__(self, theme_config):
"""Constructor"""
status_line = theme_config.get("status_line")
status_right = theme_config.get("status_right")
self.fg_option = get(
status_right, "fg_option", status_line.get("foreground")
)
self.bg_option = get(
status_right, "bg_option", status_line.get("background")
)
self.fg_icon = get(
status_right, "fg_icon", status_line.get("foreground")
)
self.bg_icon = get(
status_right, "bg_icon", status_line.get("background")
)
self.fg_decorator = get(
status_right, "fg_decorator", status_line.get("foreground")
)
self.bg_decorator = get(
status_right, "bg_decorator", status_line.get("background")
)
self.icon = get(status_right, "icon", status_line.get("left_icon"))
self.decorator = get(
status_right, "decorator", status_line.get("left_decorator")
)
self.style = get(status_right, "style", status_line.get("style"))
super().__init__(
fg_option=self.fg_option,
bg_option=self.bg_option,
fg_icon=self.fg_icon,
bg_icon=self.bg_icon,
fg_decorator=self.fg_decorator,
bg_decorator=self.bg_decorator,
icon=self.icon,
decorator=self.decorator,
style=self.style,
)
class Constructor:
"""Constructor for status line component."""
def __init__(self, catppuccin: dict, theme: Theme):
"""Constructor"""
self.general = catppuccin.get("general")
self.status_line = catppuccin.get(
"status_line", theme.get("status_line")
)
self.foreground = self.status_line.get("foreground")
self.background = self.status_line.get("background")
self.status_left = catppuccin.get("status_left")
self.window = catppuccin.get("window")
self.status_right = catppuccin.get("status_right")
self.theme = theme
def produce_general_options_commands(self):
"""Produce general options."""
general = []
for name, value in self.general.get("options").items():
if name.startswith("_"):
name = f"@{name.lstrip('_')}"
general.append(f"set-option -gq {name} '{value}'")
for name, component in self.general.get("styles").items():
if name == "option-commands":
continue
foreground = get(
component, "fg", self.status_line.get("foreground")
)
background = get(
component, "bg", self.status_line.get("background")
)
style = get(component, "style", self.status_line.get("style"))
style_command = self.get_style_command(
foreground, background, style, name
)
general.append(style_command)
return ";".join(general)
def produce_status_line(self):
"""Produce status line option"""
fg_status_line = self.foreground
bg_status_line = self.background
return f"fg={fg_status_line},bg={bg_status_line}"
def produce_status_left(self):
"""Produce status left option string"""
status_left = []
for component in self.status_left.values():
enabled = component.get("enabled", "on")
if not enabled:
continue
tmux_option = component.get("tmux_option", EMPTY)
icon = component.get("icon", self.theme.status_left.get("icon"))
decorator = component.get(
"decorator", self.theme.status_left.get("decorator")
)
fg_option = component.get(
"fg_option", self.theme.status_left.get("fg_option")
)
bg_option = component.get(
"bg_option", self.theme.status_left.get("bg_option")
)
fg_icon = component.get(
"fg_icon", self.theme.status_left.get("fg_icon")
)
bg_icon = component.get(
"bg_icon", self.theme.status_left.get("bg_icon")
)
fg_decorator = component.get(
"fg_decorator", self.theme.status_left.get("fg_decorator")
)
bg_decorator = component.get(
"bg_decorator", self.theme.status_left.get("bg_decorator")
)
style = component.get("style", self.theme.status_left.get("style"))
tmux_option_style = f"{self.get_style_for_option(fg_option, bg_option, style, tmux_option)}"
icon_style = (
f"{self.get_style_for_option(fg_icon, bg_icon, style, icon)}"
)
decorator_style = f"{self.get_style_for_option(fg_decorator, bg_decorator, style, decorator)}"
component_value = (
f"{icon_style}{decorator_style}{tmux_option_style}"
)
status_left.append(component_value)
return " ".join(status_left)
def produce_window(self):
"""Return tuple with active window and inactive window option strings"""
windows = {}
for name, component in self.window.items():
style = component.get(
"style", self.theme.window.get(name).get("style")
)
icon = component.get(
"icon", self.theme.window.get(name).get("icon")
)
decorator = component.get(
"decorator", self.theme.window.get(name).get("decorator")
)
window_name = component.get(
"window_name", self.theme.window.get(name).get("window_name")
)
window_index = component.get(
"window_index",
self.theme.window.get(name).get("window_index"),
)
decorator = component.get(
"decorator", self.theme.window.get(name).get("decorator")
)
fg_icon = component.get(
"fg_icon", self.theme.window.get(name).get("fg_icon")
)
bg_icon = component.get(
"bg_icon", self.theme.window.get(name).get("bg_icon")
)
fg_decorator = component.get(
"fg_decorator", self.theme.window.get(name).get("fg_decorator")
)
bg_decorator = component.get(
"bg_decorator", self.theme.window.get(name).get("bg_decorator")
)
fg_window = component.get(
"fg_window", self.theme.window.get(name).get("fg_window")
)
bg_window = component.get(
"bg_window", self.theme.window.get(name).get("bg_window")
)
fg_window_index = component.get(
"fg_window_index",
self.theme.window.get(name).get("fg_window_index"),
)
bg_window_index = component.get(
"bg_window_index",
self.theme.window.get(name).get("bg_window_index"),
)
window_style = self.get_style_for_option(
fg_window, bg_window, style, window_name
)
window_index_style = self.get_style_for_option(
fg_window_index, bg_window_index, style, window_index
)
icon_style = self.get_style_for_option(
fg_icon, bg_icon, style, icon
)
decorator_style = self.get_style_for_option(
fg_decorator, bg_decorator, style, decorator
)
component_value = f"{window_style}{window_index_style}{icon_style}{decorator_style} "
windows[name] = component_value
return windows
def produce_status_right(self):
"""Produce status right tmux options string."""
status_right = []
for options in self.status_right.values():
enabled = options.get("enabled", "on")
if not enabled:
continue
tmux_option = options.get("tmux_option", EMPTY)
icon = options.get("icon", self.theme.status_right.get("icon"))
decorator = options.get(
"decorator", self.theme.status_right.get("decorator")
)
fg_option = options.get(
"fg_option", self.theme.status_right.get("fg_option")
)
bg_option = options.get(
"bg_option", self.theme.status_right.get("bg_option")
)
fg_icon = options.get(
"fg_icon", self.theme.status_right.get("fg_icon")
)
bg_icon = options.get(
"bg_icon", self.theme.status_right.get("bg_icon")
)
fg_decorator = options.get(
"fg_decorator", self.theme.status_right.get("fg_decorator")
)
bg_decorator = options.get(
"bg_decorator", self.theme.status_right.get("bg_decorator")
)
style = options.get("style", self.theme.status_right.get("style"))
tmux_option_style = f"{self.get_style_for_option(fg_option, bg_option, style, tmux_option)}"
icon_style = (
f"{self.get_style_for_option(fg_icon, bg_icon, style, icon)}"
)
decorator_style = f"{self.get_style_for_option(fg_decorator, bg_decorator, style, decorator)}"
component_value = (
f"{decorator_style}{icon_style}{tmux_option_style}"
)
status_right.append(component_value)
return " ".join(status_right)
def get_style_for_option(self, foreground, background, style, option):
"""construct style string with foreground and background"""
if option is None:
option = ""
pieces = []
if foreground:
pieces.append(f"fg={foreground}")
if background:
pieces.append(f"bg={background}")
pieces.append(style)
_style = f"{STYLE_START}{','.join(pieces)}{STYLE_END}{option}"
pieces = []
if foreground:
pieces.append(f"fg={self.foreground}")
if background:
pieces.append(f"bg={self.background}")
pieces.append(style)
_default_style = f"{STYLE_START}{','.join(pieces)}{STYLE_END}"
return f"{_style}{_default_style}"
def get_style_command(self, foreground, background, style, style_name):
return f"set-option {style_name} 'fg={foreground},bg={background},{style}'"
def produce_option_command(self, option, value):
"""Return tmux set option command"""
return f"set-option -gq {option} '{value}'"
def produce_option_commands(self):
"""Return all tmux set option commands."""
status_line = self.produce_status_line()
status_left = self.produce_status_left()
window = self.produce_window()
status_right = self.produce_status_right()
status_line_cmd = self.produce_option_command(
"status-style", status_line
)
status_left_cmd = self.produce_option_command(
"status-left", status_left
)
active_window_cmd = self.produce_option_command(
"window-status-current-format", window["active"]
)
inactive_window_cmd = self.produce_option_command(
"window-status-format", window["inactive"]
)
status_right_cmd = self.produce_option_command(
"status-right", status_right
)
general_commands = self.produce_general_options_commands()
option_commands = []
option_commands.append(general_commands)
option_commands.append(status_line_cmd)
option_commands.append(status_left_cmd)
option_commands.append(active_window_cmd)
option_commands.append(inactive_window_cmd)
option_commands.append(status_right_cmd)
return option_commands
def violet(config_file="violet.yaml"):
"""Load config file, overwrite options by value from tmux.conf."""
# TODO: the config file should be customizable by putting under
# $HOME/.tmux/catppuccin.yaml
set_option_commands = []
dynamic_config_file_name = get_tmux_option(
"dynamic_config_file_name", config_file
)
with open(dynamic_config_file_name, "r", encoding=UTF_8) as config:
catppuccin = yaml.load(config, Loader=Loader)
theme_name = catppuccin.get("theme")
dynamic_theme_name = get_tmux_option("dynamic_theme_name", theme_name)
theme_filename = f"{dynamic_theme_name}.theme.yaml"
with open(theme_filename, "r", encoding=UTF_8) as theme_file:
theme_config = yaml.load(theme_file, Loader=Loader)
theme = Theme(theme_config)
constructor = Constructor(catppuccin, theme)
set_option_commands = constructor.produce_option_commands()
return ";".join(set_option_commands)
def main():
"""Run"""
set_option_commands = violet()
if set_option_commands:
for command in set_option_commands.split(";"):
run_shell_command(f"tmux {command}")
def get_tmux_option(option_name, default_value):
"""Read tmux option."""
assert option_name is not None, "option_name is None!"
assert default_value is not None, "default_value is None!"
if not option_name.startswith("@"):
option_name = f"@{option_name}"
shell_cmd = f'tmux show-option -gqv "{option_name}"'
return run_shell_command(shell_cmd, default_value)
def run_shell_command(command, default_output=None):
"""Run shell command."""
try:
command_args = shlex.split(command)
value = (
subprocess.check_output(command_args, shell=False)
.decode(UTF_8)
.strip()
)
if value is not None and value.strip() != EMPTY:
return value
return default_output
except Exception:
logger.opt(exception=True).debug(
f"{command} is failed to run. use default value: \
{default_output} as output."
)
return default_output
if __name__ == "__main__":
main()