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
This commit is contained in:
Damian D'Souza
2026-07-28 21:12:15 -04:00
committed by GitHub
parent bd7d88dfee
commit 253668025d
4 changed files with 72 additions and 35 deletions
+3 -5
View File
@@ -13,12 +13,10 @@ while plugged in, reducing battery wear and heat.
## Requirements ## Requirements
- `polkit` - required for interactive setup
- Laptop hardware supporting battery charge threshold control in sysfs - Laptop hardware supporting battery charge threshold control in sysfs
(`/sys/class/power_supply/*/charge_control_end_threshold`). (`/sys/class/power_supply/*/charge_control_end_threshold`).
- The following external programs must be available on `PATH`: `test`, `pkexec`, - The following external programs must be available on `PATH`: `test`, `sudo`,
`bash`, `cat`, `getent`, `groupadd`, `usermod`, `udevadm`, `chgrp`, and `bash`, `readlink`, `cat`, `getent`, `groupadd`, `usermod`, `udevadm`, `chgrp`, and `chmod`.
`chmod`.
## Usage ## Usage
@@ -60,4 +58,4 @@ noctalia msg plugin damian-ds7/battery-threshold:service all setup
- **Manual Setup Fallback**: If Polkit is not available, run - **Manual Setup Fallback**: If Polkit is not available, run
`sudo ./setup_rules.sh` manually from the plugin directory. `sudo ./setup_rules.sh` manually from the plugin directory.
- **Persistence**: Threshold settings are stored in `threshold.txt` in plugin - **Persistence**: Threshold settings are stored in `threshold.txt` in plugin
directory and restored across reboots. data directory and restored across reboots.
+2 -2
View File
@@ -1,13 +1,13 @@
id = "damian-ds7/battery-threshold" id = "damian-ds7/battery-threshold"
name = "Battery Threshold Control" name = "Battery Threshold Control"
version = "1.0.0" version = "1.0.1"
plugin_api = 3 plugin_api = 3
author = "Damian D'Souza" author = "Damian D'Souza"
description = "Set the battery threshold for laptop batteries to extend battery lifespan" description = "Set the battery threshold for laptop batteries to extend battery lifespan"
license = "MIT" license = "MIT"
icon = "battery-eco" icon = "battery-eco"
tags = ["hardware", "system", "utility", "bar", "panel"] 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]] [[service]]
id = "service" id = "service"
+13 -19
View File
@@ -171,26 +171,20 @@ local function run_setup()
local rules_script = plugin_dir .. "/setup_rules.sh" local rules_script = plugin_dir .. "/setup_rules.sh"
local cmd = 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.log("Launching setup script in terminal: " .. cmd)
noctalia.runAsync(cmd, function(result) local launched = noctalia.runInTerminal(cmd)
if result.exitCode == 0 then if launched then
noctalia.log("Setup completed successfully") noctalia.log("Setup script launched in terminal")
noctalia.notify( else
noctalia.tr("notification.setup-title"), noctalia.log("Failed to launch terminal for setup script")
noctalia.tr("notification.setup-success") noctalia.notifyError(
) noctalia.tr("notification.setup-title"),
check_status() noctalia.tr("notification.setup-fail")
else )
noctalia.log("Setup failed with exit code " .. tostring(result.exitCode)) end
noctalia.notifyError( noctalia.state.set("run_setup_request", false)
noctalia.tr("notification.setup-title"),
noctalia.tr("notification.setup-fail")
)
end
noctalia.state.set("run_setup_request", false)
end)
end end
noctalia.state.watch("run_setup_request", function(val) noctalia.state.watch("run_setup_request", function(val)
+54 -9
View File
@@ -8,22 +8,64 @@
# It creates a group 'battery_ctl' and adds the target user to this group. # It creates a group 'battery_ctl' and adds the target user to this group.
# #
# Usage: # Usage:
# $ sudo ./setup_rules.sh # uses SUDO_USER (with sudo) # $ ./setup_rules.sh [username] [--non-interactive|-y]
# $ ./setup_rules.sh username # use provided username (if ran as root)
# ------------------------------ # ------------------------------
set -e set -e
# Check if running as root SCRIPT_PATH="$(readlink -f "$0" 2>/dev/null || echo "$0")"
if [ "$EUID" -ne 0 ]; then
echo "Error: This script must be run as root (use sudo)" TARGET_USER=""
exit 1 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 fi
# Determine target user # Escalate privileges via sudo only after user confirmation
TARGET_USER=${SUDO_USER:-$1} 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 if [ -z "$TARGET_USER" ]; then
echo "Error: No target user specified." >&2 echo "Error: No target user specified." >&2
read -rp "Press Enter to exit..."
exit 1 exit 1
fi fi
@@ -50,5 +92,8 @@ echo "Reloading rules..."
udevadm control --reload-rules && udevadm trigger 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 "Done!"
echo ""
read -rp "Press Enter to exit..."