summaryrefslogtreecommitdiff
path: root/tmux/battery.sh
blob: 2cd1afb1e8b5a3571fa3c7dc81e63df9d6612182 (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
27
28
29
round() { awk -v n=$1 -v d=$2 'BEGIN{print int((n+d/2)/d) * d}'; }

upower -i /org/freedesktop/UPower/devices/DisplayDevice | awk '/percentage:/ {print $2}' | {
	read percentage_raw; # e.g. 69.420%
	percentage_10s=$(round "${percentage_raw%%%}" 10); # rounded number: 70
	# when battery is low, highlight it
	if [ $percentage_10s -le "10" ]; then 
		printf "#[bg=red,fg=brightwhite]";
	elif [ $percentage_10s -le "20" ]; then
		printf "#[bg=brightyellow]";
	fi
	printf " ";
	# icon
	if [ $percentage_10s -le "10" ]; then printf "󰁺"; fi
	if [ $percentage_10s -eq "20" ]; then printf "󰁻"; fi
	if [ $percentage_10s -eq "30" ]; then printf "󰁼"; fi
	if [ $percentage_10s -eq "40" ]; then printf "󰁽"; fi
	if [ $percentage_10s -eq "50" ]; then printf "󰁾"; fi
	if [ $percentage_10s -eq "60" ]; then printf "󰁿"; fi
	if [ $percentage_10s -eq "70" ]; then printf "󰂀"; fi
	if [ $percentage_10s -eq "80" ]; then printf "󰂁"; fi
	if [ $percentage_10s -eq "90" ]; then printf "󰂂"; fi
	if [ $percentage_10s -eq "100" ]; then printf "󰁹"; fi
	printf " ";
	# percentage level rounded to a whole number (69%)
	printf "%.0f%%" "${percentage_raw%%%}";
	printf " ";
	printf "#[default]"; # reset style
}