From 5d2a760802369d7b5b84d071e242664324748483 Mon Sep 17 00:00:00 2001 From: Paul Kim <44695374+thekpaul@users.noreply.github.com> Date: Mon, 24 Apr 2023 15:49:35 +0900 Subject: [PATCH 1/6] Use `echo` and Append Percentage Symbol Instead of straightforwardly calling `cat`, I've encased the command in `echo` to enable appending strings. This allows me to add a percentage symbol after calling `cat "$battery"`! This also suppresses any `echo`s for machines without battery (i.e. a desktop environment) --- scripts/battery_percentage.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/battery_percentage.sh b/scripts/battery_percentage.sh index d5dce55..18cbe9d 100755 --- a/scripts/battery_percentage.sh +++ b/scripts/battery_percentage.sh @@ -7,9 +7,11 @@ 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" + local battery=$(find /sys/class/power_supply/*/capacity | tail -n1) + if [ -n "$battery" ]; then + echo $(cat "$battery")% + return + fi elif command_exists "pmset"; then pmset -g batt | grep -o "[0-9]\{1,3\}%" elif command_exists "acpi"; then From 95a684387c93bc9f529a22d750cd827b9049fd71 Mon Sep 17 00:00:00 2001 From: Paul Kim <44695374+thekpaul@users.noreply.github.com> Date: Mon, 24 Apr 2023 23:42:05 +0900 Subject: [PATCH 2/6] Check Battery Existence before Calling and Printing Content This ensures that the script does not return errors when operating in environments without batteries, as if there are no battery-related files in the target directory, the script now stops from accessing the files to begin with. --- scripts/battery_percentage.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/scripts/battery_percentage.sh b/scripts/battery_percentage.sh index 18cbe9d..7f3714e 100755 --- a/scripts/battery_percentage.sh +++ b/scripts/battery_percentage.sh @@ -7,10 +7,8 @@ source "$CURRENT_DIR/helpers.sh" print_battery_percentage() { # percentage displayed in the 2nd field of the 2nd row if is_wsl; then - local battery=$(find /sys/class/power_supply/*/capacity | tail -n1) - if [ -n "$battery" ]; then - echo $(cat "$battery")% - return + 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\}%" From 1e656e60882dd84d73cfa2710b8319deccba628a Mon Sep 17 00:00:00 2001 From: Paul Kim <44695374+thekpaul@users.noreply.github.com> Date: Tue, 25 Apr 2023 00:08:56 +0900 Subject: [PATCH 3/6] Pull Status ONLY IF there is Status Available to Pull --- scripts/helpers.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 270e688..cc4b5cc 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -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 From ded3c406f2f6c88ad79612cc74b3fe2e088aed80 Mon Sep 17 00:00:00 2001 From: Paul Kim <44695374+thekpaul@users.noreply.github.com> Date: Tue, 25 Apr 2023 00:33:40 +0900 Subject: [PATCH 4/6] Refactor Conditional Statement to Capture Desktop Assumption --- scripts/battery_icon_charge.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/battery_icon_charge.sh b/scripts/battery_icon_charge.sh index 8bdd568..1dff148 100755 --- a/scripts/battery_icon_charge.sh +++ b/scripts/battery_icon_charge.sh @@ -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 From 3b20bf7c397b8e2dfd89e2691e0ad4600c47f668 Mon Sep 17 00:00:00 2001 From: Paul Kim <44695374+thekpaul@users.noreply.github.com> Date: Tue, 25 Apr 2023 00:34:04 +0900 Subject: [PATCH 5/6] Add WSL Desktop Assumtion Conditional Before Assessing Status --- scripts/battery_icon_status.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/battery_icon_status.sh b/scripts/battery_icon_status.sh index e1873fb..81fb037 100755 --- a/scripts/battery_icon_status.sh +++ b/scripts/battery_icon_status.sh @@ -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" From 889f2a19dcdf0bbd9a3dfc19370da483816a8309 Mon Sep 17 00:00:00 2001 From: Paul Kim <44695374+thekpaul@users.noreply.github.com> Date: Tue, 2 May 2023 13:59:23 +0900 Subject: [PATCH 6/6] Patch for UNIXes without `/proc`, such as iSH --- scripts/helpers.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index cc4b5cc..3957a57 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -23,7 +23,7 @@ is_chrome() { } is_wsl() { - version=$( /dev/null cat /proc/version) if [[ "$version" == *"Microsoft"* || "$version" == *"microsoft"* ]]; then return 0 else