7504f22a0ce649ab577a6232f2a673ea98d773a6

Author
Jérémy <PixiBixi@users.noreply.github.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(battery): add wattage for macOS (#13258)

Diff

 1diff --git a/plugins/battery/README.md b/plugins/battery/README.md
 2index 73fcb693e625aa90a9906515266308c7f3510245..07e4e0b16c5b5b93467fc34ab82b2735534f591f 100644
 3--- a/plugins/battery/README.md
 4+++ b/plugins/battery/README.md
 5@@ -19,6 +19,12 @@ For example:
 6 BATTERY_CHARGING="⚡️"
 7 ```
 8 
 9+You can see the power of your charger using the following setting (MacOS only)
10+
11+```zsh
12+BATTERY_SHOW_WATTS=true
13+```
14+
15 ## Requirements
16 
17 - On Linux, you must have the `acpi` or `acpitool` commands installed on your operating system.
18diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh
19index 7977e4d045e8938c31889c01acf6de6a7f34c787..c7618f6b3bcf5a44bd3ddd0c4f365b35ba48ca11 100644
20--- a/plugins/battery/battery.plugin.zsh
21+++ b/plugins/battery/battery.plugin.zsh
22@@ -17,8 +17,13 @@
23 # Modified to add support for OpenBSD     #
24 ###########################################
25 
26+: ${BATTERY_SHOW_WATTS:=false}
27+
28 
29 if [[ "$OSTYPE" = darwin* ]]; then
30+  function get_charger_power() {
31+    echo "$(ioreg -rc AppleSmartBattery | grep -o '"Watts"=[0-9]\+' | head -1 | grep -o '[0-9]\+')W "
32+  }
33   function battery_is_charging() {
34     ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes'
35   }
36@@ -58,7 +63,10 @@ if [[ "$OSTYPE" = darwin* ]]; then
37       fi
38       echo "%{$fg[$color]%}[${battery_pct}%%]%{$reset_color%}"
39     else
40-      echo "${BATTERY_CHARGING-⚡️}"
41+      if [[ "${BATTERY_SHOW_WATTS}" = "true" ]] ; then
42+        watts=$(get_charger_power)
43+      fi
44+      echo "${watts}${BATTERY_CHARGING-⚡️}"
45     fi
46   }
47