-
Notifications
You must be signed in to change notification settings - Fork 2
/
tbattery
executable file
·72 lines (63 loc) · 1.55 KB
/
tbattery
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
70
71
72
#!/bin/bash
# (c) 2020 Leif Sawyer
# License: GPL 3.0 (see https://github.com/akhepcat/)
# Permanent home: https://github.com/akhepcat/Miscellaneous/
# Direct download: https://raw.githubusercontent.com/akhepcat/Miscellaneous/master/tbattery
#
WIDTH=100
COLUMNS=${COLUMNS:-$(tput cols)}
if [ $COLUMNS -gt ${WIDTH} ]
then
scale=1
elif [ $COLUMNS -gt $(( $WIDTH / 2 )) ]
then
scale=2
else
scale=4
fi
if [ -e /sys/class/power_supply/BAT0/charge_now ]
then
CHARGE=charge
elif [ -e /sys/class/power_supply/BAT0/energy_now ]
then
CHARGE=energy
else
echo "No battery found"
exit 1
fi
now=$(cat /sys/class/power_supply/BAT0/${CHARGE}_now)
full=$(cat /sys/class/power_supply/BAT0/${CHARGE}_full)
charging=$(grep -i '^charging' /sys/class/power_supply/BAT0/status)
spark=$'\xE2\x8C\x81'
BATP=$(echo "scale = 2; print ( $now / $full ) * 100" | bc)
BATP=$(echo "($BATP + .5) /1" | bc)
#echo battery=$BATP
PADL=$(( ($WIDTH / $scale) - ( (100 - $BATP) / $scale ) - 2))
PADR=$(( ($WIDTH / $scale) - $PADL - 1 ))
fcolor="\e[0m\e[100m"
[[ ${BATP} -gt 98 ]] && fcolor="\e[0m"
if [ ${BATP} -gt 70 ]
then
# green
color="\e[0m\e[1m\e[92m"
ecolor="\e[0m\e[42m"
elif [ ${BATP} -gt 40 ]
then
# yellow
color="\e[0m\e[1m\e[93m"
ecolor="\e[0m\e[43m"
elif [ ${BATP} -gt 20 ]
then
# magenta
color="\e[0m\e[1m\e[91m"
ecolor="\e[0m\e[1m\e[45m"
else
# red
color="\e[0m\e[1m\e[91m"
ecolor="\e[0m\e[1m\e[101m"
fi
if [ -n "${charging}" ]
then
BATP=${spark}${BATP}${spark}
fi
printf "%bE%${PADL}s%b%b%${PADR}s\e[0m\e[1mF\e[0m \n" "${ecolor}" " " "${color}${BATP}" "${fcolor}" " "