dc83d0b7204cf66315471e980729490813b7d915

Author
Ryan Voots <simcop2387@simcop2387.info>
Committer
GitHub <noreply@github.com>
Date

Message

fix(battery): support `acpitool` and multiple batteries under Linux (#9609)

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh
  2index a525fd7da03a4539ea5090b12f4c424815ec947d..24bff8ae6a16d25070a029ad407e24a9da8b0990 100644
  3--- a/plugins/battery/battery.plugin.zsh
  4+++ b/plugins/battery/battery.plugin.zsh
  5@@ -12,15 +12,12 @@
  6 ###########################################
  7 
  8 if [[ "$OSTYPE" = darwin* ]]; then
  9-
 10   function battery_is_charging() {
 11     ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes'
 12   }
 13-
 14   function battery_pct() {
 15     pmset -g batt | grep -Eo "\d+%" | cut -d% -f1
 16   }
 17-
 18   function battery_pct_remaining() {
 19     if battery_is_charging; then
 20       echo "External Power"
 21@@ -28,7 +25,6 @@ if [[ "$OSTYPE" = darwin* ]]; then
 22       battery_pct
 23     fi
 24   }
 25-
 26   function battery_time_remaining() {
 27     local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
 28     if [[ $(echo $smart_battery_status | command grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]]; then
 29@@ -42,7 +38,6 @@ if [[ "$OSTYPE" = darwin* ]]; then
 30       echo "∞"
 31     fi
 32   }
 33-
 34   function battery_pct_prompt () {
 35     local battery_pct color
 36     if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then
 37@@ -61,17 +56,14 @@ if [[ "$OSTYPE" = darwin* ]]; then
 38   }
 39 
 40 elif [[ "$OSTYPE" = freebsd* ]]; then
 41-
 42   function battery_is_charging() {
 43     [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]]
 44   }
 45-
 46   function battery_pct() {
 47     if (( $+commands[sysctl] )); then
 48       sysctl -n hw.acpi.battery.life
 49     fi
 50   }
 51-
 52   function battery_pct_remaining() {
 53     if ! battery_is_charging; then
 54       battery_pct
 55@@ -79,7 +71,6 @@ elif [[ "$OSTYPE" = freebsd* ]]; then
 56       echo "External Power"
 57     fi
 58   }
 59-
 60   function battery_time_remaining() {
 61     local remaining_time
 62     remaining_time=$(sysctl -n hw.acpi.battery.time)
 63@@ -89,7 +80,6 @@ elif [[ "$OSTYPE" = freebsd* ]]; then
 64       printf %02d:%02d $hour $minute
 65     fi
 66   }
 67-
 68   function battery_pct_prompt() {
 69     local battery_pct color
 70     battery_pct=$(battery_pct_remaining)
 71@@ -106,19 +96,42 @@ elif [[ "$OSTYPE" = freebsd* ]]; then
 72       echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
 73     fi
 74   }
 75-
 76 elif [[ "$OSTYPE" = linux*  ]]; then
 77-
 78   function battery_is_charging() {
 79-    ! acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -q '^Battery.*Discharging'
 80+    if (( $+commands[acpitool] )); then
 81+      ! acpitool 2>/dev/null | command grep -qE '^\s+Battery.*Discharging'
 82+    elif (( $+commands[acpi] )); then
 83+      ! acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -q '^Battery.*Discharging'
 84+    fi
 85   }
 86-
 87   function battery_pct() {
 88-    if (( $+commands[acpi] )); then
 89-      acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -E '^Battery.*(Full|(Disc|C)harging)' | cut -f2 -d ',' | tr -cd '[:digit:]'
 90+    if (( $+commands[acpitool] )); then
 91+      # Sample output:
 92+      #   Battery #1     : Unknown, 99.55%
 93+      #   Battery #2     : Discharging, 49.58%, 01:12:05
 94+      #   All batteries  : 62.60%, 02:03:03
 95+      acpitool 2>/dev/null | command awk -F, '
 96+        /^\s+All batteries/ {
 97+          gsub(/[^0-9.]/, "", $1)
 98+          pct=$1
 99+          exit
100+        }
101+        !pct && /^\s+Battery/ {
102+          gsub(/[^0-9.]/, "", $2)
103+          pct=$2
104+        }