battery.plugin.zsh 6 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
  function battery_pct() {
21
    pmset -g batt | grep -Eo "\d+%" | cut -d% -f1
Ron Shapiro's avatar
Ron Shapiro committed
22
  }
23

24
  function battery_pct_remaining() {
25
    if battery_is_charging; then
26
      echo "External Power"
Ron Shapiro's avatar
Ron Shapiro committed
27
28
    else
      battery_pct
Peter Hoeg's avatar
Peter Hoeg committed
29
30
    fi
  }
31
32

  function battery_time_remaining() {
33
    local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
34
35
36
    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
37
38
39
40
        echo "::"
      else
        echo "~$((timeremaining / 60)):$((timeremaining % 60))"
      fi
41
42
43
44
45
46
    else
      echo "∞"
    fi
  }

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

63
elif [[ "$OSTYPE" = freebsd* ]]; then
64
65
66
67
68
69

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

  function battery_pct() {
70
71
    if (( $+commands[sysctl] )); then
      sysctl -n hw.acpi.battery.life
72
73
74
75
    fi
  }

  function battery_pct_remaining() {
76
    if ! battery_is_charging; then
77
78
79
80
81
82
83
      battery_pct
    else
      echo "External Power"
    fi
  }

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

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

110
elif [[ "$OSTYPE" = linux*  ]]; then
111

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

  function battery_pct() {
117
    if (( $+commands[acpi] )); then
118
      acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -E '^Battery.*(Full|(Disc|C)harging)' | cut -f2 -d ',' | tr -cd '[:digit:]'
119
    fi
120
121
  }

122
  function battery_pct_remaining() {
123
    if ! battery_is_charging; then
124
125
126
      battery_pct
    else
      echo "External Power"
127
128
129
130
    fi
  }

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

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

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

function battery_level_gauge() {
163
164
165
166
167
168
169
170
171
172
173
174
  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}
175
  local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'}
176
177

  local battery_remaining_percentage=$(battery_pct)
178
  local filled empty gauge_color
179
180

  if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
181
182
    filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 ))
    empty=$(( $gauge_slots - $filled ))
183

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

197
  local charging=' '
198
  battery_is_charging && charging=$charging_symbol
199

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