Skip to content

Commit 0df96af

Browse files
committed
1 parent de9b473 commit 0df96af

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

scripts/battery_percentage.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ 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
9+
if command_exists "termux-battery-status" "jq"; then
10+
termux-battery-status | jq -r .percentage
11+
elif is_wsl; then
1012
if [ -e /sys/class/power_supply/*/capacity ]; then
1113
echo $(cat $(find /sys/class/power_supply/*/capacity | tail -n1))%
1214
fi

scripts/helpers.sh

+8-5
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ 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
38+
(($? == 0)) || return $?
39+
done
3740
}
3841

3942
battery_status() {
40-
if is_wsl; then
43+
if command_exists "termux-battery-status" "jq"; then
44+
termux-battery-status | jq -r '.status' | awk '{printf("%s%", tolower($1))}'
45+
elif is_wsl; then
4146
if [ -e /sys/class/power_supply/*/status ]; then
4247
local battery
4348
battery=$(find /sys/class/power_supply/*/status | tail -n1)
@@ -51,8 +56,6 @@ battery_status() {
5156
local battery
5257
battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1)
5358
upower -i $battery | awk '/state/ {print $2}'
54-
elif command_exists "termux-battery-status"; then
55-
termux-battery-status | jq -r '.status' | awk '{printf("%s%", tolower($1))}'
5659
elif command_exists "apm"; then
5760
local battery
5861
battery=$(apm -a)

0 commit comments

Comments
 (0)