from yaml import safe_load from io import open from threading import Thread from subprocess import run from time import sleep running_modules_dict = {"left": {}, "center": {}, "right": {}} def print_modules(): 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) def new_module_thread(alignment, name, cmd, refresh, pre, post): while True: result = pre result += run(cmd, shell=True, capture_output=True, text=True).stdout.strip() result += post running_modules_dict[alignment][name] = result print_modules() sleep(refresh/1000) 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: match line_option: 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: config_file = safe_load(file) modules = config_file["bar"]["modules"] for module in modules: parameters = parse_module(modules[module]) parameters.insert(0, module) alignment = modules[module]["format"]["align"] parameters.insert(0, alignment) running_modules_dict[alignment][module] = '' print(parameters) x = Thread(target=new_module_thread, args=parameters) x.start()