Skip to content

Commit a0de7f5

Browse files
committed
Fix termux
use termux-battery-status to get percentage detect jq is_wsl read /proc/version, which termux doesn't have permission. So detect termux-battery-status firstly.
1 parent 48fae59 commit a0de7f5

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

scripts/battery_percentage.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ source "$CURRENT_DIR/helpers.sh"
66

77
print_battery_percentage() {
88
# percentage displayed in the 2nd field of the 2nd row
9-
if is_wsl; then
10-
local battery
11-
battery=$(find /sys/class/power_supply/*/capacity | tail -n1)
12-
cat "$battery"
13-
elif command_exists "pmset"; then
9+
if command_exists "pmset"; then
1410
pmset -g batt | grep -o "[0-9]\{1,3\}%"
1511
elif command_exists "acpi"; then
1612
acpi -b | grep -m 1 -Eo "[0-9]+%"
@@ -36,6 +32,12 @@ print_battery_percentage() {
3632
termux-battery-status | jq -r '.percentage' | awk '{printf("%d%%", $1)}'
3733
elif command_exists "apm"; then
3834
apm -l
35+
elif command_exists "termux-battery-status" "jq"; then
36+
termux-battery-status | jq -er '"\(.percentage)%"'
37+
elif is_wsl; then
38+
local battery
39+
battery=$(find /sys/class/power_supply/*/capacity | tail -n1)
40+
cat "$battery"
3941
fi
4042
}
4143

scripts/helpers.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,21 @@ is_wsl() {
3232
}
3333

3434
command_exists() {
35-
local command="$1"
36-
type "$command" >/dev/null 2>&1
35+
local command
36+
for command; do
37+
type "$command" >/dev/null 2>&1 || return $?
38+
done
3739
}
3840

3941
battery_status() {
40-
if is_wsl; then
41-
local battery
42-
battery=$(find /sys/class/power_supply/*/status | tail -n1)
43-
awk '{print tolower($0);}' "$battery"
44-
elif command_exists "pmset"; then
42+
if command_exists "pmset"; then
4543
pmset -g batt | awk -F '; *' 'NR==2 { print $2 }'
4644
elif command_exists "acpi"; then
4745
acpi -b | awk '{gsub(/,/, ""); print tolower($3); exit}'
4846
elif command_exists "upower"; then
4947
local battery
5048
battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1)
5149
upower -i $battery | awk '/state/ {print $2}'
52-
elif command_exists "termux-battery-status"; then
53-
termux-battery-status | jq -r '.status' | awk '{printf("%s%", tolower($1))}'
5450
elif command_exists "apm"; then
5551
local battery
5652
battery=$(apm -a)
@@ -59,5 +55,11 @@ battery_status() {
5955
elif [ $battery -eq 1 ]; then
6056
echo "charging"
6157
fi
58+
elif command_exists "termux-battery-status" "jq"; then
59+
termux-battery-status | jq -er '.status | ascii_downcase'
60+
elif is_wsl; then
61+
local battery
62+
battery=$(find /sys/class/power_supply/*/status | tail -n1)
63+
awk '{print tolower($0);}' "$battery"
6264
fi
6365
}

0 commit comments

Comments
 (0)