49c423c7e0a6d5193b77e60107e7c329da28d987

Author
927589452 <927589452@users.noreply.github.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

battery: add support for sysctl in FreeBSD (#8155)

Diff

 1diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh
 2index 8f398cfb34214953a1934c884da0d217cd995126..6b668471606f9ac8248f0898815a45deb37ba927 100644
 3--- a/plugins/battery/battery.plugin.zsh
 4+++ b/plugins/battery/battery.plugin.zsh
 5@@ -7,6 +7,9 @@
 6 # Email: neuralsandwich@gmail.com         #
 7 # Modified to add support for Apple Mac   #
 8 ###########################################
 9+# Author: J (927589452)                   #
10+# Modified to add support for FreeBSD     #
11+###########################################
12 
13 if [[ "$OSTYPE" = darwin* ]] ; then
14 
15@@ -64,6 +67,52 @@ if [[ "$OSTYPE" = darwin* ]] ; then
16     [[ $(ioreg -rc "AppleSmartBattery"| grep '^.*"IsCharging"\ =\ ' | sed -e 's/^.*"IsCharging"\ =\ //') == "Yes" ]]
17   }
18 
19+elif [[ "$OSTYPE" = freebsd*  ]] ; then
20+
21+  function battery_is_charging() {
22+    [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]]
23+  }
24+
25+  function battery_pct() {
26+    if (( $+commands[sysctl] )) ; then
27+      echo "$(sysctl -n hw.acpi.battery.life)"
28+    fi
29+  }
30+
31+  function battery_pct_remaining() {
32+    if [ ! $(battery_is_charging) ] ; then
33+      battery_pct
34+    else
35+      echo "External Power"
36+    fi
37+  }
38+
39+  function battery_time_remaining() {
40+    remaining_time=$(sysctl -n hw.acpi.battery.time)
41+    if [[ $remaining_time -ge 0 ]] ; then
42+      # calculation from https://www.unix.com/shell-programming-and-scripting/23695-convert-minutes-hours-minutes-seconds.html
43+      ((hour=$remaining_time/60))
44+      ((minute=$remaining_time-$hour*60))
45+      echo $hour:$minute
46+    fi
47+  }
48+
49+  function battery_pct_prompt() {
50+    b=$(battery_pct_remaining)
51+    if [ ! $(battery_is_charging) ] ; then
52+      if [ $b -gt 50 ] ; then
53+        color='green'
54+      elif [ $b -gt 20 ] ; then
55+        color='yellow'
56+      else
57+        color='red'
58+      fi
59+      echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}"
60+    else
61+      echo "∞"
62+    fi
63+  }
64+
65 elif [[ "$OSTYPE" = linux*  ]] ; then
66 
67   function battery_is_charging() {