easyconf-lemonbar/modules.py

102 lines
3.5 KiB
Python
Raw Normal View History

2023-02-11 00:46:56 +00:00
from yaml import safe_load
from io import open
from threading import Thread
from subprocess import run
from time import sleep
2023-02-11 03:17:06 +00:00
running_modules_dict = {"left": {}, "center": {}, "right": {}}
def print_modules():
2023-02-11 03:17:06 +00:00
for alignment in running_modules_dict:
match alignment:
case "left":
print("%{l}", end="")
case "center":
print("%{c}", end="")
case "right":
print("%{r}", end="")
for module in running_modules_dict[alignment]:
print(running_modules_dict[alignment][module], end="")
print(flush=True)
2023-02-11 03:17:06 +00:00
def new_module_thread(alignment, name, cmd, refresh, pre, post):
2023-02-11 00:46:56 +00:00
while True:
result = pre
result += run(cmd, shell=True, capture_output=True, text=True).stdout.strip()
result += post
2023-02-11 03:17:06 +00:00
running_modules_dict[alignment][name] = result
print_modules()
2023-02-11 00:46:56 +00:00
sleep(refresh/1000)
2023-02-11 03:17:06 +00:00
def parse_module(module):
cmd = module["command"]
refresh = module["refresh"]
pre = ""
post = ""
format_options = module["format"]
for option in format_options:
match option:
# Align is handled differently. See below.
# case "align":
case "offset":
pre += ("%{O" + str(format_options[option]) + "}")
case "bg_color":
pre += ("%{B" + format_options[option] + "}")
post += "%{B-}"
case "fg_color":
pre += ("%{F" + format_options[option] + "}")
post += "%{F-}"
case "font":
pre += ("%{T" + format_options[option] + "}")
post += "%{T-}"
case "line":
line_options = format_options[option]
for line_option in line_options:
2023-02-11 03:26:06 +00:00
match line_option:
2023-02-11 03:17:06 +00:00
case "type":
if line_options[line_option] == "underline":
pre += ("%{+u}")
post += ("%{-u}")
elif line_options[line_option] == "overline":
pre += ("%{+o}")
post += ("%{-o}")
case "color":
pre += ("%{U" + line_options[line_option] + "}")
post += "%{U-}"
case "button":
button_options = format_options[option]
button = 1
match button_options["activator"]:
case "left":
button = 1
case "middle":
button = 2
case "right":
button = 3
case "scrup":
button = 4
case "scrdown":
button = 5
pre += ("%{A" + str(button)+ ":" + button_options["command"] + ":}")
post += "%{A}"
return [cmd, refresh, pre, post]
with open("./testing_config.yml", "r", encoding="utf8") as file:
2023-02-11 00:46:56 +00:00
config_file = safe_load(file)
2023-02-11 03:17:06 +00:00
modules = config_file["bar"]["modules"]
2023-02-11 00:46:56 +00:00
for module in modules:
2023-02-11 03:17:06 +00:00
parameters = parse_module(modules[module])
parameters.insert(0, module)
2023-02-11 03:17:06 +00:00
alignment = modules[module]["format"]["align"]
parameters.insert(0, alignment)
running_modules_dict[alignment][module] = ''
print(parameters)
2023-02-11 03:17:06 +00:00
x = Thread(target=new_module_thread, args=parameters)
2023-02-11 00:46:56 +00:00
x.start()