summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2023-03-17 11:32:58 +0200
committerSyndamia <kamen@syndamia.com>2023-03-17 11:32:58 +0200
commit292540dd15862b66f7350fef05a1b009460c486c (patch)
tree422b3718a7e8e3537fed1b134b29ab3dffa2e9e1
parentc06d86b3ec9147f03dec9d35693166b7341cbb18 (diff)
downloaddotfiles-292540dd15862b66f7350fef05a1b009460c486c.tar
dotfiles-292540dd15862b66f7350fef05a1b009460c486c.tar.gz
dotfiles-292540dd15862b66f7350fef05a1b009460c486c.zip
[.a/bs] Fully rewrote script
-rwxr-xr-x.a/battery-status.sh92
1 files changed, 46 insertions, 46 deletions
diff --git a/.a/battery-status.sh b/.a/battery-status.sh
index efbc2a2..dcfae2d 100755
--- a/.a/battery-status.sh
+++ b/.a/battery-status.sh
@@ -1,57 +1,57 @@
#!/bin/bash
-if [[ "$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep native-path | awk '{print $2}')" == '(null)' ]]; then
- exit
-fi
+# Check for dependencies
+for comm in upower awk; do
+ command -v upower >/dev/null 2>&1 || (echo "Error, missing command dependencies: $comm" && exit)
+done
-battery_notify () {
- # if battery percentage is equal to given one, if battery percentage inside /tmp/battery.tmp is not equal to given one and if power indicator is the given on
- if [ ${__bat_per} -eq $1 ] && ! grep -q $1 "/tmp/battery.tmp" && [[ ${__bat_power} = $2 ]]; then
- # Send notification and rewrite battery.tmp file with current percentage
- notify-send -u critical -i battery-low -t 99999999 "$3"
- echo $1 > /tmp/battery.tmp
- fi
-}
+# Prepare battery informatoin
+upower -i /org/freedesktop/UPower/devices/battery_BAT0 > /tmp/BAT0
+[ "$(awk '/native-path/{print $2}' /tmp/BAT0)" = '(null)' ] && exit
-# If upower command is available
-if command -v upower > /dev/null 2>&1 ; then
+bat_state="$(awk '/state/{print substr($2, 1, 1)}' /tmp/BAT0)"
+[ $bat_state = 'c' ] && bat_power_ind='' || \
+[ $bat_state = 'd' ] && bat_power_ind='' || \
+ bat_power_ind=''
- # Get battery power indicator (charging/discharging/fully charged) and stop script if there is no value for it (if there is no battery in device)
- __bat_power=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep state | awk '{print $2}')
- if [[ -n ${__bat_per} ]]; then
- exit
- fi
+bat_per="$(awk '/percentage/{print strtonum($2)}' /tmp/BAT0)"
- # Set the battery power indicator icon
- __bat_power_ind=""
- if [[ ${__bat_power} = "charging" ]]; then __bat_power_ind=""
- elif [[ ${__bat_power} = "discharging" ]]; then __bat_power_ind=""
- elif [[ ${__bat_power} = "fully-charged" ]]; then __bat_power_ind=""
- fi
+[ $bat_state = 'd' ] \
+&& bat_time="$(awk '/time to empty/{print $4 substr($5, 1, 1)}' /tmp/BAT0)" \
+|| bat_time="$(awk '/time to full/ {print $4 substr($5, 1, 1)}' /tmp/BAT0)"
- # Get the battery percentage, time until the battery is empty and time until the battery is full
- __bat_per=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep percentage | awk '{print $2}' | sed "s|%||g")
- __time_empt=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to empty" | awk '{print $4 $5}' | grep -o '^[0-9.]*[a-z]')
- __time_full=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep "time to full" | awk '{print $4 $5}' | grep -o '^[0-9.]*[a-z]')
-
- # Print the battery power indicator icon, percentage and time until empty or time until full
- echo -n " ${__bat_power_ind} ${__bat_per}%"
- if [[ ${__bat_power} = "discharging" ]]; then
- echo " ${__time_empt}"
- else
- echo " ${__time_full}"
- fi
+echo " $bat_power_ind ${bat_per}% $bat_time"
- # Create the temporary battery file, if it doesn't exist
- if [ ! -f "/tmp/battery.tmp" ]; then
- touch /tmp/battery.tmp
+#
+# Notifications
+#
+
+command -v notify-send >/dev/null 2>&1 || exit
+[ ! -e '/tmp/bat_notif' ] && touch /tmp/bat_notif
+bat_notif="$(cat /tmp/bat_notif)"
+
+
+# Notify when battery is charged to 80%
+if [ $bat_state = 'c' ]; then
+ if [ "$bat_notif" != 80 ] && [ $bat_per -ge 80 ]; then
+ notify-send -u normal -i battery-charging -t 999999 "Battery ${per}%"
+ echo 80 > /tmp/bat_notif
fi
- # Setup for battery notifications
- battery_notify 100 'fully-charged' 'Battery 100%'
- battery_notify 80 'charging' 'Battery 80%'
- battery_notify 20 'discharging' 'Battery 20%'
- battery_notify 15 'discharging' 'Battery 15%'
- battery_notify 10 'discharging' 'Battery 10%'
- battery_notify 5 'discharging' 'Battery 5%'
+# Notify when battery discharges to 5%, 10%, 15% and 20%
+elif [ $bat_state = 'd' ]; then
+ for per in 5 10 15 20; do
+ if [ "$bat_notif" != $per ] && [ $bat_per -le $per ]; then
+ notify-send -u critical -i battery-low -t 999999 "Battery ${per}%"
+ echo $per > /tmp/bat_notif
+ break
+ fi
+ done
+
+# Notify when battery is full
+else
+ if [ "$bat_notif" != 100 ]; then
+ notify-send -u normal -i battery-full -t 999999 'Fully charged!'
+ echo 100 > /tmp/bat_notif
+ fi
fi