easyconf-lemonbar/main.py

70 lines
2.2 KiB
Python

from threading import Thread
from subprocess import run
from time import sleep
from config_parsing import get_validated_config, parse_module
running_modules_dict = {"left": {}, "center": {}, "right": {}}
def print_bar():
for alignment in running_modules_dict:
match alignment:
case "left":
print("%{l}", end="")
case "center":
print("%{c}", end="")
case "right":
print("%{r}", end="")
# convert group to list so we can access subsequent items
group = list(running_modules_dict[alignment].values())
for index, module in enumerate(group):
print(module, end="")
# IF module is not the last AND it has text
if index != (len(group) - 1) and len(module) > 0:
# THEN only print seperator if the module is eventually followed
# by a module with text
for i in range(index + 1, len(group)):
if len(group[i]) > 0:
print(seperator, end="")
break;
print(flush=True)
def create_module_string(pre, prefix, command, post):
cmd_output = run(command, shell=True, capture_output=True, text=True).stdout.strip()
if cmd_output != "":
return (pre + padding + prefix + cmd_output + padding + post)
else:
return cmd_output
def new_module_thread(alignment, pre, name, prefix, command, post, refresh):
running_modules_dict[alignment][name] = ""
while True:
module_string = create_module_string(pre, prefix, command, post)
running_modules_dict[alignment][name] = module_string
print_bar()
if refresh == 0:
break
sleep(refresh/1000)
def main():
global padding
global seperator
config = get_validated_config("./testing_config.yml")
padding = config["bar"]["config"]["padding"]
seperator = config["bar"]["config"]["seperator"]
modules = config["bar"]["modules"]
for module in modules:
parameters = parse_module(module)
# print(parameters)
x = Thread(target=new_module_thread, args=parameters)
x.start()
if __name__ == "__main__":
main()