2012-12-02 19:36:33 +00:00
|
|
|
###########################################
|
|
|
|
# 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 #
|
|
|
|
###########################################
|
|
|
|
|
2014-04-23 14:13:26 +00:00
|
|
|
if [[ "$OSTYPE" = darwin* ]] ; then
|
2012-12-02 19:36:33 +00:00
|
|
|
|
2013-04-21 02:37:36 +00:00
|
|
|
function battery_pct() {
|
2014-01-06 01:09:15 +00:00
|
|
|
local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
|
|
|
|
typeset -F maxcapacity=$(echo $smart_battery_status | grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //')
|
|
|
|
typeset -F currentcapacity=$(echo $smart_battery_status | grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //')
|
2013-04-21 02:37:36 +00:00
|
|
|
integer i=$(((currentcapacity/maxcapacity) * 100))
|
|
|
|
echo $i
|
|
|
|
}
|
2013-04-29 14:18:32 +00:00
|
|
|
|
|
|
|
function plugged_in() {
|
|
|
|
[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ Yes') -eq 1 ]
|
|
|
|
}
|
2014-03-17 16:01:00 +00:00
|
|
|
|
2012-12-02 19:36:33 +00:00
|
|
|
function battery_pct_remaining() {
|
2013-04-29 14:18:32 +00:00
|
|
|
if plugged_in ; then
|
2012-12-02 19:36:33 +00:00
|
|
|
echo "External Power"
|
2013-04-29 14:18:32 +00:00
|
|
|
else
|
|
|
|
battery_pct
|
2011-10-20 06:12:13 +00:00
|
|
|
fi
|
|
|
|
}
|
2012-12-02 19:36:33 +00:00
|
|
|
|
|
|
|
function battery_time_remaining() {
|
2014-03-17 16:01:00 +00:00
|
|
|
local smart_battery_status="$(ioreg -rc "AppleSmartBattery")"
|
2014-01-06 01:09:15 +00:00
|
|
|
if [[ $(echo $smart_battery_status | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
|
|
|
|
timeremaining=$(echo $smart_battery_status | grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
|
2014-02-17 00:32:24 +00:00
|
|
|
if [ $timeremaining -gt 720 ] ; then
|
|
|
|
echo "::"
|
|
|
|
else
|
|
|
|
echo "~$((timeremaining / 60)):$((timeremaining % 60))"
|
|
|
|
fi
|
2012-12-02 19:36:33 +00:00
|
|
|
else
|
|
|
|
echo "∞"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function battery_pct_prompt () {
|
|
|
|
if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then
|
|
|
|
b=$(battery_pct_remaining)
|
|
|
|
if [ $b -gt 50 ] ; then
|
|
|
|
color='green'
|
|
|
|
elif [ $b -gt 20 ] ; then
|
|
|
|
color='yellow'
|
|
|
|
else
|
|
|
|
color='red'
|
|
|
|
fi
|
|
|
|
echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
|
|
|
|
else
|
2013-03-02 11:09:57 +00:00
|
|
|
echo "∞"
|
2012-12-02 19:36:33 +00:00
|
|
|
fi
|
|
|
|
}
|
2014-03-17 16:01:00 +00:00
|
|
|
|
2014-01-06 01:09:15 +00:00
|
|
|
function battery_is_charging() {
|
2014-03-17 16:01:00 +00:00
|
|
|
[[ $(ioreg -rc "AppleSmartBattery"| grep '^.*"IsCharging"\ =\ ' | sed -e 's/^.*"IsCharging"\ =\ //') == "Yes" ]]
|
2014-01-06 01:09:15 +00:00
|
|
|
}
|
2012-12-02 19:36:33 +00:00
|
|
|
|
2016-10-10 20:24:30 +00:00
|
|
|
elif [[ "$OSTYPE" = linux* ]] ; then
|
2012-12-02 19:36:33 +00:00
|
|
|
|
2014-03-02 17:47:35 +00:00
|
|
|
function battery_is_charging() {
|
2016-09-15 00:01:10 +00:00
|
|
|
! [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]]
|
2014-03-02 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function battery_pct() {
|
2014-03-17 16:01:00 +00:00
|
|
|
if (( $+commands[acpi] )) ; then
|
2016-09-15 00:01:10 +00:00
|
|
|
echo "$(acpi 2>/dev/null | cut -f2 -d ',' | tr -cd '[:digit:]')"
|
2014-03-17 16:01:00 +00:00
|
|
|
fi
|
2014-03-02 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
2013-03-02 11:09:57 +00:00
|
|
|
function battery_pct_remaining() {
|
2014-03-02 17:47:35 +00:00
|
|
|
if [ ! $(battery_is_charging) ] ; then
|
|
|
|
battery_pct
|
|
|
|
else
|
|
|
|
echo "External Power"
|
2013-03-02 11:09:57 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function battery_time_remaining() {
|
2016-09-15 00:01:10 +00:00
|
|
|
if [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
|
|
|
|
echo $(acpi 2>/dev/null | cut -f3 -d ',')
|
2013-03-02 11:09:57 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function battery_pct_prompt() {
|
|
|
|
b=$(battery_pct_remaining)
|
2016-09-15 00:01:10 +00:00
|
|
|
if [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then
|
2012-12-02 19:36:33 +00:00
|
|
|
if [ $b -gt 50 ] ; then
|
|
|
|
color='green'
|
|
|
|
elif [ $b -gt 20 ] ; then
|
|
|
|
color='yellow'
|
|
|
|
else
|
|
|
|
color='red'
|
|
|
|
fi
|
|
|
|
echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}"
|
2013-03-02 11:09:57 +00:00
|
|
|
else
|
|
|
|
echo "∞"
|
|
|
|
fi
|
|
|
|
}
|
2014-03-17 16:01:00 +00:00
|
|
|
|
2013-11-05 02:00:11 +00:00
|
|
|
else
|
2014-01-06 01:09:15 +00:00
|
|
|
# Empty functions so we don't cause errors in prompts
|
|
|
|
function battery_pct_remaining() {
|
|
|
|
}
|
2013-11-05 02:00:11 +00:00
|
|
|
|
2014-01-06 01:09:15 +00:00
|
|
|
function battery_time_remaining() {
|
|
|
|
}
|
2013-11-05 02:00:11 +00:00
|
|
|
|
2014-01-06 01:09:15 +00:00
|
|
|
function battery_pct_prompt() {
|
|
|
|
}
|
2011-10-20 06:12:13 +00:00
|
|
|
fi
|
2014-01-06 01:09:15 +00:00
|
|
|
|
|
|
|
function battery_level_gauge() {
|
|
|
|
local gauge_slots=${BATTERY_GAUGE_SLOTS:-10};
|
|
|
|
local green_threshold=${BATTERY_GREEN_THRESHOLD:-6};
|
|
|
|
local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-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);
|
|
|
|
|
|
|
|
if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then
|
|
|
|
local filled=$(((( $battery_remaining_percentage + $gauge_slots - 1) / $gauge_slots)));
|
|
|
|
local empty=$(($gauge_slots - $filled));
|
2014-03-17 16:01:00 +00:00
|
|
|
|
2014-01-06 01:09:15 +00:00
|
|
|
if [[ $filled -gt $green_threshold ]]; then local gauge_color=$color_green;
|
|
|
|
elif [[ $filled -gt $yellow_threshold ]]; then local gauge_color=$color_yellow;
|
|
|
|
else local gauge_color=$color_red;
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
local filled=$gauge_slots;
|
|
|
|
local empty=0;
|
2014-03-17 16:01:00 +00:00
|
|
|
filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'};
|
2014-01-06 01:09:15 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
local charging=' ' && battery_is_charging && charging=$charging_symbol;
|
|
|
|
|
|
|
|
printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%}
|
|
|
|
printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled}
|
|
|
|
[[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty}
|
|
|
|
printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%}
|
|
|
|
}
|
|
|
|
|
|
|
|
|