47 lines
1.9 KiB
Bash
Executable File
47 lines
1.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
#export DISPLAY=:0
|
|
export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u "$USER" -o startx)/environ | xargs -0)
|
|
|
|
# Battery percentage at which to notify
|
|
WARNING_LEVEL=30
|
|
CRITICAL_LEVEL=10
|
|
|
|
#BATTERY_LEVEL=$(<"/sys/class/power_supply/BAT0/capacity")
|
|
#BATTERY_DISCHARGING=$([[ $(<"/sys/class/power_supply/BAT0/status") == "Discharging" ]] && echo 1 || echo 0)
|
|
# Using acpi
|
|
BATTERY_DISCHARGING=$(acpi -b | grep "Battery 0" | grep -c "Discharging")
|
|
BATTERY_LEVEL=$(acpi -b | grep "Battery 0" | grep -P -o '[0-9]+(?=%)')
|
|
|
|
# Use temporary files to store whether a notification has already been shown (to prevent multiple notifications)
|
|
CRIT_FILE=/tmp/battery_crit
|
|
WARN_FILE=/tmp/battery_warn
|
|
FULL_FILE=/tmp/battery_full
|
|
|
|
# Reset notifications if the battery is charging/discharging
|
|
if [ "$BATTERY_DISCHARGING" -eq 1 ] && [ -f $FULL_FILE ]; then
|
|
rm $FULL_FILE
|
|
elif [ "$BATTERY_DISCHARGING" -eq 0 ]; then
|
|
if [ -f $WARN_FILE ]; then
|
|
rm $WARN_FILE
|
|
fi
|
|
if [ -f $CRIT_FILE ]; then
|
|
rm $CRIT_FILE
|
|
fi
|
|
fi
|
|
|
|
# If the battery is charging and is full (and has not shown notification yet)
|
|
if [ "$BATTERY_LEVEL" -gt 95 ] && [ "$BATTERY_DISCHARGING" -eq 0 ] && [ ! -f $FULL_FILE ]; then
|
|
notify-send "Battery Full" "Battery is fully charged." -t 1500
|
|
touch $FULL_FILE
|
|
# If the battery is low and is not charging (and has not shown notification yet)
|
|
elif [ "$BATTERY_LEVEL" -le $CRITICAL_LEVEL ] && [ "$BATTERY_DISCHARGING" -eq 1 ] && [ ! -f $CRIT_FILE ]; then
|
|
notify-send "⚠️ Critical Battery Level" "${BATTERY_LEVEL}% of battery remaining." -u critical -r 9991 -t 5000
|
|
# uncomment the line below if you want to get notified at most once
|
|
#touch $WARN_FILE
|
|
elif [ "$BATTERY_LEVEL" -le $WARNING_LEVEL ] && [ "$BATTERY_DISCHARGING" -eq 1 ] && [ ! -f $WARN_FILE ]; then
|
|
notify-send "Low Battery" "${BATTERY_LEVEL}% of battery remaining." -u critical -r 9991 -t 1500
|
|
# uncomment the line below if you want to get notified at most once
|
|
#touch $WARN_FILE
|
|
fi
|