From 044dbf2b85749c2771e1c842cd2f8feb49d1dd33 Mon Sep 17 00:00:00 2001 From: Damian D'Souza Date: Thu, 9 Oct 2025 21:38:00 +0200 Subject: [PATCH] BatteryService: add -q option to supress notifications --- Bin/battery-manager/set-battery-treshold.sh | 37 +++++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/Bin/battery-manager/set-battery-treshold.sh b/Bin/battery-manager/set-battery-treshold.sh index e4e4c8b7..f771fcb1 100755 --- a/Bin/battery-manager/set-battery-treshold.sh +++ b/Bin/battery-manager/set-battery-treshold.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SUPPRESS_NOTIFICATIONS=false print_error() { echo -e "$1" >&2 @@ -15,25 +16,47 @@ send_notification() { local title="$2" local message="$3" - if command -v notify-send >/dev/null 2>&1; then + if [ "$SUPPRESS_NOTIFICATIONS" = false ] && command -v notify-send >/dev/null 2>&1; then notify-send -u "$urgency" "$title" "$message" fi } -if [ "$#" -ne 1 ]; then +while [[ $# -gt 0 ]]; do + case "$1" in + -q|--quiet) + SUPPRESS_NOTIFICATIONS=true + shift + ;; + -*) + print_error "Unknown option: $1" + echo "Usage: $0 [OPTIONS] " >&2 + echo "Options:" >&2 + echo " -q, --quiet Suppress notifications" >&2 + exit 1 + ;; + *) + BATTERY_LEVEL="$1" + shift + ;; + esac +done + +if [ -z "$BATTERY_LEVEL" ]; then print_error "Battery level not specified" - echo "Usage: $0 " >&2 + echo "Usage: $0 [OPTIONS] " >&2 + echo "Options:" >&2 + echo " -q, --quiet Suppress notifications" >&2 exit 1 fi -if ! [[ "$1" =~ ^[0-9]+$ ]] || [ "$1" -gt 100 ] || [ "$1" -lt 0 ]; then +if ! [[ "$BATTERY_LEVEL" =~ ^[0-9]+$ ]] || [ "$BATTERY_LEVEL" -gt 100 ] || [ "$BATTERY_LEVEL" -lt 0 ]; then print_error "Battery level must be a number between 0-100" - echo "Usage: $0 " >&2 + echo "Usage: $0 [OPTIONS] " >&2 + echo "Options:" >&2 + echo " -q, --quiet Suppress notifications" >&2 exit 1 fi -BATTERY_LEVEL="$1" - CURRENT_USER="$USER" if [ -z "$CURRENT_USER" ]; then CURRENT_USER="$(whoami)"