summaryrefslogtreecommitdiffstats
path: root/bin/batmon
blob: fec22b7caca0d9f9cf53776f36964389466e3182 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh

bat()
{
	eval "$(cat /sys/class/power_supply/BAT0/uevent | grep -E \
		'^POWER_SUPPLY_(STATUS|ENERGY_)')"
	percentage=$(printf 'scale = 0; %d * 100 / %d;\n' \
		${POWER_SUPPLY_ENERGY_NOW} ${POWER_SUPPLY_ENERGY_FULL} | bc)
	case "${POWER_SUPPLY_STATUS}" in
		'Unknown')
			status='?'
			;;
		'Charging')
			status='⚡'
			;;
		'Discharging')
			status='🔋'
			;;
		'Not charging')
			status='!'
			;;
		'Full')
			status='🔌'
			;;
	esac
	printf '%s%3d%%\n' "${status}" ${percentage}
}

ac()
{
	status='🔌'
	printf '%s\n' "${status}"
}

main()
{
	if [ -e /sys/class/power_supply/BAT0/uevent ]; then
		bat
	else
		ac
	fi
}

main "${@}"