mirror of
https://github.com/ohmybash/oh-my-bash.git
synced 2024-05-11 05:55:37 +00:00
battery.plugin: Refactor
This commit is contained in:
@@ -4,55 +4,41 @@ function _omb_plugin_battery__upower_print_info {
|
|||||||
upower -i "$(upower -e | sed -n '/BAT/{p;q;}')"
|
upower -i "$(upower -e | sed -n '/BAT/{p;q;}')"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## @fn ac_adapter_connected
|
||||||
|
## @exit 0 if the adapter is disconnected, or non-zero exit status
|
||||||
|
## otherwise.
|
||||||
function ac_adapter_connected {
|
function ac_adapter_connected {
|
||||||
if _omb_util_command_exists upower;
|
if _omb_util_command_exists upower; then
|
||||||
then
|
|
||||||
_omb_plugin_battery__upower_print_info | grep -qE 'state[:[:blank:]]*(charging|fully-charged)'
|
_omb_plugin_battery__upower_print_info | grep -qE 'state[:[:blank:]]*(charging|fully-charged)'
|
||||||
return $?
|
elif _omb_util_command_exists acpi; then
|
||||||
elif _omb_util_command_exists acpi;
|
|
||||||
then
|
|
||||||
acpi -a | grep -q "on-line"
|
acpi -a | grep -q "on-line"
|
||||||
return $?
|
elif _omb_util_command_exists pmset; then
|
||||||
elif _omb_util_command_exists pmset;
|
|
||||||
then
|
|
||||||
pmset -g batt | grep -q 'AC Power'
|
pmset -g batt | grep -q 'AC Power'
|
||||||
return $?
|
elif _omb_util_command_exists ioreg; then
|
||||||
elif _omb_util_command_exists ioreg;
|
|
||||||
then
|
|
||||||
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes'
|
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes'
|
||||||
return $?
|
elif _omb_util_command_exists WMIC; then
|
||||||
elif _omb_util_command_exists WMIC;
|
|
||||||
then
|
|
||||||
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2'
|
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2'
|
||||||
return $?
|
|
||||||
elif [[ -r /sys/class/power_supply/ADP0/online ]]; then
|
elif [[ -r /sys/class/power_supply/ADP0/online ]]; then
|
||||||
[[ $(cat /sys/class/power_supply/ADP0/online) == 1 ]]
|
[[ $(< /sys/class/power_supply/ADP0/online) == 1 ]]
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## @fn ac_adapter_disconnected
|
||||||
|
## @exit 0 if the adapter is disconnected, or non-zero exit status
|
||||||
|
## otherwise.
|
||||||
function ac_adapter_disconnected {
|
function ac_adapter_disconnected {
|
||||||
if _omb_util_command_exists upower;
|
if _omb_util_command_exists upower; then
|
||||||
then
|
|
||||||
_omb_plugin_battery__upower_print_info | grep -qE 'state[:[:blank:]]*discharging'
|
_omb_plugin_battery__upower_print_info | grep -qE 'state[:[:blank:]]*discharging'
|
||||||
return $?
|
elif _omb_util_command_exists acpi; then
|
||||||
elif _omb_util_command_exists acpi;
|
|
||||||
then
|
|
||||||
acpi -a | grep -q "off-line"
|
acpi -a | grep -q "off-line"
|
||||||
return $?
|
elif _omb_util_command_exists pmset; then
|
||||||
elif _omb_util_command_exists pmset;
|
|
||||||
then
|
|
||||||
pmset -g batt | grep -q 'Battery Power'
|
pmset -g batt | grep -q 'Battery Power'
|
||||||
return $?
|
elif _omb_util_command_exists ioreg; then
|
||||||
elif _omb_util_command_exists ioreg;
|
|
||||||
then
|
|
||||||
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No'
|
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No'
|
||||||
return $?
|
elif _omb_util_command_exists WMIC; then
|
||||||
elif _omb_util_command_exists WMIC;
|
|
||||||
then
|
|
||||||
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1'
|
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1'
|
||||||
return $?
|
|
||||||
elif [[ -r /sys/class/power_supply/ADP0/online ]]; then
|
elif [[ -r /sys/class/power_supply/ADP0/online ]]; then
|
||||||
[[ $(cat /sys/class/power_supply/ADP0/online) == 0 ]]
|
[[ $(< /sys/class/power_supply/ADP0/online) == 0 ]]
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,13 +46,11 @@ function ac_adapter_disconnected {
|
|||||||
## @about 'displays battery charge as a percentage of full (100%)'
|
## @about 'displays battery charge as a percentage of full (100%)'
|
||||||
## @group 'battery'
|
## @group 'battery'
|
||||||
function battery_percentage {
|
function battery_percentage {
|
||||||
if _omb_util_command_exists upower;
|
if _omb_util_command_exists upower; then
|
||||||
then
|
|
||||||
local UPOWER_OUTPUT=$(_omb_plugin_battery__upower_print_info | sed -n 's/.*percentage[:[:blank:]]*\([0-9%]\{1,\}\)$/\1/p')
|
local UPOWER_OUTPUT=$(_omb_plugin_battery__upower_print_info | sed -n 's/.*percentage[:[:blank:]]*\([0-9%]\{1,\}\)$/\1/p')
|
||||||
[[ $UPOWER_OUTPUT ]] &&
|
[[ $UPOWER_OUTPUT ]] &&
|
||||||
echo "${UPOWER_OUTPUT::-1}"
|
echo "${UPOWER_OUTPUT::-1}"
|
||||||
elif _omb_util_command_exists acpi;
|
elif _omb_util_command_exists acpi; then
|
||||||
then
|
|
||||||
local ACPI_OUTPUT=$(acpi -b)
|
local ACPI_OUTPUT=$(acpi -b)
|
||||||
case $ACPI_OUTPUT in
|
case $ACPI_OUTPUT in
|
||||||
*" Unknown"*)
|
*" Unknown"*)
|
||||||
@@ -92,8 +76,7 @@ function battery_percentage {
|
|||||||
echo '-1'
|
echo '-1'
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
elif _omb_util_command_exists pmset;
|
elif _omb_util_command_exists pmset; then
|
||||||
then
|
|
||||||
local PMSET_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p')
|
local PMSET_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p')
|
||||||
case $PMSET_OUTPUT in
|
case $PMSET_OUTPUT in
|
||||||
100*)
|
100*)
|
||||||
@@ -103,8 +86,7 @@ function battery_percentage {
|
|||||||
echo $PMSET_OUTPUT | head -c 2
|
echo $PMSET_OUTPUT | head -c 2
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
elif _omb_util_command_exists ioreg;
|
elif _omb_util_command_exists ioreg; then
|
||||||
then
|
|
||||||
local IOREG_OUTPUT=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%05.2f%%"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}')
|
local IOREG_OUTPUT=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%05.2f%%"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}')
|
||||||
case $IOREG_OUTPUT in
|
case $IOREG_OUTPUT in
|
||||||
100*)
|
100*)
|
||||||
@@ -114,8 +96,7 @@ function battery_percentage {
|
|||||||
echo $IOREG_OUTPUT | head -c 2
|
echo $IOREG_OUTPUT | head -c 2
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
elif _omb_util_command_exists WMIC;
|
elif _omb_util_command_exists WMIC; then
|
||||||
then
|
|
||||||
local WINPC=$(echo porcent=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List) | grep -o '[0-9]*')
|
local WINPC=$(echo porcent=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List) | grep -o '[0-9]*')
|
||||||
case $WINPC in
|
case $WINPC in
|
||||||
100*)
|
100*)
|
||||||
@@ -126,7 +107,7 @@ function battery_percentage {
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
elif [[ -r /sys/class/power_supply/BAT0/capacity ]]; then
|
elif [[ -r /sys/class/power_supply/BAT0/capacity ]]; then
|
||||||
echo "$(cat /sys/class/power_supply/BAT0/capacity)"
|
echo "$(< /sys/class/power_supply/BAT0/capacity)"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user