~kris/dots

srice

ref: 2dc7c2f7e39f562e3603676ef50c2df0a4723c19 srice/.local/bin/statusbar/sb-battery -rwxr-xr-x 1.5 KiB
2dc7c2f7 — Kris Yotam update nvim plan9 theme, add hosts file, misc updates 6 months ago
                                                                                
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh

# Prints all batteries, their percentage remaining and an icon corresponding
# to charge level. Uses Nerd Font battery icons.

case $BLOCK_BUTTON in
	3) notify-send " Battery module" ": discharging
: charging
: charged
- Scroll to adjust xbacklight." ;;
	4) xbacklight -inc 10 ;;
	5) xbacklight -dec 10 ;;
	6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

# Get battery level icon based on percentage
# full= (f240), 3/4= (f241), half= (f242), quarter= (f243)
# 20%=󰁻 (f007b), 10%=󰁺 (f007a), empty= (f244)
get_battery_icon() {
	if [ "$1" -ge 95 ]; then
		echo ""
	elif [ "$1" -ge 65 ]; then
		echo ""
	elif [ "$1" -ge 40 ]; then
		echo ""
	elif [ "$1" -ge 25 ]; then
		echo ""
	elif [ "$1" -ge 10 ]; then
		echo "󰁻"
	elif [ "$1" -ge 5 ]; then
		echo "󰁺"
	else
		echo ""
	fi
}

# Exit silently if no battery exists (desktop)
[ ! -d /sys/class/power_supply/BAT0 ] && [ ! -d /sys/class/power_supply/BAT1 ] && exit 0

# Loop through all attached batteries and format the info
for battery in /sys/class/power_supply/BAT?*; do
	# If non-first battery, print a space separator.
	[ -n "${capacity+x}" ] && printf " "
	# Sets up the status and capacity
	capacity="$(cat "$battery/capacity" 2>&1)"
	status="$(cat "$battery/status" 2>&1)"

	# Get icon based on battery level
	icon=$(get_battery_icon "$capacity")

	# Add charging indicator
	case "$status" in
		"Charging") charge_indicator=" " ;;
		"Full") charge_indicator=" " ;;
		*) charge_indicator="" ;;
	esac

	# Prints the info
	printf "%s %d%%%s" "$icon" "$capacity" "$charge_indicator"
done && printf "\\n"