battery.plugin.zsh 6.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
###########################################
# Battery plugin for oh-my-zsh            #
# Original Author: Peter hoeg (peterhoeg) #
# Email: peter@speartail.com              #
###########################################
# Author: Sean Jones (neuralsandwich)     #
# Email: neuralsandwich@gmail.com         #
# Modified to add support for Apple Mac   #
###########################################
10
11
12
# Author: J (927589452)                   #
# Modified to add support for FreeBSD     #
###########################################
13

14
if [[ "$OSTYPE" = darwin* ]]; then
15

16
17
  function battery_is_charging() {
    ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes'
18
  }
Ron Shapiro's avatar
Ron Shapiro committed
19

20
21
22
23
  function battery_pct() {
    local smart_battery_status="$(ioreg -rc AppleSmartBattery)"
    local -F maxcapacity=$(command grep '^.*"MaxCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*"MaxCapacity"\ =\ //')
    local -F currentcapacity=$(command grep '^.*"CurrentCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*CurrentCapacity"\ =\ //')
24
25
    local -i pct=$(( (currentcapacity/maxcapacity) * 100 ))
    echo $pct
Ron Shapiro's avatar
Ron Shapiro committed
26
  }
27

28
  function battery_pct_remaining() {
29
    if battery_is_charging; then
30
      echo "External Power"
Ron Shapiro's avatar
Ron Shapiro committed
31
32
    else
      battery_pct
Peter Hoeg's avatar
Peter Hoeg committed
33
34
    fi
  }
35
36

  function battery_time_remaining() {
37
    local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
38
39
40
    if [[ $(echo $smart_battery_status | command grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]]; then
      timeremaining=$(echo $smart_battery_status | command grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
      if [ $timeremaining -gt 720 ]; then
41
42
43
44
        echo "::"
      else
        echo "~$((timeremaining / 60)):$((timeremaining % 60))"
      fi
45
46
47
48
49
50
    else
      echo "∞"
    fi
  }

  function battery_pct_prompt () {
51
    local battery_pct color
52
    if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then
53
54
      battery_pct=$(battery_pct_remaining)
      if [[ $battery_pct -gt 50 ]]; then
55
        color='green'
56
      elif [[ $battery_pct -gt 20 ]]; then
57
58
59
60
        color='yellow'
      else
        color='red'
      fi
61
      echo "%{$fg[$color]%}[${battery_pct}%%]%{$reset_color%}"
62
    else
63
      echo "∞"
64
65
    fi
  }
66

67
elif [[ "$OSTYPE" = freebsd* ]]; then
68
69
70
71
72
73

  function battery_is_charging() {
    [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]]
  }

  function battery_pct() {
74
75
    if (( $+commands[sysctl] )); then
      sysctl -n hw.acpi.battery.life
76
77
78
79
    fi
  }

  function battery_pct_remaining() {
80
    if ! battery_is_charging; then
81
82
83
84
85
86
87
      battery_pct
    else
      echo "External Power"
    fi
  }

  function battery_time_remaining() {
88
    local remaining_time
89
    remaining_time=$(sysctl -n hw.acpi.battery.time)
90
91
92
93
    if [[ $remaining_time -ge 0 ]]; then
      ((hour = $remaining_time / 60 ))
      ((minute = $remaining_time % 60 ))
      printf %02d:%02d $hour $minute
94
95
96
97
    fi
  }

  function battery_pct_prompt() {
98
99
    local battery_pct color
    battery_pct=$(battery_pct_remaining)
100
101
102
    if battery_is_charging; then
      echo "∞"
    else
103
      if [[ $battery_pct -gt 50 ]]; then
104
        color='green'
105
      elif [[ $battery_pct -gt 20 ]]; then
106
107
108
109
        color='yellow'
      else
        color='red'
      fi
110
      echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
111
112
113
    fi
  }

114
elif [[ "$OSTYPE" = linux*  ]]; then
115

116
  function battery_is_charging() {
GregoireW's avatar
GregoireW committed
117
    ! acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -q '^Battery.*Discharging'
118
119
120
  }

  function battery_pct() {
121
    if (( $+commands[acpi] )); then
GregoireW's avatar
GregoireW committed
122
      acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -E '^Battery.*(Disc|C)harging' | cut -f2 -d ',' | tr -cd '[:digit:]'
123
    fi
124
125
  }

126
  function battery_pct_remaining() {
127
    if ! battery_is_charging; then
128
129
130
      battery_pct
    else
      echo "External Power"
131
132
133
134
    fi
  }

  function battery_time_remaining() {
135
    if ! battery_is_charging; then
GregoireW's avatar
GregoireW committed
136
      acpi 2>/dev/null | command grep -v "rate information unavailable" | cut -f3 -d ','
137
138
139
140
    fi
  }

  function battery_pct_prompt() {
141
142
    local battery_pct color
    battery_pct=$(battery_pct_remaining)
143
144
145
    if battery_is_charging; then
      echo "∞"
    else
146
      if [[ $battery_pct -gt 50 ]]; then
147
        color='green'
148
      elif [[ $battery_pct -gt 20 ]]; then
149
150
151
152
        color='yellow'
      else
        color='red'
      fi
153
      echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}"
154
155
    fi
  }
156

157
else
158
  # Empty functions so we don't cause errors in prompts
159
160
161
162
163
  function battery_is_charging { false }
  function battery_pct \
    battery_pct_remaining \
    battery_time_remaining \
    battery_pct_prompt { }
Peter Hoeg's avatar
Peter Hoeg committed
164
fi
165
166

function battery_level_gauge() {
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  local gauge_slots=${BATTERY_GAUGE_SLOTS:-10}
  local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))}
  local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.4 ))}
  local color_green=${BATTERY_COLOR_GREEN:-%F{green}}
  local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}}
  local color_red=${BATTERY_COLOR_RED:-%F{red}}
  local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}}
  local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['}
  local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'}
  local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'}
  local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'}
  local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow}
  local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'}

  local battery_remaining_percentage=$(battery_pct)
182
  local filled empty gauge_color
183
184

  if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
185
186
    filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 ))
    empty=$(( $gauge_slots - $filled ))
187

188
    if [[ $filled -gt $green_threshold ]]; then
189
      gauge_color=$color_green
190
    elif [[ $filled -gt $yellow_threshold ]]; then
191
      gauge_color=$color_yellow
192
    else
193
      gauge_color=$color_red
194
195
    fi
  else
196
197
198
    filled=$gauge_slots
    empty=0
    filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'}
199
200
  fi

201
  local charging=' '
202
  battery_is_charging && charging=$charging_symbol
203

204
  # Charging status and prefix
205
  printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%}
206
207
208
  # Filled slots
  [[ $filled -gt 0 ]] && printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
  # Empty slots
209
  [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
210
  # Suffix
211
212
  printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%}
}