easyconf-lemonbar/easyconf_lemonbar/Module.py

114 lines
4.4 KiB
Python
Raw Permalink Normal View History

2023-03-11 22:51:30 +00:00
from threading import Thread
from subprocess import run
from time import sleep
from signal import SIGRTMIN, SIGUSR1, sigtimedwait, sigwait
2023-03-11 22:51:30 +00:00
class Module:
# All text that the module contains. Will be re-generated every time the
# module refreshes.
text = ""
# Parse the raw module configuration
2023-03-11 23:52:47 +00:00
def __init__(self, module_config, padding, bar):
2023-03-11 22:51:30 +00:00
# Attributes that will be extracted from the config
self.name = ""
2023-03-12 02:20:00 +00:00
self.signal = 0
2023-03-11 22:51:30 +00:00
self.alignment = ""
self.pre = ""
self.prefix = ""
self.command = ""
self.refresh = 0
self.post =""
self.padding = padding
2023-03-11 23:52:47 +00:00
# We pass in the Bar which contains the modules we can print it.
self.bar = bar
2023-03-11 22:51:30 +00:00
for option in module_config:
match option:
case "name":
self.name = module_config[option]
case "command":
self.command = module_config[option]
case "refresh":
2023-03-12 02:20:00 +00:00
self.refresh = module_config[option]/1000
2023-03-11 22:51:30 +00:00
case "prefix":
self.prefix = module_config[option]
2023-03-12 02:20:00 +00:00
case "signal":
self.signal = SIGRTMIN + module_config[option]
2023-03-11 22:51:30 +00:00
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
2023-03-11 22:51:30 +00:00
def thread_callback(self):
while True:
# Generate new text
self.generate_text()
2023-03-11 23:52:47 +00:00
# Print the entire bar
2023-03-15 01:29:25 +00:00
self.bar.print()
2023-03-12 02:20:00 +00:00
if self.signal == 0:
if self.refresh == 0:
break
sleep(self.refresh)
else:
if self.refresh == 0:
sigwait([self.signal])
else:
2023-03-15 01:29:25 +00:00
sigtimedwait([self.signal], self.refresh)
2023-03-11 22:51:30 +00:00
def start_thread(self):
thread = Thread(target=self.thread_callback)
thread.start()
self.ident = thread.ident