Skip to content

Instantly share code, notes, and snippets.

@magdesign
Created February 26, 2024 18:21
Show Gist options
  • Save magdesign/462dd4b87e4480519995e432737ac073 to your computer and use it in GitHub Desktop.
Save magdesign/462dd4b87e4480519995e432737ac073 to your computer and use it in GitHub Desktop.
low battery warning, goes into ~/.config/sxmo/hooks/sxmo_hook_battery.sh
GNU nano 7.2 .config/sxmo/hooks/sxmo_hook_battery.sh
#!/bin/sh
# configversion: d7a31b6b01150a2b1ad3e57f556a5de6
load_data() {
data="$(upower -i "$1" | grep . | sed -e 's|^ \+||' -e 's|: \+|:|')"
type="$(printf "%b" "$data" | grep -m1 -v : | sed -e 's|^ \+||')"
}
data_get() {
printf "%b" "$data" | grep "^$1:" | cut -d: -f2
}
device_changed() {
name="$(data_get "native-path")"
state="$(data_get "state")"
percentage="$(data_get "percentage" | cut -d% -f1)"
if [ -z "$name" ] || [ -z "$state" ]; then
return
fi
if [ "$state" = unknown ]; then
return
fi
sxmo_hook_statusbar.sh battery "$name" "$state" "$percentage"
}
# battery low warning
PERCENTAGE=$(upower -d | awk '/percentage/; /^$/ {exit}' | sed 's/[^0-9]//g')
STATE=$(upower -d | awk '/state/; /^$/ {exit}' | sed 's/state://g;s/ //g')
if [ $STATE = "discharging" ] && [ $PERCENTAGE -lt 8 ]; then
notify-send -t 5000 --urgency=critical "BATTERY is LOW" "(+_+) I will die soon"
fi
object="$1"
event="$2"
if [ "$type" != "battery" ]; then
exit
fi
case "$event" in
"device changed")
device_changed "$object"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment