blob: 002d42f2d4d8409135bbd19c3cfca95447e4106b [file] [log] [blame]
Erich Smith3f8fe612012-05-09 09:27:10 -04001cite about-plugin
Erich Smith08e439c2012-05-13 08:37:31 -04002about-plugin 'display info about your battery charge level'
Erich Smith3f8fe612012-05-09 09:27:10 -04003
Jesus de Mula Cano8188b912011-03-06 18:12:24 +01004battery_percentage(){
Erich Smith3f8fe612012-05-09 09:27:10 -04005 about 'displays battery charge as a percentage of full (100%)'
Erich Smith08e439c2012-05-13 08:37:31 -04006 group 'battery'
Erich Smith3f8fe612012-05-09 09:27:10 -04007
Jesus de Mula Cano1080af32011-03-07 00:05:54 +01008 if command_exists acpi;
9 then
10 local ACPI_OUTPUT=$(acpi -b)
11 case $ACPI_OUTPUT in
Erich Smith3f8fe612012-05-09 09:27:10 -040012 *" Unknown"*)
Jesus de Mula Cano7ae29522011-03-07 17:06:31 +010013 local PERC_OUTPUT=$(echo $ACPI_OUTPUT | head -c 22 | tail -c 2)
14 case $PERC_OUTPUT in
15 *%)
16 echo "0${PERC_OUTPUT}" | head -c 2
17 ;;
18 *)
19 echo ${PERC_OUTPUT}
20 ;;
21 esac
Jesus de Mula Cano1080af32011-03-07 00:05:54 +010022 ;;
Erich Smith3f8fe612012-05-09 09:27:10 -040023 *" Discharging"*)
Jesus de Mula Cano7ae29522011-03-07 17:06:31 +010024 local PERC_OUTPUT=$(echo $ACPI_OUTPUT | head -c 26 | tail -c 2)
25 case $PERC_OUTPUT in
26 *%)
27 echo "0${PERC_OUTPUT}" | head -c 2
28 ;;
29 *)
30 echo ${PERC_OUTPUT}
31 ;;
32 esac
Jesus de Mula Cano1080af32011-03-07 00:05:54 +010033 ;;
Erich Smith3f8fe612012-05-09 09:27:10 -040034 *" Charging"*)
Jesus de Mula Cano7ae29522011-03-07 17:06:31 +010035 local PERC_OUTPUT=$(echo $ACPI_OUTPUT | head -c 23 | tail -c 2)
36 case $PERC_OUTPUT in
37 *%)
38 echo "0${PERC_OUTPUT}" | head -c 2
39 ;;
40 *)
41 echo ${PERC_OUTPUT}
42 ;;
43 esac
Jesus de Mula Cano1080af32011-03-07 00:05:54 +010044 ;;
Erich Smith3f8fe612012-05-09 09:27:10 -040045 *" Full"*)
Jesus de Mula Cano1080af32011-03-07 00:05:54 +010046 echo '99'
47 ;;
48 *)
49 echo '-1'
50 ;;
51 esac
Jesus de Mula Canof98a7282011-03-07 01:11:20 +010052 elif command_exists ioreg;
53 then
54 # http://hints.macworld.com/article.php?story=20100130123935998
Jesus de Mula Canob79fc5d2011-03-07 01:26:24 +010055 #local IOREG_OUTPUT_10_6=$(ioreg -l | grep -i capacity | tr '\n' ' | ' | awk '{printf("%.2f%%", $10/$5 * 100)}')
56 #local IOREG_OUTPUT_10_5=$(ioreg -l | grep -i capacity | grep -v Legacy| tr '\n' ' | ' | awk '{printf("%.2f%%", $14/$7 * 100)}')
57 local IOREG_OUTPUT=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%.2f%%"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}')
58 case $IOREG_OUTPUT in
Erich Smith3f8fe612012-05-09 09:27:10 -040059 100*)
Jesus de Mula Canob79fc5d2011-03-07 01:26:24 +010060 echo '99'
61 ;;
62 *)
63 echo $IOREG_OUTPUT | head -c 2
64 ;;
65 esac
Jesus de Mula Cano1080af32011-03-07 00:05:54 +010066 else
67 echo "no"
68 fi
Jesus de Mula Cano8188b912011-03-06 18:12:24 +010069}
70
71battery_charge(){
Erich Smith08e439c2012-05-13 08:37:31 -040072 about 'graphical display of your battery charge'
73 group 'battery'
Erich Smith3f8fe612012-05-09 09:27:10 -040074
Jesus de Mula Cano8188b912011-03-06 18:12:24 +010075 # Full char
76 local F_C='▸'
77 # Depleted char
78 local D_C='▹'
JFSIII1b7c9412011-06-17 19:45:21 -040079 local DEPLETED_COLOR="${normal}"
80 local FULL_COLOR="${green}"
81 local HALF_COLOR="${yellow}"
82 local DANGER_COLOR="${red}"
83 local BATTERY_OUTPUT="${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${D_C}"
Jesus de Mula Cano8188b912011-03-06 18:12:24 +010084 local BATTERY_PERC=$(battery_percentage)
85
86 case $BATTERY_PERC in
Jesus de Mula Cano1080af32011-03-07 00:05:54 +010087 no)
88 echo ""
89 ;;
Jesus de Mula Cano8188b912011-03-06 18:12:24 +010090 9*)
91 echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${F_C}${normal}"
92 ;;
93 8*)
94 echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${HALF_COLOR}${F_C}${normal}"
95 ;;
96 7*)
97 echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${normal}"
98 ;;
99 6*)
100 echo "${FULL_COLOR}${F_C}${F_C}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${normal}"
101 ;;
102 5*)
103 echo "${FULL_COLOR}${F_C}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${normal}"
104 ;;
105 4*)
106 echo "${FULL_COLOR}${F_C}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${normal}"
107 ;;
108 3*)
109 echo "${FULL_COLOR}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${normal}"
110 ;;
111 2*)
112 echo "${FULL_COLOR}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${normal}"
113 ;;
114 1*)
115 echo "${FULL_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
116 ;;
117 05)
118 echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
119 ;;
120 04)
121 echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
122 ;;
123 03)
124 echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
125 ;;
126 02)
127 echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
128 ;;
129 0*)
130 echo "${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
131 ;;
132 *)
JFSIII1b7c9412011-06-17 19:45:21 -0400133 echo "${DANGER_COLOR}UNPLG${normal}"
Jesus de Mula Cano8188b912011-03-06 18:12:24 +0100134 ;;
135 esac
Jesus de Mula Cano8188b912011-03-06 18:12:24 +0100136}