Skip to content

Commit 5c52d4f

Browse files
authored
Add support for WSL2 and for alternate powersupply names (#100)
1 parent f865b04 commit 5c52d4f

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

scripts/battery_percentage.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ source "$CURRENT_DIR/helpers.sh"
77
print_battery_percentage() {
88
# percentage displayed in the 2nd field of the 2nd row
99
if is_wsl; then
10-
cat /sys/class/power_supply/battery/capacity
10+
local battery
11+
battery=$(find /sys/class/power_supply/*/capacity | tail -n1)
12+
cat "$battery"
1113
elif command_exists "pmset"; then
1214
pmset -g batt | grep -o "[0-9]\{1,3\}%"
1315
elif command_exists "acpi"; then

scripts/battery_remain.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ acpi_battery_remaining_time() {
110110
}
111111

112112
print_battery_remain() {
113-
if command_exists "pmset"; then
113+
if is_wsl; then
114+
echo "?" # currently unsupported on WSL
115+
elif command_exists "pmset"; then
114116
pmset_battery_remaining_time
115117
elif command_exists "acpi"; then
116118
acpi_battery_remaining_time

scripts/helpers.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ is_chrome() {
2424

2525
is_wsl() {
2626
version=$(</proc/version)
27-
if [[ "$version" == *"Microsoft"* ]]; then
27+
if [[ "$version" == *"Microsoft"* || "$version" == *"microsoft"* ]]; then
2828
return 0
2929
else
3030
return 1
@@ -38,7 +38,9 @@ command_exists() {
3838

3939
battery_status() {
4040
if is_wsl; then
41-
cat /sys/class/power_supply/battery/status | awk '{print tolower($0);}'
41+
local battery
42+
battery=$(find /sys/class/power_supply/*/status | tail -n1)
43+
awk '{print tolower($0);}' "$battery"
4244
elif command_exists "pmset"; then
4345
pmset -g batt | awk -F '; *' 'NR==2 { print $2 }'
4446
elif command_exists "acpi"; then

0 commit comments

Comments
 (0)