battery.plugin.zsh 6.12 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
24
  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"\ =\ //')
    echo $(( (currentcapacity/maxcapacity) * 100 ))
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
    if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then
51
      b=$(battery_pct_remaining)
52
      if [[ $b -gt 50 ]]; then
53
        color='green'
54
      elif [[ $b -gt 20 ]]; then
55
56
57
58
59
60
        color='yellow'
      else
        color='red'
      fi
      echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
    else
61
      echo "∞"
62
63
    fi
  }
64

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

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

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

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

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

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

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

114
  function battery_is_charging() {
115
    ! acpi 2>/dev/null | command grep -q '^Battery.*Discharging'
116
117
118
  }

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

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

  function battery_time_remaining() {
133
134
    if ! battery_is_charging; then
      acpi 2>/dev/null | cut -f3 -d ','
135
136
137
138
    fi
  }

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

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

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

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

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

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

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