9aeb967581061ca12c1679d3fdce8a5b516a9796

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

fix(battery): fix system check so Termux uses the correct method

Diff

 1diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh
 2index 8e6afe65e0c4ad73f06754a4823267c7f34f61cc..0e96b00aaf7250bc687359e72f742678db488338 100644
 3--- a/plugins/battery/battery.plugin.zsh
 4+++ b/plugins/battery/battery.plugin.zsh
 5@@ -99,6 +99,46 @@ elif [[ "$OSTYPE" = freebsd* ]]; then
 6       echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
 7     fi
 8   }
 9+elif [[ "$OSTYPE" = linux-android ]] && (( ${+commands[termux-battery-status]} )); then
10+  function battery_is_charging() {
11+    termux-battery-status 2>/dev/null | command awk '/status/ { exit ($0 ~ /DISCHARGING/) }'
12+  }
13+  function battery_pct() {
14+    # Sample output:
15+    # {
16+    #   "health": "GOOD",
17+    #   "percentage": 93,
18+    #   "plugged": "UNPLUGGED",
19+    #   "status": "DISCHARGING",
20+    #   "temperature": 29.0,
21+    #   "current": 361816
22+    # }
23+    termux-battery-status 2>/dev/null | command awk '/percentage/ { gsub(/[,]/,""); print $2}'
24+  }
25+  function battery_pct_remaining() {
26+    if ! battery_is_charging; then
27+      battery_pct
28+    else
29+      echo "External Power"
30+    fi
31+  }
32+  function battery_time_remaining() { } # Not available on android
33+  function battery_pct_prompt() {
34+    local battery_pct color
35+    battery_pct=$(battery_pct_remaining)
36+    if battery_is_charging; then
37+      echo "∞"
38+    else
39+      if [[ $battery_pct -gt 50 ]]; then
40+        color='green'
41+      elif [[ $battery_pct -gt 20 ]]; then
42+        color='yellow'
43+      else
44+        color='red'
45+      fi
46+      echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
47+    fi
48+  }
49 elif [[ "$OSTYPE" = linux*  ]]; then
50   function battery_is_charging() {
51     if (( $+commands[acpitool] )); then
52@@ -163,46 +203,6 @@ elif [[ "$OSTYPE" = linux*  ]]; then
53       echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
54     fi
55   }
56-elif [[ "$OSTYPE" = linux-android ]] && (( ${+commands[termux-battery-status]} )); then
57-  function battery_is_charging() {
58-    termux-battery-status 2>/dev/null | command awk '/status/ { exit ($0 ~ /DISCHARGING/) }'
59-  }
60-  function battery_pct() {
61-    # Sample output:
62-    # {
63-    #   "health": "GOOD",
64-    #   "percentage": 93,
65-    #   "plugged": "UNPLUGGED",
66-    #   "status": "DISCHARGING",
67-    #   "temperature": 29.0,
68-    #   "current": 361816
69-    # }
70-    termux-battery-status 2>/dev/null | command awk '/percentage/ { gsub(/[,]/,""); print $2}'
71-  }
72-  function battery_pct_remaining() {
73-    if ! battery_is_charging; then
74-      battery_pct
75-    else
76-      echo "External Power"
77-    fi
78-  }
79-  function battery_time_remaining() { } # Not available on android
80-  function battery_pct_prompt() {
81-    local battery_pct color
82-    battery_pct=$(battery_pct_remaining)
83-    if battery_is_charging; then
84-      echo "∞"
85-    else
86-      if [[ $battery_pct -gt 50 ]]; then
87-        color='green'
88-      elif [[ $battery_pct -gt 20 ]]; then
89-        color='yellow'
90-      else
91-        color='red'
92-      fi
93-      echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
94-    fi
95-  }
96 else
97   # Empty functions so we don't cause errors in prompts
98   function battery_is_charging { false }