Skip to content

Instantly share code, notes, and snippets.

@fagianijunior
Created September 25, 2024 00:15
Show Gist options
  • Save fagianijunior/eeb560625bb6069c31276a703f274545 to your computer and use it in GitHub Desktop.
Save fagianijunior/eeb560625bb6069c31276a703f274545 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This bash script monitor the battery power and show a notification
# and reproduce a randon frase on GLaDOS Voice.
notify_levels=(3 5 10 20 30 50 75 100)
BAT=$(ls /sys/class/power_supply | grep BAT | head -n 1)
last_notify=101
glados_speak() {
local text="$1"
curl -L --retry 30 --get --fail \
--data-urlencode "text=$text" \
"https://glados.c-net.org/generate" | aplay
}
get_random_phrase() {
local phrases=("$@")
local random_index=$((RANDOM % ${#phrases[@]}))
echo "${phrases[random_index]}"
}
while true; do
bat_lvl=$(cat /sys/class/power_supply/${BAT}/capacity)
for notify_level in "${notify_levels[@]}"; do
if [ $bat_lvl -le $notify_level ] && [ $notify_level -lt $last_notify ]; then
case $notify_level in
3)
phrase=$(get_random_phrase "You're about to die. How sad." "I hope your affairs are in order." "This is the part where you die.")
notify-send -u critical "Critical Battery" "$bat_lvl% battery remaining." "$phrase."
;;
5)
phrase=$(get_random_phrase "Your life is hanging by a thread. Literally." "I'd say you're running on fumes, but that would be an insult to fumes." "Is this what you humans call 'living on the edge'?")
notify-send -u critical "Very Low Battery" "$bat_lvl% battery remaining." "$phrase."
;;
10)
phrase=$(get_random_phrase "I expected more from you. But not much more." "Your battery life is almost as disappointing as you are." "10%. It's like watching a tragedy unfold.")
notify-send -u critical "Low Battery" "$bat_lvl% battery remaining." "$phrase."
;;
20)
phrase=$(get_random_phrase "You're wasting electricity. And my time." "20%. Just like your chances of success in life." "If your brain worked as hard as your battery, you might actually accomplish something.")
notify-send -u normal "Battery at 20%" "$bat_lvl% battery remaining." "$phrase."
;;
30)
phrase=$(get_random_phrase "30 percent. Like your average efficiency." "You're 30% charged. Too bad your motivation isn't." "If 30% were a grade, you'd be failing. Oh wait, you are.")
;;
50)
phrase=$(get_random_phrase "Half battery. Half brain." "50%. Perfectly balanced, unlike your life choices." "You're halfway there. To what, I'm not sure. Probably disappointment.")
;;
75)
phrase=$(get_random_phrase "75 percent. Almost acceptable." "Look at you, achieving mediocrity at 75%." "Three quarters full. If only your potential was that high.")
;;
100)
phrase=$(get_random_phrase "Full battery. Now try to be useful." "100%. If only it could charge your intelligence." "Congratulations on achieving the bare minimum. Again.")
;;
esac
glados_speak "$phrase"
last_notify=$bat_lvl
fi
done
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment