alignment works properly

This commit is contained in:
Noah Swerhun 2023-02-10 21:17:06 -06:00
parent 55f21b2180
commit dbfd6543bb
3 changed files with 94 additions and 27 deletions

View file

@ -13,22 +13,22 @@ bar:
offset: 0 # vertical offset of text in pixels. can be negative. offset: 0 # vertical offset of text in pixels. can be negative.
line_color: '#ff0000' # default underline color line_color: '#ff0000' # default underline color
modules: # bar modules modules: # bar modules
time: # module name can be anything you want time: # module name can be anything you want
command: 'date' # command to be run command: 'date' # command to be run
refresh: 1000 # how often to refresh in milliseconds refresh: 1000 # how often to refresh in milliseconds
bg_color: '#000' # color of the bar behind the text of this module format:
fg_color: '#FFF' # color of the text itself bg_color: '#000' # color of the bar behind the text of this module
align: left # left, right, or center fg_color: '#FFF' # color of the text itself
font: 'fontname' # the name of the font for this specific module align: left # left, right, or center.
line: font: 'fontname' # the name of the font for this specific module
type: underline # underline or overline line:
color: '#000' # line color type: underline # underline or overline
button: color: '#000' # line color
activator: left # how to activate the button. clicks: left, right, middle. scroll wheel: scrup, scrdown button: # you may have multiple buttons with different activators.
command: 'echo "hello" > ~/file.txt' # command to run when button is clicked activator: left # how to activate the button. clicks: left, right, middle. scroll wheel: scrup, scrdown
# you may have multiple buttons with different activators. command: 'echo "hello" > ~/file.txt' # command to run when button is clicked
memory: # define multiple modules... memory: # define multiple modules...
command: "free -h | awk '/Mem/ {print $3}'" command: "free -h | awk '/Mem/ {print $3}'"

View file

@ -26,7 +26,7 @@ for item in lemonbar_cmd_options:
case 'fg_color': case 'fg_color':
lemonbar_command += (" -F '" + lemonbar_cmd_options['fg_color'] + "'") lemonbar_command += (" -F '" + lemonbar_cmd_options['fg_color'] + "'")
case 'offset': case 'offset':
lemonbar_command += (" -o " + lemonbar_cmd_options['offset']) lemonbar_command += (" -o " + str(lemonbar_cmd_options['offset']))
case 'line_color': case 'line_color':
lemonbar_command += (" -U '" + lemonbar_cmd_options['line_color'] + "'") lemonbar_command += (" -U '" + lemonbar_cmd_options['line_color'] + "'")

View file

@ -4,31 +4,98 @@ from threading import Thread
from subprocess import run from subprocess import run
from time import sleep from time import sleep
running_modules_dict = {} running_modules_dict = {"left": {}, "center": {}, "right": {}}
def print_modules(): def print_modules():
for module in running_modules_dict: for alignment in running_modules_dict:
print(running_modules_dict[module] + " | ", end='') 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) print(flush=True)
def module_thread(name, cmd, refresh, pre, post): def new_module_thread(alignment, name, cmd, refresh, pre, post):
while True: while True:
result = pre result = pre
result += run(cmd, shell=True, capture_output=True, text=True).stdout.strip() result += run(cmd, shell=True, capture_output=True, text=True).stdout.strip()
result += post result += post
running_modules_dict[name] = result running_modules_dict[alignment][name] = result
print_modules() print_modules()
sleep(refresh/1000) sleep(refresh/1000)
with open("./testing_config.yml", 'r', encoding='utf8') as file: 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_options:
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) config_file = safe_load(file)
modules = config_file['bar']['modules'] modules = config_file["bar"]["modules"]
for module in modules: for module in modules:
cmd = modules[module]['command'] parameters = parse_module(modules[module])
refresh = modules[module]['refresh'] parameters.insert(0, module)
running_modules_dict[module] = '' alignment = modules[module]["format"]["align"]
parameters.insert(0, alignment)
running_modules_dict[alignment][module] = ''
print(parameters)
x = Thread(target=module_thread, args=(module, cmd, refresh, '', '')) x = Thread(target=new_module_thread, args=parameters)
x.start() x.start()