summaryrefslogtreecommitdiff
path: root/scripts/battery.sh
blob: ebc85215efd582a29472206079d050a279312183 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
#!/bin/bash
# dotfiles -- scripts/battery.sh
# author: johannst

bat="${BLOCK_INSTANCE:-BAT0}"
[[ ! -f "/sys/class/power_supply/$bat/type" ]] && exit 1

# Auto-detect file prefix for battery files
for prefix in energy charge; do
	[[ -f "/sys/class/power_supply/$bat/${prefix}_now" ]] && {
		file_prefix=$prefix
		break
	}
done && test -n $file_prefix || exit 1

show_bat_use_design_capacity=0
show_bat_status=0
show_bat_color_output=0
for arg in "$@"; do
	case $arg in
		-dc | --design_capacity) show_bat_use_design_capacity=1
			;;
		-s | --status) show_bat_status=1
			;;
		-c | --color) show_bat_color_output=1
			;;
		*) # ignore unknown
			;;
	esac
done

# Unknown, Charging, Discharging, Full
BAT_STATUS=$(cat /sys/class/power_supply/$bat/status)

BAT_NOW=$(cat /sys/class/power_supply/$bat/${file_prefix}_now)
[[ $show_bat_use_design_capacity == 1 ]] \
	&& BAT_FULL=$(cat /sys/class/power_supply/$bat/${file_prefix}_full_design) \
	|| BAT_FULL=$(cat /sys/class/power_supply/$bat/${file_prefix}_full)
BAT_POWER=$(echo $BAT_NOW $BAT_FULL | awk '{ print int($1 * 100 / $2); }')

OUT="$BAT_POWER%"
[[ $show_bat_status == 1 ]] && \
	case $BAT_STATUS in
		Charging) OUT="$OUT (Charging)"
			;;
		*) # only show charging state for now
			;;
	esac

echo "$OUT"

[[ $show_bat_color_output == 1 ]] && {
	# print twice, else colors not working?!
	echo ""
	if [[ $BAT_POWER -ge 80 ]]; then
		# green
		echo "#00FF00"
	elif [[ $BAT_POWER -ge 60 ]]; then
		# yellow
		echo "#FFF600"
	elif [[ $BAT_POWER -ge 40 ]]; then
		# dark orange
		echo "#FFAE00"
	else
		# red
		echo "#FF0000"
	fi
}