#!/usr/bin/env python3 import subprocess import json from string import Template import os def panic(err): import traceback print(traceback.format_exc()) print(f"error {err}") os._exit(1) class Monitor: id: int name: str width: int height: int def __init__(self, id: int, name: str, width: int, height: int): self.id = id self.name = name self.width = width self.height = height def get_monitors() -> list[Monitor]: ws = subprocess.run( ["hyprctl", "monitors", "-j"], capture_output=True ) _monitors = json.loads(ws.stdout) ret = [] for m in _monitors: ret.append(Monitor( id=m["id"], name=m["name"], width=m["width"], height=m["height"], )) return ret def temlate(source: str, monitor: Monitor, bar_name: str, home: str, base_dir: str) -> str: height = int(monitor.height * .02) return Template(source) \ .safe_substitute({ "monitor": monitor.id, "monitor_name": monitor.name, "bar_name": bar_name, "home": home, "base_dir": base_dir, "height": height, }) def generate_bar_config(monitor: Monitor, bar_name: str, home: str, base_dir: str, source_dir: str, dest_dir: str): items = os.listdir(source_dir) for item in items: if os.path.isdir(f"{source_dir}/{item}"): os.makedirs(f"{dest_dir}/{item}") generate_bar_config(monitor, bar_name, home, base_dir, f"{source_dir}/{item}", f"{dest_dir}/{item}") elif os.path.isfile(f"{source_dir}/{item}"): data = "" try: with open(f"{source_dir}/{item}", "r") as fp: data = fp.read() except Exception as e: panic(e) data = temlate( data, monitor, bar_name, home, base_dir ) try: with open(f"{dest_dir}/{item}", "w") as fp: fp.write(data) except Exception as e: panic(e) def launch_bar(monitor: Monitor, wayland_display: str): bar_name = f"bar-{wayland_display}-{monitor.id}" dir = f"/tmp/vbar/{wayland_display}/{monitor.id}" home = os.getenv("HOME", default=None) if home == None: panic("HOME environment variable is unset") try: os.makedirs(dir) except FileExistsError: try: subprocess.run(["rm", "-rf", dir]) os.makedirs(dir) except Exception as e: panic(e) config = generate_bar_config(monitor, bar_name, home, dir, f"{home}/.config/eww/templates/bar", dir) try: subprocess.run(["eww", "-c", dir, "reload"]) subprocess.run(["eww", "-c", dir, "open", bar_name]) except Exception as e: panic(e) def main(): wayland_display = os.getenv("WAYLAND_DISPLAY", default=None) if wayland_display is None: os.exit(1) monitors = get_monitors() for monitor in monitors: launch_bar( monitor=monitor, wayland_display=wayland_display, ) if __name__ == '__main__': main()