f26a1ecdf01185218004dd6d15ce02541e6a2b26

Author
Avneet Singh <4151485+kalsi-avneet@users.noreply.github.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

feat(battery): add support for Android via Termux (#9752)

Diff

 1diff --git a/plugins/battery/README.md b/plugins/battery/README.md
 2index c2554a36db149b9a00111bbf99f35d1fa3ef535a..18e5bd882702fc4049de8fc193b8e34d19c176b6 100644
 3--- a/plugins/battery/README.md
 4+++ b/plugins/battery/README.md
 5@@ -8,15 +8,22 @@ To use, add `battery` to the list of plugins in your `.zshrc` file:
 6 
 7 Then, add the `battery_pct_prompt` function to your custom theme. For example:
 8 
 9-```
10+```zsh
11 RPROMPT='$(battery_pct_prompt) ...'
12 ```
13 
14 ## Requirements
15 
16-On Linux, you must have the `acpi` tool installed on your operating system.
17+- On Linux, you must have the `acpi` or `acpitool` commands installed on your operating system.
18+  On Debian/Ubuntu, you can do that with `sudo apt install acpi` or `sudo apt install acpitool`.
19 
20-Here's an example of how to install with apt:
21-```
22-sudo apt-get install acpi
23-```
24+- On Android (via [Termux](https://play.google.com/store/apps/details?id=com.termux)), you must have:
25+
26+  1. The `Termux:API` addon app installed:
27+     [Google Play](https://play.google.com/store/apps/details?id=com.termux.api) | [F-Droid](https://f-droid.org/packages/com.termux.api/)
28+
29+  2. The `termux-api` package installed within termux:
30+
31+     ```sh
32+     pkg install termux-api
33+     ```
34diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh
35index 24bff8ae6a16d25070a029ad407e24a9da8b0990..8e6afe65e0c4ad73f06754a4823267c7f34f61cc 100644
36--- a/plugins/battery/battery.plugin.zsh
37+++ b/plugins/battery/battery.plugin.zsh
38@@ -10,6 +10,9 @@
39 # Author: J (927589452)                   #
40 # Modified to add support for FreeBSD     #
41 ###########################################
42+# Author: Avneet Singh (kalsi-avneet)     #
43+# Modified to add support for Android     #
44+###########################################
45 
46 if [[ "$OSTYPE" = darwin* ]]; then
47   function battery_is_charging() {
48@@ -160,6 +163,46 @@ elif [[ "$OSTYPE" = linux*  ]]; then
49       echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
50     fi
51   }
52+elif [[ "$OSTYPE" = linux-android ]] && (( ${+commands[termux-battery-status]} )); then
53+  function battery_is_charging() {
54+    termux-battery-status 2>/dev/null | command awk '/status/ { exit ($0 ~ /DISCHARGING/) }'
55+  }
56+  function battery_pct() {
57+    # Sample output:
58+    # {
59+    #   "health": "GOOD",
60+    #   "percentage": 93,
61+    #   "plugged": "UNPLUGGED",
62+    #   "status": "DISCHARGING",
63+    #   "temperature": 29.0,
64+    #   "current": 361816
65+    # }
66+    termux-battery-status 2>/dev/null | command awk '/percentage/ { gsub(/[,]/,""); print $2}'
67+  }
68+  function battery_pct_remaining() {
69+    if ! battery_is_charging; then
70+      battery_pct
71+    else
72+      echo "External Power"
73+    fi
74+  }
75+  function battery_time_remaining() { } # Not available on android
76+  function battery_pct_prompt() {
77+    local battery_pct color
78+    battery_pct=$(battery_pct_remaining)
79+    if battery_is_charging; then
80+      echo "∞"
81+    else
82+      if [[ $battery_pct -gt 50 ]]; then
83+        color='green'
84+      elif [[ $battery_pct -gt 20 ]]; then
85+        color='yellow'
86+      else
87+        color='red'
88+      fi
89+      echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
90+    fi
91+  }
92 else
93   # Empty functions so we don't cause errors in prompts
94   function battery_is_charging { false }