From bf3920ad35ebd315f00fc482ebfa945e0d78787b Mon Sep 17 00:00:00 2001 From: Makesesama Date: Tue, 13 May 2025 23:07:22 +0200 Subject: [PATCH] config --- bar/config.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++ bar/modules/bar.py | 15 ++++++++----- flake.nix | 46 +++++++++++++++++++++++++++++----------- nix/derivation.nix | 2 ++ nix/shell.nix | 4 ++++ 5 files changed, 102 insertions(+), 17 deletions(-) create mode 100644 bar/config.py diff --git a/bar/config.py b/bar/config.py new file mode 100644 index 0000000..4a6ca75 --- /dev/null +++ b/bar/config.py @@ -0,0 +1,52 @@ +import yaml +import os +from platformdirs import user_config_dir +import argparse + + +APP_NAME = "makku_bar" + +XDG_CONFIG_HOME = user_config_dir(appname=APP_NAME) +XDG_CONFIG_FILE = os.path.join(XDG_CONFIG_HOME, "config.yaml") + + +def load_config(config_path=XDG_CONFIG_FILE): + """Loads configuration from a YAML file.""" + if config_path is None: + print("No configuration file path provided or found.") + return None + + try: + with open(config_path, "r") as f: + config = yaml.safe_load(f) + return config + except FileNotFoundError: + print(f"Error: Configuration file not found at {config_path}") + return None + except yaml.YAMLError as e: + print(f"Error parsing YAML file '{config_path}': {e}") + return None + except Exception as e: + print(f"An unexpected error occurred loading config file '{config_path}': {e}") + return None + + +def load_args(): + parser = argparse.ArgumentParser(description="makku_bar") + parser.add_argument( + "-c", + "--config", + help="Path to a custom configuration file.", + type=str, + ) + + args = parser.parse_args() + return args.config + + +app_config = load_config() if not load_args() else load_config(load_args()) + +if app_config is None: + raise Exception("Config file missing") + +VINYL = app_config.get("vinyl", {"enabled": False}) diff --git a/bar/modules/bar.py b/bar/modules/bar.py index b8df86f..8dd0200 100644 --- a/bar/modules/bar.py +++ b/bar/modules/bar.py @@ -19,6 +19,8 @@ from fabric.utils import ( ) from fabric.widgets.circularprogressbar import CircularProgressBar +from bar.config import VINYL + class StatusBar(Window): def __init__( @@ -74,7 +76,9 @@ class StatusBar(Window): overlays=[self.cpu_progress_bar, self.progress_label], ) self.player = Player() - self.vinyl = VinylButton() + self.vinyl = None + if VINYL["enabled"]: + self.vinyl = VinylButton() self.status_container = Box( name="widgets-container", @@ -83,15 +87,16 @@ class StatusBar(Window): children=self.progress_bars_overlay, ) - end_container_children = [ - self.vinyl, - self.status_container, - ] + end_container_children = [] + + if self.vinyl: + end_container_children.append(self.vinyl) if self.system_tray: end_container_children.append(self.system_tray) end_container_children.append(self.date_time) + end_container_children.append(self.status_container) self.children = CenterBox( name="bar-inner", diff --git a/flake.nix b/flake.nix index 8369b0b..0e9e206 100644 --- a/flake.nix +++ b/flake.nix @@ -47,6 +47,11 @@ pkgs, ... }: + let + cfg = config.services.makku-bar; + + settingsFormat = pkgs.formats.yaml { }; + in { options.services.makku-bar = { enable = lib.mkEnableOption "makku-bar status bar"; @@ -56,24 +61,41 @@ default = self.packages.${pkgs.system}.default; description = "The makku-bar package to use."; }; + + settings = lib.mkOption { + type = lib.types.submodule { + options = { + vinyl = { + enable = lib.mkOption { + type = lib.types.bool; + default = false; + }; + }; + }; + }; + }; }; config = lib.mkIf config.services.makku-bar.enable { - systemd.user.services.makku-bar = { - Unit = { - Description = "Makku Status Bar"; - After = [ "graphical-session.target" ]; - }; + systemd.user.services.makku-bar = + let + configFile = settingsFormat.generate "config.yaml" cfg.settings; + in + { + Unit = { + Description = "Makku Status Bar"; + After = [ "graphical-session.target" ]; + }; - Service = { - ExecStart = "${config.services.makku-bar.package}/bin/bar"; - Restart = "on-failure"; - }; + Service = { + ExecStart = "${config.services.makku-bar.package}/bin/bar --config ${configFile}"; + Restart = "on-failure"; + }; - Install = { - WantedBy = [ "default.target" ]; + Install = { + WantedBy = [ "default.target" ]; + }; }; - }; }; }; }; diff --git a/nix/derivation.nix b/nix/derivation.nix index 1dc6466..dc3359a 100644 --- a/nix/derivation.nix +++ b/nix/derivation.nix @@ -43,6 +43,8 @@ python3Packages.buildPythonApplication { dependencies = with python3Packages; [ python-fabric pywayland + pyyaml + platformdirs ]; doCheck = false; dontWrapGApps = true; diff --git a/nix/shell.nix b/nix/shell.nix index e31ca4b..b6ec37d 100644 --- a/nix/shell.nix +++ b/nix/shell.nix @@ -27,6 +27,8 @@ pkgs.mkShell { wayland-scanner wayland wayland-protocols + playerctl + (python3.withPackages ( ps: with ps; [ setuptools @@ -39,6 +41,8 @@ pkgs.mkShell { pylsp-mypy pyls-isort python-lsp-ruff + pyyaml + platformdirs ] )) ];