aboutsummaryrefslogtreecommitdiff
path: root/shlowbat
blob: a4fb9d7a03631518f8e3f6edae49609aab059ff9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/sh
psupath=/sys/class/power_supply
warning_level=0  # 0 = not warned, 1 = warned, 2 = warned critical

sum_props() {
	for prop in $(cat $psupath/*/$1); do sum=$(( $sum + $prop )); done
	echo $sum
}
is_discharging() { cat $psupath/*/status | grep "Discharging" > /dev/null ; }

while true; do
	capacity=$(( $(sum_props energy_now) * 100 / $(sum_props energy_full) ))

	if is_discharging; then
		if   [ $capacity -le  5 ] && [ $warning_level -lt 2 ]; then
			notify-send -u critical -w "Critical Low Battery Warning" "${capacity}% remains"
		elif [ $capacity -le 20 ] && [ $warning_level -lt 1 ]; then
			notify-send -u normal -w "Low Battery Warning" "${capacity}% remains"
		fi
	else
		warning_level=0
	fi

	printf "\r%d%%" $capacity
	sleep 3
done