Enable battery on Mac OS X

This uses ioreg to read the battery status.  This was tested on
Mac OS 10.8.3, but will probably work a few versions back as well.
This commit is contained in:
George Macon 2013-05-14 19:04:26 -04:00 committed by Olivier Mengué
parent 75afb57855
commit d66cafb86a

View File

@ -356,7 +356,10 @@ unset _lp_source_config
[[ "$LP_ENABLE_FOSSIL" = 1 ]] && { command -v fossil >/dev/null || LP_ENABLE_FOSSIL=0 ; } [[ "$LP_ENABLE_FOSSIL" = 1 ]] && { command -v fossil >/dev/null || LP_ENABLE_FOSSIL=0 ; }
[[ "$LP_ENABLE_HG" = 1 ]] && { command -v hg >/dev/null || LP_ENABLE_HG=0 ; } [[ "$LP_ENABLE_HG" = 1 ]] && { command -v hg >/dev/null || LP_ENABLE_HG=0 ; }
[[ "$LP_ENABLE_BZR" = 1 ]] && { command -v bzr > /dev/null || LP_ENABLE_BZR=0 ; } [[ "$LP_ENABLE_BZR" = 1 ]] && { command -v bzr > /dev/null || LP_ENABLE_BZR=0 ; }
[[ "$LP_ENABLE_BATT" = 1 ]] && { command -v acpi >/dev/null || LP_ENABLE_BATT=0 ; } case "$LP_OS" in
Darwin) [[ "$LP_ENABLE_BATT" = 1 ]] && { command -v ioreg >/dev/null || LP_ENABLE_BATT=0 ; };;
*) [[ "$LP_ENABLE_BATT" = 1 ]] && { command -v acpi >/dev/null || LP_ENABLE_BATT=0 ; };;
esac
# If we are running in a terminal multiplexer, brackets are colored # If we are running in a terminal multiplexer, brackets are colored
if [[ "$TERM" == screen* ]]; then if [[ "$TERM" == screen* ]]; then
@ -1106,45 +1109,89 @@ _lp_bzr_branch_color()
# returns 2 (and battery level) if battery is charging but under threshold # returns 2 (and battery level) if battery is charging but under threshold
# returns 3 (and battery level) if battery is charging and above threshold # returns 3 (and battery level) if battery is charging and above threshold
# returns 4 if no battery support # returns 4 if no battery support
_lp_battery() case "$LP_OS" in
{ Linux)
[[ "$LP_ENABLE_BATT" != 1 ]] && return 4 _lp_battery()
local acpi {
acpi="$(acpi --battery 2>/dev/null)" [[ "$LP_ENABLE_BATT" != 1 ]] && return
# Extract the battery load value in percent local acpi
# First, remove the beginning of the line... acpi="$(acpi --battery 2>/dev/null)"
local bat="${acpi#Battery *, }" # Extract the battery load value in percent
bat="${bat%%%*}" # remove everything starting at '%' # First, remove the beginning of the line...
local bat="${acpi#Battery *, }"
bat="${bat%%%*}" # remove everything starting at '%'
if [[ -z "${bat}" ]] ; then if [[ -z "${bat}" ]] ; then
# not battery level found # not battery level found
return 4 return 4
# discharging # discharging
elif [[ "$acpi" == *"Discharging"* ]] ; then elif [[ "$acpi" == *"Discharging"* ]] ; then
if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then
# under threshold # under threshold
echo -n "${bat}" echo -n "${bat}"
return 0 return 0
else
# above threshold
echo -n "${bat}"
return 1
fi
# charging
else else
# above threshold if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then
echo -n "${bat}" # under threshold
return 1 echo -n "${bat}"
return 2
else
# above threshold
echo -n "${bat}"
return 3
fi
fi fi
}
# charging ;;
else Darwin)
if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then _lp_battery()
# under threshold {
echo -n "${bat}" [[ "$LP_ENABLE_BATT" != 1 ]] && return
return 2 local ioreg="$(ioreg -rn AppleSmartBattery | grep -v LegacyBatteryInfo)"
[[ -z "$ioreg" ]] && return 4
local ac=$(awk -F= '/ExternalConnected/ {print (tolower($2) ~ /yes/ ? 1 : 0)}' <<< "$ioreg")
local charge=$(awk -F= '/IsCharging/ {print (tolower($2) ~ /yes/ ? 1 : 0)}' <<< "$ioreg")
local max_charge=$(awk -F= '/MaxCapacity/ {print 0+$2}' <<< "$ioreg")
local current_charge=$(awk -F= '/CurrentCapacity/ {print 0+$2}' <<< "$ioreg")
local bat=$((100 * $current_charge / $max_charge))
if (($ac)) ; then
if (($charge)) ; then
# charging
echo -n "${bat}"
if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then
return 2
else
return 3
fi
else
# fully charged
# This is tested as a separate case because the Mac battery
# charger acts as if the battery is charged even when it
# is only _almost_ charged when AC power is applied.
# I don't have exact numbers, but the threshold for this
# behavior appears to be around 95%.
return 4
fi
else else
# above threshold # discharging
echo -n "${bat}" echo -n "${bat}"
return 3 if [[ ${bat} -le $LP_BATTERY_THRESHOLD ]] ; then
return 0
else
return 1
fi
fi fi
fi }
} ;;
esac
# Compute a gradient of background/foreground colors depending on the battery status # Compute a gradient of background/foreground colors depending on the battery status
# Display: # Display: