battery.plugin.zsh 6.29 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
22
23
24
    local battery_status="$(ioreg -rc AppleSmartBattery)"
    local -i capacity=$(sed -n -e '/MaxCapacity/s/^.*"MaxCapacity"\ =\ //p' <<< $battery_status)
    local -i current=$(sed -n -e '/CurrentCapacity/s/^.*"CurrentCapacity"\ =\ //p' <<< $battery_status)
    echo $(( current * 100 / capacity ))
Ron Shapiro's avatar
Ron Shapiro committed
25
  }
26

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

function battery_level_gauge() {
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  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)
181
  local filled empty gauge_color
182
183

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

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

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

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