From 22555c2bcd75d35062f1f94a1b69929684c03b20 Mon Sep 17 00:00:00 2001 From: Andrea Date: Sat, 1 Mar 2025 22:33:17 +0100 Subject: [PATCH] chore(waybar): update battery script --- waybar/scripts/check_battery.sh | 44 +++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/waybar/scripts/check_battery.sh b/waybar/scripts/check_battery.sh index 3784123..8773cad 100755 --- a/waybar/scripts/check_battery.sh +++ b/waybar/scripts/check_battery.sh @@ -1,17 +1,41 @@ #! /usr/bin/env bash -FILE=~/.config/waybar/scripts/.low_bat - +file=~/.config/waybar/scripts/lowbat bat="/sys/class/power_supply/BAT1" -CRIT="${1:-15}" +crit="${1:-150}" stat="$(cat $bat/status)" perc="$(cat $bat/capacity)" -if [[ $perc -le $CRIT ]] && [[ "$stat" == "Discharging" ]] && [[ ! -f "$FILE" ]]; then - notify-send --urgency=critical --icon=dialog-warning "Battery Low" "Current charge: $perc%" - touch $FILE -fi +notifysend() { + local pid + local title="Low Battery" + local body="Current charge: $perc%" + if [[ ! -f "$file" ]]; then + pid="$(notify-send --print-id --urgency=critical --icon=dialog-warning "$title" "$body")" + else + pid="$(notify-send --print-id --replace-id="$(cat $file)" --urgency=critical --icon=dialog-warning "$title" "$body")" + fi -if [[ -f "$FILE" ]] && [[ "$stat" == "Charging" ]]; then - rm $FILE -fi \ No newline at end of file + echo $pid > $file +} + +main() { + if [[ "$stat" == "Discharging" ]] && [[ "$perc" -le "$crit" ]] && [[ ! -f "$file" ]]; then + notifysend + fi + + if [[ -f "$file" ]]; then + case "$stat" in + "Charging" ) + rm $file + ;; + "Discharging" ) + notifysend + ;; + * );; + esac + + fi +} + +main $@ \ No newline at end of file