easyconf-lemonbar/main.py

63 lines
1.9 KiB
Python
Raw Normal View History

2023-02-12 20:09:01 +00:00
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="")
group = running_modules_dict[alignment]
for index, module in enumerate(group):
print(group[module], end="")
2023-02-12 21:55:18 +00:00
if index != (len(group) - 1) and len(group[module]) > 0:
2023-02-12 20:09:01 +00:00
print(seperator, end="")
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
2023-02-12 20:09:01 +00:00
def new_module_thread(alignment, pre, name, prefix, command, post, refresh):
2023-02-12 21:54:06 +00:00
running_modules_dict[alignment][name] = ""
2023-02-12 20:09:01 +00:00
while True:
module_string = create_module_string(pre, prefix, command, post)
running_modules_dict[alignment][name] = module_string
print_bar()
2023-02-18 19:14:08 +00:00
if refresh == 0:
break
2023-02-12 20:09:01 +00:00
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()