-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmux_battery_charge_indicator.sh
executable file
·162 lines (142 loc) · 3.77 KB
/
tmux_battery_charge_indicator.sh
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
# global variables
total_slots=10
# 1. find battery data
function find_battery_data {
# for linux
if [[ $(uname) == "Linux" ]]
then
# check usual locations for battery folder
if [[ -d $(echo /sys/class/power_supply/BAT?/ | cut -d " " -f 1) ]]
then
directories=$(echo /sys/class/power_supply/BAT?/)
elif [[ -d $(echo /proc/acpi/battery/BAT?/ | cut -d " " -f 1) ]]
then
directories=$(echo /proc/acpi/battery/BAT?/)
else
exit 1
fi
# check different possibilities for storing the battery data
for directory in ${directories}
do
if [[ -f "${directory}charge_now" ]]
then
((current_charge+=$(cat "${directory}charge_now")))
((total_charge+=$(cat "${directory}charge_full")))
((design_charge+=$(cat "${directory}charge_full_design")))
elif [[ -f "${directory}energy_now" ]]
then
((current_charge+=$(cat "${directory}energy_now")))
((total_charge+=$(cat "${directory}energy_full")))
((design_charge+=$(cat "${directory}energy_full_design")))
elif [[ -f "${directory}state" ]]
then
((current_charge+=$(grep "remaining capacity:" "${directory}state" | cut -d " " -f 3)))
((total_charge+=$(grep "last full capacity:" "${directory}info" | cut -d " " -f 4)))
((design_charge+=$(grep "design capacity:" "${directory}info" | cut -d " " -f 3)))
else
exit 2
fi
done
# for apple (not tested)
else
battery_info=$(ioreg -rc AppleSmartBattery)
current_charge=$(echo ${battery_info} | grep -o '"CurrentCapacity" = [0-9]\+' | cut -d " " -f 3)
total_charge=$(echo ${battery_info} | grep -o '"MaxCapacity" = [0-9]\+' | cut -d " " -f 3)
design_charge=$(echo ${battery_info} | grep -o '"DesignCapacity" = [0-9]\+' | cut -d " " -f 3)
fi
echo ${current_charge}
echo ${total_charge}
echo ${design_charge}
}
# 2. calcuate slots
# $1 current charge
# $2 total charge
# $3 design charge
# ceil(x/y)=(x+y-1)/y with integer arithmetic
function calculate_slots {
current_charge=$1
total_charge=$2
design_charge=$3
charged_slots=$(((${current_charge}*${total_slots}+${design_charge}-1)/${design_charge}))
dead_slots=$((${total_slots}-(${total_charge}*${total_slots}+${design_charge}-1)/${design_charge}))
if [[ ${dead_slots} -lt 1 ]]
then
dead_slots=0
fi
echo ${charged_slots}
echo ${dead_slots}
}
# 3. print hearts
# $1 number of charged slots
# $2 number of dead slots
function print_hearts {
charged_slots=$1
dead_slots=$2
heart="♥"
if [[ ${charged_slots} -ge 1 ]]
then
echo -n "#[fg=red]"
for i in $(seq 1 ${charged_slots})
do
echo -n "${heart}"
done
fi
if [[ ${charged_slots} -lt ${total_slots} ]]
then
echo -n "#[fg=white]"
for i in $(seq 1 $((${total_slots}-(${charged_slots}+${dead_slots}))))
do
echo -n "${heart}"
done
fi
if [[ ${dead_slots} -ge 1 ]]
then
echo -n "#[fg=black]"
for i in $(seq 1 ${dead_slots})
do
echo -n "${heart}"
done
fi
}
function test_find_battery_data {
if [ -z "$(find_battery_data)" ]
then
echo "test_find_battery_data failed at line $LINENO"
fi
}
function test_calculate_slots {
if [ "$(calculate_slots 80 90 100)" != "8
1" ]
then
echo "test_calculate_slots failed at line $LINENO"
fi
if [ "$(calculate_slots 80 95 100)" != "8
0" ]
then
echo "test_calculate_slots failed at line $LINENO"
fi
}
function test_print_hearts {
if [ "$(print_hearts 8 1)" != "#[fg=red]♥♥♥♥♥♥♥♥#[fg=white]♥#[fg=black]♥" ]
then
echo "test_print_hearts failed at line $LINENO"
fi
if [ "$(print_hearts 9 0)" != "#[fg=red]♥♥♥♥♥♥♥♥♥#[fg=white]♥" ]
then
echo "test_print_hearts failed at line $LINENO"
fi
}
function test_all {
test_find_battery_data
test_calculate_slots
test_print_hearts
}
if [ "$1" == "test" ]
then
test_all
else
charges=$(find_battery_data)
slots=$(calculate_slots ${charges})
print_hearts ${slots}
fi