From a1366fea77e60d353f0ec0c91916d280ccea2ab0 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Tue, 13 Feb 2024 15:02:42 +0545 Subject: [PATCH] feat(battery_notifier): Improved versio XD:( --- config/hypr/scripts/System_cmd.sh | 49 +++++++++++++------------------ 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/config/hypr/scripts/System_cmd.sh b/config/hypr/scripts/System_cmd.sh index 86a613b9..120299be 100755 --- a/config/hypr/scripts/System_cmd.sh +++ b/config/hypr/scripts/System_cmd.sh @@ -365,38 +365,31 @@ esac ###################################### battery_notify() { -last="NONE" # Possible values: NONE, FULL, LOW, CRITICAL, CHARGING -# Default values for LOW/CRITICAL status -low=40 -critical=20 +PREV_STATUS="Unknown" # Initialize previous status + while true; do - # If battery is plugged, do stuff - battery="/sys/class/power_supply/BAT0" - if [ -d $battery ]; then - capacity=$(cat $battery/capacity) - status=$(cat $battery/status) - # If battery full and not already warned about that - if [ "$last" != "FULL" ] && [ "$status" = "Full" ]; then - notify-send -u normal -i "$iDIR/battery-full.png" "Battery full" "Remove the adapter! $capacity%" - last="FULL" + STATUS=$(cat /sys/class/power_supply/AC/online 2>/dev/null) # Get charger status using power supply directory + + if [ "$STATUS" != "$PREV_STATUS" ]; then # Check if the charger status has changed + if [ "$STATUS" == "1" ]; then # Send a notification when charger is plugged in or unplugged + notify-send -u low "🔌 Charger Plugged In" "Battery charging" + else + notify-send -u low "🔌 Charger Unplugged" "Battery not charging" + fi + PREV_STATUS="$STATUS" # Update previous status fi - # If low and discharging - if [ "$last" != "LOW" ] && [ "$status" = "Discharging" ] && [ $capacity -le $low ]; then - notify-send -u normal -i "$iDIR/battery-low.png" "Battery low" "Plug in the adapter! $capacity%" - last="LOW" + + # Get battery percentage and remaining time using acpi + BATTERY_INFO=$(acpi) + PERCENT=$(echo "$BATTERY_INFO" | awk -F ',|%' '{print $2}') + + if [ "$STATUS" == "1" ] && [ "$PERCENT" -eq 100 ]; then # Check if the battery is charging and the percentage is 100% + notify-send -u low "🔌 Battery Fully Charged" "You can unplug the charger" # Send a notification when the battery is fully charged fi - # If critical and discharging - if [ "$status" = "Discharging" ] && [ $capacity -le $critical ]; then - notify-send -u critical -i "$iDIR/battery-low.png" "Battery very low" "Plug in the adapter!!! $capacity%" - last="CRITICAL" + if [ "$PERCENT" -le 15 ]; then # Check if the battery percentage is less than or equal to 20% + notify-send -u critical "đŸĒĢ Low Battery" "Plug in the charger" #Send low battery notification fi - # If charging and not already notified - if [ "$last" != "CHARGING" ] && [ "$status" = "Charging" ]; then - notify-send -u low -i "$iDIR/battery-charging.png" "Charger connected" "Battery is charging $capacity%" - last="CHARGING" - fi - fi - sleep 0.1 + sleep 0.1 # Sleep for some time before checking again done }