]> git.rmz.io Git - dotfiles.git/blob - zsh/plugins/battery/battery.plugin.zsh
update zshrc
[dotfiles.git] / zsh / plugins / battery / battery.plugin.zsh
1 ###########################################
2 # Battery plugin for oh-my-zsh #
3 # Original Author: Peter hoeg (peterhoeg) #
4 # Email: peter@speartail.com #
5 ###########################################
6 # Author: Sean Jones (neuralsandwich) #
7 # Email: neuralsandwich@gmail.com #
8 # Modified to add support for Apple Mac #
9 ###########################################
10
11 if [[ $(uname) == "Darwin" ]] ; then
12
13 function battery_pct_remaining() {
14 if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
15 typeset -F maxcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
16 typeset -F currentcapacity=$(ioreg -rc "AppleSmartBattery"| grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
17 integer i=$(((currentcapacity/maxcapacity) * 100))
18 echo $i
19 else
20 echo "External Power"
21 fi
22 }
23
24 function battery_time_remaining() {
25 if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
26 timeremaining=$(ioreg -rc "AppleSmartBattery"| grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
27 echo "~$((timeremaining / 60)):$((timeremaining % 60))"
28 else
29 echo "∞"
30 fi
31 }
32
33 function battery_pct_prompt () {
34 if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
35 b=$(battery_pct_remaining)
36 if [ $b -gt 50 ] ; then
37 color='green'
38 elif [ $b -gt 20 ] ; then
39 color='yellow'
40 else
41 color='red'
42 fi
43 echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
44 else
45 echo ""
46 fi
47 }
48
49 elif [[ $(uname) == "Linux" ]] ; then
50
51 if [[ $(acpi 2&>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
52 function battery_pct_remaining() { echo "$(acpi | cut -f2 -d ',' | tr -cd '[:digit:]')" }
53 function battery_time_remaining() { echo $(acpi | cut -f3 -d ',') }
54 function battery_pct_prompt() {
55 b=$(battery_pct_remaining)
56 if [ $b -gt 50 ] ; then
57 color='green'
58 elif [ $b -gt 20 ] ; then
59 color='yellow'
60 else
61 color='red'
62 fi
63 echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
64 }
65 else
66 error_msg='no battery'
67 function battery_pct_remaining() { echo $error_msg }
68 function battery_time_remaining() { echo $error_msg }
69 function battery_pct_prompt() { echo '' }
70 fi
71 fi