From 7d9e63e4c1e3a7cb11c31b07c6a87149887f6942 Mon Sep 17 00:00:00 2001 From: Wu Zhenyu Date: Tue, 27 Feb 2024 19:24:55 +0800 Subject: [PATCH] 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. --- scripts/battery_percentage.sh | 4 +++- scripts/helpers.sh | 12 +++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/scripts/battery_percentage.sh b/scripts/battery_percentage.sh index d5dce55..1865c23 100755 --- a/scripts/battery_percentage.sh +++ b/scripts/battery_percentage.sh @@ -6,7 +6,9 @@ source "$CURRENT_DIR/helpers.sh" print_battery_percentage() { # percentage displayed in the 2nd field of the 2nd row - if is_wsl; then + if command_exists "termux-battery-status" "jq"; then + termux-battery-status | jq -er '"\(.percentage)%"' + elif is_wsl; then local battery battery=$(find /sys/class/power_supply/*/capacity | tail -n1) cat "$battery" diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 270e688..2e5138b 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -32,12 +32,16 @@ is_wsl() { } command_exists() { - local command="$1" - type "$command" >/dev/null 2>&1 + local command + for command; do + type "$command" >/dev/null 2>&1 || return $? + done } battery_status() { - if is_wsl; then + if command_exists "termux-battery-status" "jq"; then + termux-battery-status | jq -er '.status | ascii_downcase' + elif is_wsl; then local battery battery=$(find /sys/class/power_supply/*/status | tail -n1) awk '{print tolower($0);}' "$battery" @@ -49,8 +53,6 @@ battery_status() { local battery battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1) upower -i $battery | awk '/state/ {print $2}' - elif command_exists "termux-battery-status"; then - termux-battery-status | jq -r '.status' | awk '{printf("%s%", tolower($1))}' elif command_exists "apm"; then local battery battery=$(apm -a)