Refactor "{ => _omb_util_}command_exists"

https://github.com/ohmybash/oh-my-bash/pull/239#issuecomment-1007886245

Co-authored-by: Koichi Murase <myoga.murase@gmail.com>
This commit is contained in:
M
2022-01-23 12:29:59 -08:00
committed by Koichi Murase
parent b893ce6f79
commit de66a70f91
2 changed files with 15 additions and 24 deletions

View File

@@ -272,12 +272,6 @@ bigfind() {
fi fi
} }
# command_exists: checks for existence of a command (0 = true, 1 = false)
# -------------------------------------------------------------------
command_exists () {
type "$1" &> /dev/null ;
}
# pickfrom: picks random line from file # pickfrom: picks random line from file
# ------------------------------------------------------------------- # -------------------------------------------------------------------
pickfrom () { pickfrom () {

View File

@@ -1,30 +1,27 @@
#! bash oh-my-bash.module #! bash oh-my-bash.module
# 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
_omb_plugin_battery__upower_print_info() { _omb_plugin_battery__upower_print_info() {
upower -i "$(upower -e | sed -n '/BAT/{p;q;}')" upower -i "$(upower -e | sed -n '/BAT/{p;q;}')"
} }
ac_adapter_connected(){ ac_adapter_connected(){
if 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 $? return $?
elif command_exists acpi; elif _omb_util_command_exists acpi;
then then
acpi -a | grep -q "on-line" acpi -a | grep -q "on-line"
return $? return $?
elif command_exists pmset; elif _omb_util_command_exists pmset;
then then
pmset -g batt | grep -q 'AC Power' pmset -g batt | grep -q 'AC Power'
return $? return $?
elif command_exists ioreg; elif _omb_util_command_exists ioreg;
then then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes' ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes'
return $? return $?
elif command_exists WMIC; elif _omb_util_command_exists WMIC;
then 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 $? return $?
@@ -32,23 +29,23 @@ ac_adapter_connected(){
} }
ac_adapter_disconnected(){ ac_adapter_disconnected(){
if 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 $? return $?
elif command_exists acpi; elif _omb_util_command_exists acpi;
then then
acpi -a | grep -q "off-line" acpi -a | grep -q "off-line"
return $? return $?
elif command_exists pmset; elif _omb_util_command_exists pmset;
then then
pmset -g batt | grep -q 'Battery Power' pmset -g batt | grep -q 'Battery Power'
return $? return $?
elif command_exists ioreg; elif _omb_util_command_exists ioreg;
then then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No' ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No'
return $? return $?
elif command_exists WMIC; elif _omb_util_command_exists WMIC;
then 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 $? return $?
@@ -59,12 +56,12 @@ 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'
battery_percentage(){ battery_percentage(){
if 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 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
@@ -91,7 +88,7 @@ battery_percentage(){
echo '-1' echo '-1'
;; ;;
esac esac
elif 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
@@ -102,7 +99,7 @@ battery_percentage(){
echo $PMSET_OUTPUT | head -c 2 echo $PMSET_OUTPUT | head -c 2
;; ;;
esac esac
elif 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
@@ -113,7 +110,7 @@ battery_percentage(){
echo $IOREG_OUTPUT | head -c 2 echo $IOREG_OUTPUT | head -c 2
;; ;;
esac esac
elif 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