From 253668025d2ce96e2709701bebca41d364beb839 Mon Sep 17 00:00:00 2001 From: Damian D'Souza <113062544+damian-ds7@users.noreply.github.com> Date: Wed, 29 Jul 2026 03:12:15 +0200 Subject: [PATCH] Fix/battery threshold setup (#136) * refactor(battery-threshold): change setup to run script in terminal via sudo * chore(battery-threshold): update dependencies * docs(battery-threshold): update threshold.txt location description * chore(battery-threshold): bump version --- battery-threshold/README.md | 8 ++-- battery-threshold/plugin.toml | 4 +- battery-threshold/service.luau | 32 +++++++--------- battery-threshold/setup_rules.sh | 63 +++++++++++++++++++++++++++----- 4 files changed, 72 insertions(+), 35 deletions(-) diff --git a/battery-threshold/README.md b/battery-threshold/README.md index 40fa7c2..f906edc 100644 --- a/battery-threshold/README.md +++ b/battery-threshold/README.md @@ -13,12 +13,10 @@ while plugged in, reducing battery wear and heat. ## Requirements -- `polkit` - required for interactive setup - Laptop hardware supporting battery charge threshold control in sysfs (`/sys/class/power_supply/*/charge_control_end_threshold`). -- The following external programs must be available on `PATH`: `test`, `pkexec`, - `bash`, `cat`, `getent`, `groupadd`, `usermod`, `udevadm`, `chgrp`, and - `chmod`. +- The following external programs must be available on `PATH`: `test`, `sudo`, + `bash`, `readlink`, `cat`, `getent`, `groupadd`, `usermod`, `udevadm`, `chgrp`, and `chmod`. ## Usage @@ -60,4 +58,4 @@ noctalia msg plugin damian-ds7/battery-threshold:service all setup - **Manual Setup Fallback**: If Polkit is not available, run `sudo ./setup_rules.sh` manually from the plugin directory. - **Persistence**: Threshold settings are stored in `threshold.txt` in plugin - directory and restored across reboots. + data directory and restored across reboots. diff --git a/battery-threshold/plugin.toml b/battery-threshold/plugin.toml index 65bc0be..004dbcb 100644 --- a/battery-threshold/plugin.toml +++ b/battery-threshold/plugin.toml @@ -1,13 +1,13 @@ id = "damian-ds7/battery-threshold" name = "Battery Threshold Control" -version = "1.0.0" +version = "1.0.1" plugin_api = 3 author = "Damian D'Souza" description = "Set the battery threshold for laptop batteries to extend battery lifespan" license = "MIT" icon = "battery-eco" tags = ["hardware", "system", "utility", "bar", "panel"] -dependencies = ["test", "pkexec", "bash", "cat", "getent", "groupadd", "usermod", "udevadm", "chgrp", "chmod"] +dependencies = ["test", "sudo", "bash", "readlink", "cat", "getent", "groupadd", "usermod", "udevadm", "chgrp", "chmod"] [[service]] id = "service" diff --git a/battery-threshold/service.luau b/battery-threshold/service.luau index a9a956f..ceba96a 100644 --- a/battery-threshold/service.luau +++ b/battery-threshold/service.luau @@ -171,26 +171,20 @@ local function run_setup() local rules_script = plugin_dir .. "/setup_rules.sh" local cmd = - string.format("pkexec bash %s %s", shell_quote(rules_script), shell_quote(user)) + string.format("bash %s %s", shell_quote(rules_script), shell_quote(user)) - noctalia.log("Running setup script via pkexec: " .. cmd) - noctalia.runAsync(cmd, function(result) - if result.exitCode == 0 then - noctalia.log("Setup completed successfully") - noctalia.notify( - noctalia.tr("notification.setup-title"), - noctalia.tr("notification.setup-success") - ) - check_status() - else - noctalia.log("Setup failed with exit code " .. tostring(result.exitCode)) - noctalia.notifyError( - noctalia.tr("notification.setup-title"), - noctalia.tr("notification.setup-fail") - ) - end - noctalia.state.set("run_setup_request", false) - end) + noctalia.log("Launching setup script in terminal: " .. cmd) + local launched = noctalia.runInTerminal(cmd) + if launched then + noctalia.log("Setup script launched in terminal") + else + noctalia.log("Failed to launch terminal for setup script") + noctalia.notifyError( + noctalia.tr("notification.setup-title"), + noctalia.tr("notification.setup-fail") + ) + end + noctalia.state.set("run_setup_request", false) end noctalia.state.watch("run_setup_request", function(val) diff --git a/battery-threshold/setup_rules.sh b/battery-threshold/setup_rules.sh index 1865d41..aaed2f6 100755 --- a/battery-threshold/setup_rules.sh +++ b/battery-threshold/setup_rules.sh @@ -8,22 +8,64 @@ # It creates a group 'battery_ctl' and adds the target user to this group. # # Usage: -# $ sudo ./setup_rules.sh # uses SUDO_USER (with sudo) -# $ ./setup_rules.sh username # use provided username (if ran as root) +# $ ./setup_rules.sh [username] [--non-interactive|-y] # ------------------------------ set -e -# Check if running as root -if [ "$EUID" -ne 0 ]; then - echo "Error: This script must be run as root (use sudo)" - exit 1 +SCRIPT_PATH="$(readlink -f "$0" 2>/dev/null || echo "$0")" + +TARGET_USER="" +SKIP_PROMPT=false + +for arg in "$@"; do + case "$arg" in + -y | --non-interactive) + SKIP_PROMPT=true + ;; + -*) + ;; + *) + if [ -z "$TARGET_USER" ]; then + TARGET_USER="$arg" + fi + ;; + esac +done + +TARGET_USER="${TARGET_USER:-${SUDO_USER:-$USER}}" + +if [ "$SKIP_PROMPT" = false ]; then + echo "====================================================" + echo " Battery Threshold Udev Setup" + echo "====================================================" + echo "Script location: $SCRIPT_PATH" + echo "Target user: $TARGET_USER" + echo "" + echo "Please examine the script location and contents above before proceeding." + echo "" + + read -rp "Do you want to proceed with setup? (y/N): " CONFIRM + case "$CONFIRM" in + [yY][eE][sS] | [yY]) + ;; + *) + echo "Setup cancelled by user." + read -rp "Press Enter to exit..." + exit 1 + ;; + esac fi -# Determine target user -TARGET_USER=${SUDO_USER:-$1} +# Escalate privileges via sudo only after user confirmation +if [ "$EUID" -ne 0 ]; then + echo "" + echo "Escalating privileges via sudo..." + exec sudo bash "$SCRIPT_PATH" "$TARGET_USER" --non-interactive +fi if [ -z "$TARGET_USER" ]; then echo "Error: No target user specified." >&2 + read -rp "Press Enter to exit..." exit 1 fi @@ -50,5 +92,8 @@ echo "Reloading rules..." udevadm control --reload-rules && udevadm trigger -echo "You may need a reboot for the plugin's write access to take effect" +echo "" +echo "You may need a reboot for the plugin's write access to take effect." echo "Done!" +echo "" +read -rp "Press Enter to exit..."