from threading import Thread from subprocess import run from time import sleep from signal import SIGRTMIN, SIGUSR1, sigtimedwait, sigwait class Module: # All text that the module contains. Will be re-generated every time the # module refreshes. text = "" # Parse the raw module configuration def __init__(self, module_config, padding, bar): # Attributes that will be extracted from the config self.name = "" self.signal = 0 self.alignment = "" self.pre = "" self.prefix = "" self.command = "" self.refresh = 0 self.post ="" self.padding = padding # We pass in the Bar which contains the modules we can print it. self.bar = bar for option in module_config: match option: case "name": self.name = module_config[option] case "command": self.command = module_config[option] case "refresh": self.refresh = module_config[option]/1000 case "prefix": self.prefix = module_config[option] case "signal": self.signal = SIGRTMIN + module_config[option] format_options = module_config["format"] for option in format_options: match option: case "align": self.alignment = format_options[option] case "offset": self.pre = ("%{O" + str(format_options[option]) + "}") + self.pre case "bg_color": self.pre = ("%{B" + format_options[option] + "}") + self.pre self.post += "%{B-}" case "fg_color": self.pre = ("%{F" + format_options[option] + "}") + self.pre self.post += "%{F-}" case "font": self.pre = ("%{T" + format_options[option] + "}") + self.pre self.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": self.pre = ("%{+u}") + self.pre self.post += ("%{-u}") elif line_options[line_option] == "overline": self.pre = ("%{+o}") + self.pre self.post += ("%{-o}") case "color": self.pre = ("%{U" + line_options[line_option] + "}") + self.pre self.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 self.pre = ("%{A" + str(button)+ ":" + button_options["command"] + ":}") + self.pre self.post += "%{A}" def generate_text(self): command_output = run(self.command, shell=True, capture_output=True, text=True).stdout.strip() if len(command_output) == 0: self.text = "" else: self.text = self.pre + self.padding + self.prefix + command_output + self.padding + self.post def thread_callback(self): while True: # Generate new text self.generate_text() # Print the entire bar self.bar.print() if self.signal == 0: if self.refresh == 0: break sleep(self.refresh) else: if self.refresh == 0: sigwait([self.signal]) else: sigtimedwait([self.signal], self.refresh) def start_thread(self): thread = Thread(target=self.thread_callback) thread.start() self.ident = thread.ident