Skip to content

Commit ad98fe3

Browse files
authored
Merge pull request #480 from xx4h/fix/455/get_all_batteries
fix: get power supplies of type battery
2 parents 557ae9b + 101abc6 commit ad98fe3

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

segments/battery.sh

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,6 @@ __battery_macos() {
104104
__battery_linux() {
105105
case "$SHELL_PLATFORM" in
106106
"linux")
107-
BATPATH=/sys/class/power_supply/BAT0
108-
if [ ! -d $BATPATH ]; then
109-
BATPATH=/sys/class/power_supply/BAT1
110-
fi
111-
BAT_FULL=$BATPATH/charge_full
112-
if [ ! -r $BAT_FULL ]; then
113-
BAT_FULL=$BATPATH/energy_full
114-
fi
115-
BAT_NOW=$BATPATH/charge_now
116-
if [ ! -r $BAT_NOW ]; then
117-
BAT_NOW=$BATPATH/energy_now
118-
fi
119107
__linux_get_bat
120108
;;
121109
"bsd")
@@ -158,28 +146,26 @@ __linux_get_bat() {
158146
local total_full=0
159147
local total_now=0
160148

161-
for bat in /sys/class/power_supply/BAT*; do
162-
if [ -d "$bat" ]; then
163-
local full="$bat/charge_full"
164-
local now="$bat/charge_now"
149+
while read -r bat; do
150+
local full="$bat/charge_full"
151+
local now="$bat/charge_now"
165152

166-
if [ ! -r "$full" ]; then
167-
full="$bat/energy_full"
168-
fi
169-
if [ ! -r "$now" ]; then
170-
now="$bat/energy_now"
171-
fi
153+
if [ ! -r "$full" ]; then
154+
full="$bat/energy_full"
155+
fi
156+
if [ ! -r "$now" ]; then
157+
now="$bat/energy_now"
158+
fi
172159

173-
if [ -r "$full" ] && [ -r "$now" ]; then
174-
local bf
175-
local bn
176-
bf=$(cat "$full")
177-
bn=$(cat "$now")
178-
total_full=$((total_full + bf))
179-
total_now=$((total_now + bn))
180-
fi
160+
if [ -r "$full" ] && [ -r "$now" ]; then
161+
local bf
162+
local bn
163+
bf=$(cat "$full")
164+
bn=$(cat "$now")
165+
total_full=$((total_full + bf))
166+
total_now=$((total_now + bn))
181167
fi
182-
done
168+
done <<<"$(grep -l "Battery" /sys/class/power_supply/*/type | sed -e 's,/type$,,')"
183169

184170
if [ "$total_full" -gt 0 ]; then
185171
if [ "$total_now" -gt "$total_full" ]; then

0 commit comments

Comments
 (0)