diff options
Diffstat (limited to 'plugins/battery')
| -rw-r--r-- | plugins/battery/README.md | 13 | ||||
| -rw-r--r-- | plugins/battery/battery.plugin.zsh | 53 | 
2 files changed, 64 insertions, 2 deletions
| diff --git a/plugins/battery/README.md b/plugins/battery/README.md new file mode 100644 index 000000000..b7a13a7ec --- /dev/null +++ b/plugins/battery/README.md @@ -0,0 +1,13 @@ +# Battery Plugin + +This plugin adds some functions you can use to display battery information in your custom theme. + +To use, add `battery` to the list of plugins in your `.zshrc` file: + +`plugins=(... battery)` + +Then, add the `battery_pct_prompt` function to your custom theme. For example: + +``` +RPROMPT='$(battery_pct_prompt)' +``` diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index 0bb9e77f0..6b6684716 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -7,6 +7,9 @@  # Email: neuralsandwich@gmail.com         #  # Modified to add support for Apple Mac   #  ########################################### +# Author: J (927589452)                   # +# Modified to add support for FreeBSD     # +###########################################  if [[ "$OSTYPE" = darwin* ]] ; then @@ -64,7 +67,53 @@ if [[ "$OSTYPE" = darwin* ]] ; then      [[ $(ioreg -rc "AppleSmartBattery"| grep '^.*"IsCharging"\ =\ ' | sed -e 's/^.*"IsCharging"\ =\ //') == "Yes" ]]    } -elif [[ $(uname) == "Linux"  ]] ; then +elif [[ "$OSTYPE" = freebsd*  ]] ; then + +  function battery_is_charging() { +    [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]] +  } + +  function battery_pct() { +    if (( $+commands[sysctl] )) ; then +      echo "$(sysctl -n hw.acpi.battery.life)" +    fi +  } + +  function battery_pct_remaining() { +    if [ ! $(battery_is_charging) ] ; then +      battery_pct +    else +      echo "External Power" +    fi +  } + +  function battery_time_remaining() { +    remaining_time=$(sysctl -n hw.acpi.battery.time) +    if [[ $remaining_time -ge 0 ]] ; then +      # calculation from https://www.unix.com/shell-programming-and-scripting/23695-convert-minutes-hours-minutes-seconds.html +      ((hour=$remaining_time/60)) +      ((minute=$remaining_time-$hour*60)) +      echo $hour:$minute +    fi +  } + +  function battery_pct_prompt() { +    b=$(battery_pct_remaining) +    if [ ! $(battery_is_charging) ] ; then +      if [ $b -gt 50 ] ; then +        color='green' +      elif [ $b -gt 20 ] ; then +        color='yellow' +      else +        color='red' +      fi +      echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}" +    else +      echo "∞" +    fi +  } + +elif [[ "$OSTYPE" = linux*  ]] ; then    function battery_is_charging() {      ! [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] @@ -100,7 +149,7 @@ elif [[ $(uname) == "Linux"  ]] ; then        else          color='red'        fi -      echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}" +      echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}"      else        echo "∞"      fi | 
