2022-01-15 19:00:35 +09:00
|
|
|
#! bash oh-my-bash.module
|
2017-10-10 18:07:01 +07:00
|
|
|
|
2020-02-17 03:46:33 +01:00
|
|
|
# Bug: https://github.com/ohmybash/oh-my-bash/issues/68
|
|
|
|
|
if ! command -v "command_exists" >/dev/null; then command_exists() { command -v "$1" >/dev/null ; } fi
|
|
|
|
|
|
2022-01-08 14:13:07 +09:00
|
|
|
_omb_plugin_battery__upower_print_info() {
|
|
|
|
|
upower -i "$(upower -e | sed -n '/BAT/{p;q;}')"
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 18:07:01 +07:00
|
|
|
ac_adapter_connected(){
|
2020-02-17 03:46:33 +01:00
|
|
|
if command_exists upower;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
2022-01-08 14:13:07 +09:00
|
|
|
_omb_plugin_battery__upower_print_info | grep -qE 'state[:[:blank:]]*(charging|fully-charged)'
|
2017-10-10 18:07:01 +07:00
|
|
|
return $?
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists acpi;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
acpi -a | grep -q "on-line"
|
|
|
|
|
return $?
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists pmset;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
pmset -g batt | grep -q 'AC Power'
|
|
|
|
|
return $?
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists ioreg;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes'
|
|
|
|
|
return $?
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists WMIC;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2'
|
|
|
|
|
return $?
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ac_adapter_disconnected(){
|
2020-02-17 03:46:33 +01:00
|
|
|
if command_exists upower;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
2022-01-08 14:13:07 +09:00
|
|
|
_omb_plugin_battery__upower_print_info | grep -qE 'state[:[:blank:]]*discharging'
|
2017-10-10 18:07:01 +07:00
|
|
|
return $?
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists acpi;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
acpi -a | grep -q "off-line"
|
|
|
|
|
return $?
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists pmset;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
pmset -g batt | grep -q 'Battery Power'
|
|
|
|
|
return $?
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists ioreg;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No'
|
|
|
|
|
return $?
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists WMIC;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1'
|
|
|
|
|
return $?
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 13:51:41 +09:00
|
|
|
## @fn battery_percentage
|
|
|
|
|
## @about 'displays battery charge as a percentage of full (100%)'
|
|
|
|
|
## @group 'battery'
|
2017-10-10 18:07:01 +07:00
|
|
|
battery_percentage(){
|
2020-02-17 03:46:33 +01:00
|
|
|
if command_exists upower;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
2022-01-08 14:14:05 +09:00
|
|
|
local UPOWER_OUTPUT=$(_omb_plugin_battery__upower_print_info | sed -n 's/.*percentage[:[:blank:]]*\([0-9%]\{1,\}\)$/\1/p')
|
2022-01-08 14:34:22 +09:00
|
|
|
[[ $UPOWER_OUTPUT ]] &&
|
|
|
|
|
echo "${UPOWER_OUTPUT::-1}"
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists acpi;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
local ACPI_OUTPUT=$(acpi -b)
|
|
|
|
|
case $ACPI_OUTPUT in
|
|
|
|
|
*" Unknown"*)
|
|
|
|
|
local PERC_OUTPUT=$(echo $ACPI_OUTPUT | head -c 22 | tail -c 2)
|
|
|
|
|
case $PERC_OUTPUT in
|
|
|
|
|
*%)
|
|
|
|
|
echo "0${PERC_OUTPUT}" | head -c 2
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo ${PERC_OUTPUT}
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
*" Charging"* | *" Discharging"*)
|
|
|
|
|
local PERC_OUTPUT=$(echo $ACPI_OUTPUT | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' )
|
|
|
|
|
echo ${PERC_OUTPUT}
|
|
|
|
|
;;
|
|
|
|
|
*" Full"*)
|
|
|
|
|
echo '100'
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo '-1'
|
|
|
|
|
;;
|
|
|
|
|
esac
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists pmset;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
local PMSET_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p')
|
|
|
|
|
case $PMSET_OUTPUT in
|
|
|
|
|
100*)
|
|
|
|
|
echo '100'
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo $PMSET_OUTPUT | head -c 2
|
|
|
|
|
;;
|
|
|
|
|
esac
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists ioreg;
|
2017-10-10 18:07:01 +07:00
|
|
|
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: "?")}')
|
|
|
|
|
case $IOREG_OUTPUT in
|
|
|
|
|
100*)
|
|
|
|
|
echo '100'
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo $IOREG_OUTPUT | head -c 2
|
|
|
|
|
;;
|
|
|
|
|
esac
|
2020-02-17 03:46:33 +01:00
|
|
|
elif command_exists WMIC;
|
2017-10-10 18:07:01 +07:00
|
|
|
then
|
|
|
|
|
local WINPC=$(echo porcent=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List) | grep -o '[0-9]*')
|
|
|
|
|
case $WINPC in
|
|
|
|
|
100*)
|
|
|
|
|
echo '100'
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo $WINPC
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
else
|
|
|
|
|
echo "no"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 13:51:41 +09:00
|
|
|
## @fn battery_charge
|
|
|
|
|
## @about 'graphical display of your battery charge'
|
|
|
|
|
## @group 'battery'
|
2017-10-10 18:07:01 +07:00
|
|
|
battery_charge(){
|
|
|
|
|
# Full char
|
|
|
|
|
local F_C='▸'
|
|
|
|
|
# Depleted char
|
|
|
|
|
local D_C='▹'
|
2022-01-19 16:52:15 +09:00
|
|
|
local DEPLETED_COLOR="${_omb_prompt_normal}"
|
lib, themes: Resolve conflicting variables
"lib" has used the variables "red", "green", "yellow", "blue", and
"purple" for outputting logs to stdout/stderr. On the other hand,
"themes" has used the same variables for including escape sequences in
PS1. These two sets of variables have different values, i.e., the
latter is enclosed by '\[' and '\]' and contains escaped '\e', which
means that there have been variable conflicts. In this commit, more
specific variables are used in these places to resolve the conflicts.
2022-01-19 17:23:35 +09:00
|
|
|
local FULL_COLOR="${_omb_prompt_green}"
|
2022-01-24 14:55:36 +09:00
|
|
|
local HALF_COLOR="${_omb_prompt_olive}"
|
|
|
|
|
local DANGER_COLOR="${_omb_prompt_brown}"
|
2017-10-10 18:07:01 +07:00
|
|
|
local BATTERY_OUTPUT="${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${D_C}"
|
|
|
|
|
local BATTERY_PERC=$(battery_percentage)
|
|
|
|
|
|
|
|
|
|
case $BATTERY_PERC in
|
|
|
|
|
no)
|
|
|
|
|
echo ""
|
|
|
|
|
;;
|
|
|
|
|
9*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${F_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
8*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${HALF_COLOR}${F_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
7*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
6*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
5*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
4*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${FULL_COLOR}${F_C}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
3*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${FULL_COLOR}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
2*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${FULL_COLOR}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
1*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${FULL_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
05)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
04)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
03)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
02)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
0*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
*)
|
2022-01-19 16:52:15 +09:00
|
|
|
echo "${DANGER_COLOR}UNPLG${_omb_prompt_normal}"
|
2017-10-10 18:07:01 +07:00
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
}
|