Skip to content

Extended Support for Battery Detection and Display on WSL #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/battery_icon_charge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ get_icon_charge_settings() {

print_icon_charge() {
percentage=$($CURRENT_DIR/battery_percentage.sh | sed -e 's/%//')
if [ $percentage -ge 95 -o "$percentage" == "" ]; then
if [[ $percentage -ge 95 || -z "$percentage" ]]; then
# if percentage is empty, assume it's a desktop
printf "$icon_charge_tier8"
elif [ $percentage -ge 80 ]; then
Expand Down
5 changes: 4 additions & 1 deletion scripts/battery_icon_status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ get_icon_status_settings() {

print_icon_status() {
local status=$1
if [[ $status =~ (charged) || $status =~ (full) ]]; then
if [[ is_wsl && -z $status ]]; then
# WSL env with no battery -> _probably_ gonna be a desktop!
printf "$icon_status_charged"
elif [[ $status =~ (charged) || $status =~ (full) ]]; then
printf "$icon_status_charged"
elif [[ $status =~ (^charging) ]]; then
printf "$icon_status_charging"
Expand Down
6 changes: 3 additions & 3 deletions scripts/battery_percentage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ source "$CURRENT_DIR/helpers.sh"
print_battery_percentage() {
# percentage displayed in the 2nd field of the 2nd row
if is_wsl; then
local battery
battery=$(find /sys/class/power_supply/*/capacity | tail -n1)
cat "$battery"
if [ -e /sys/class/power_supply/*/capacity ]; then
echo $(cat $(find /sys/class/power_supply/*/capacity | tail -n1))%
fi
elif command_exists "pmset"; then
pmset -g batt | grep -o "[0-9]\{1,3\}%"
elif command_exists "acpi"; then
Expand Down
10 changes: 6 additions & 4 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ is_chrome() {
}

is_wsl() {
version=$(</proc/version)
version=$(2> /dev/null cat /proc/version)
if [[ "$version" == *"Microsoft"* || "$version" == *"microsoft"* ]]; then
return 0
else
Expand All @@ -38,9 +38,11 @@ command_exists() {

battery_status() {
if is_wsl; then
local battery
battery=$(find /sys/class/power_supply/*/status | tail -n1)
awk '{print tolower($0);}' "$battery"
if [ -e /sys/class/power_supply/*/status ]; then
local battery
battery=$(find /sys/class/power_supply/*/status | tail -n1)
awk '{print tolower($0);}' "$battery"
fi
elif command_exists "pmset"; then
pmset -g batt | awk -F '; *' 'NR==2 { print $2 }'
elif command_exists "acpi"; then
Expand Down