5f6f7b6e8de8e6b64f07deb6b185535a8cdbe03e

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Various syntax fixes and function naming equivalence

- Fix code style
- Fix local definitions
- Don't declare unnecessary variables
- Use `command` before grep

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh
  2index c1037ad2643be9b19435595139127242c5dd156c..7c5bf6f82576b37f8b68d01dced300d3cd5f8a3b 100644
  3--- a/plugins/battery/battery.plugin.zsh
  4+++ b/plugins/battery/battery.plugin.zsh
  5@@ -11,22 +11,21 @@
  6 # Modified to add support for FreeBSD     #
  7 ###########################################
  8 
  9-if [[ "$OSTYPE" = darwin* ]] ; then
 10+if [[ "$OSTYPE" = darwin* ]]; then
 11 
 12-  function battery_pct() {
 13-    local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
 14-    typeset -F maxcapacity=$(echo $smart_battery_status | grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
 15-    typeset -F currentcapacity=$(echo $smart_battery_status | grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
 16-    integer i=$(((currentcapacity/maxcapacity) * 100))
 17-    echo $i
 18+  function battery_is_charging() {
 19+    ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes'
 20   }
 21 
 22-  function plugged_in() {
 23-    [ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ Yes') -eq 1 ]
 24+  function battery_pct() {
 25+    local smart_battery_status="$(ioreg -rc AppleSmartBattery)"
 26+    local -F maxcapacity=$(command grep '^.*"MaxCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*"MaxCapacity"\ =\ //')
 27+    local -F currentcapacity=$(command grep '^.*"CurrentCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*CurrentCapacity"\ =\ //')
 28+    echo $(( (currentcapacity/maxcapacity) * 100 ))
 29   }
 30 
 31   function battery_pct_remaining() {
 32-    if plugged_in ; then
 33+    if battery_is_charging; then
 34       echo "External Power"
 35     else
 36       battery_pct
 37@@ -35,9 +34,9 @@ if [[ "$OSTYPE" = darwin* ]] ; then
 38 
 39   function battery_time_remaining() {
 40     local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
 41-    if [[ $(echo $smart_battery_status | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
 42-      timeremaining=$(echo $smart_battery_status | grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
 43-      if [ $timeremaining -gt 720 ] ; then
 44+    if [[ $(echo $smart_battery_status | command grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]]; then
 45+      timeremaining=$(echo $smart_battery_status | command grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
 46+      if [ $timeremaining -gt 720 ]; then
 47         echo "::"
 48       else
 49         echo "~$((timeremaining / 60)):$((timeremaining % 60))"
 50@@ -48,11 +47,11 @@ if [[ "$OSTYPE" = darwin* ]] ; then
 51   }
 52 
 53   function battery_pct_prompt () {
 54-    if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
 55+    if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then
 56       b=$(battery_pct_remaining)
 57-      if [ $b -gt 50 ] ; then
 58+      if [[ $b -gt 50 ]]; then
 59         color='green'
 60-      elif [ $b -gt 20 ] ; then
 61+      elif [[ $b -gt 20 ]]; then
 62         color='yellow'
 63       else
 64         color='red'
 65@@ -63,24 +62,20 @@ if [[ "$OSTYPE" = darwin* ]] ; then
 66     fi
 67   }
 68 
 69-  function battery_is_charging() {
 70-    [[ $(ioreg -rc "AppleSmartBattery"| grep '^.*"IsCharging"\ =\ ' | sed -e 's/^.*"IsCharging"\ =\ //') == "Yes" ]]
 71-  }
 72-
 73-elif [[ "$OSTYPE" = freebsd*  ]] ; then
 74+elif [[ "$OSTYPE" = freebsd* ]]; then
 75 
 76   function battery_is_charging() {
 77     [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]]
 78   }
 79 
 80   function battery_pct() {
 81-    if (( $+commands[sysctl] )) ; then
 82-      echo "$(sysctl -n hw.acpi.battery.life)"
 83+    if (( $+commands[sysctl] )); then
 84+      sysctl -n hw.acpi.battery.life
 85     fi
 86   }
 87 
 88   function battery_pct_remaining() {
 89-    if [ ! $(battery_is_charging) ] ; then
 90+    if ! battery_is_charging; then
 91       battery_pct
 92     else
 93       echo "External Power"
 94@@ -88,39 +83,40 @@ elif [[ "$OSTYPE" = freebsd*  ]] ; then
 95   }
 96 
 97   function battery_time_remaining() {
 98+    local remaining_time
 99     remaining_time=$(sysctl -n hw.acpi.battery.time)
100-    if [[ $remaining_time -ge 0 ]] ; then
101-      # calculation from https://www.unix.com/shell-programming-and-scripting/23695-convert-minutes-hours-minutes-seconds.html
102-      ((hour=$remaining_time/60))
103-      ((minute=$remaining_time-$hour*60))
104-      echo $hour:$minute