Skip to content

Instantly share code, notes, and snippets.

@rac021
Last active August 29, 2019 16:26
Show Gist options
  • Select an option

  • Save rac021/2eb64d1f6b255dd263d04059b310cd57 to your computer and use it in GitHub Desktop.

Select an option

Save rac021/2eb64d1f6b255dd263d04059b310cd57 to your computer and use it in GitHub Desktop.
#!/bin/bash
# crontab -e :
# @reboot nohup /home/ryahiaoui/.config/i3/i3_Battery_Notif.sh &
_alarm() {
( \speaker-test --frequency $1 --test sine )&
pid=$!
\sleep 0.${2}s
\kill -9 $pid
}
_beep() {
_alarm 700 100
_alarm 800 100
}
_beep_1() {
play -q -n synth 0.5 sin 400 || echo -e "\a"
}
show_popup() {
WISH_SCRIPT="wm state . withdrawn; tk_messageBox -icon warning -title \"Battery Warning\" -message \"${1}\"; exit"
echo "$WISH_SCRIPT" | wish
}
show_nagbar(){
i3-msg "exec i3-nagbar -m \"${1}\""
}
show_notify(){
GNOME_ICON="/usr/share/icons/gnome/scalable/status/battery-low-symbolic.svg"
XFCE_ICON="/usr/share/icons/elementary-xfce/status/48/battery-low.png"
# try to find nice notify icon
if [[ -z $NOTIFY_ICON ]]; then
if [[ -f $GNOME_ICON ]]; then
NOTIFY_ICON="${GNOME_ICON}"
elif [[ -f $XFCE_ICON ]]; then
NOTIFY_ICON="${XFCE_ICON}"
fi
fi
[[ -n $NOTIFY_ICON ]] && NOTIFY_OPT="-i ${NOTIFY_ICON}"
notify-send -u "${2}" ${NOTIFY_OPT} "${1}"
}
show_message(){
if [[ -n $USE_NOTIFY_SEND ]] && which notify-send; then
show_notify "$1" "$2"
elif [[ -z $DONT_USE_WISH ]] && which wish; then
show_popup "$1"
else
show_nagbar "$1"
fi
} >&2
USE_NOTIFY_SEND="y"
FULL_BATTERY="N"
while true
do
export DISPLAY=:0.0
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pidof i3)/environ | tr '\0' '\n' | cut -d= -f2-)
battery1_level=`acpi -b | grep -P -o -m 1 '[0-9]+(?=%)'`
battery2_level=`acpi -b | grep -P -o '[0-9]+(?=%)' | sed -n 2p`
#battery_level_added=$(( battery1_level + battery2_level ))
#battery_level=$(( battery_level_added / 2 ))
ac_power=`acpi -a | grep -P -o '(on|off)'`
if [ "$ac_power" = "on" ]; then
if [ $battery1_level -ge 100 ]; then
#notify-send -t 20000 "Battery charging above 90%. Unplug your AC adapter!" "Charging: ${battery_level}% "
if [ "$FULL_BATTERY" = "N" ]; then
show_message " $( echo -e "Battery charging above 100%.\n Unplug your AC adapter.\n ( -- Charging : ${battery1_level}% ! -- ) " ) " "low"
FULL_BATTERY="Y"
fi
fi
else #if discharging battery
if [ $battery1_level -le 40 ]; then
# notify-send -t 30000 "Battery is lower than %. Connect your AC adapter." "Charging: ${battery_level}%"
show_message " $( echo -e "Battery is lower than 40%.\n Connect your AC adapter.\n ( -- Charging : ${battery1_level}% ! -- ) ")" "critical"
FULL_BATTERY="N"
_beep_1
if [ $battery1_level -le 15 ]; then
_beep
fi
fi
fi
sleep 30 # run once every 30 Ssecondes
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment