Copied command_exists function to the helpers lib so that there is no hidden dependency on the base plugin

The original command_exists function will stay in the base plugin, but will no longer be used by other plugins or themes.
pull/1043/head
Nils Winkler 2017-09-15 08:10:17 +02:00
parent 0fe2710c61
commit 145ec5dfaa
10 changed files with 60 additions and 28 deletions

View File

@ -5,6 +5,15 @@ BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN=${BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN:-250
BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION=${BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION:-350} BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION=${BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION:-350}
BASH_IT_LOAD_PRIORITY_SEPARATOR="---" BASH_IT_LOAD_PRIORITY_SEPARATOR="---"
function _command_exists ()
{
_about 'checks for existence of a command'
_param '1: command to check'
_example '$ _command_exists ls && echo exists'
_group 'lib'
type "$1" &> /dev/null ;
}
# Helper function loading various enable-able files # Helper function loading various enable-able files
function _load_bash_it_files() { function _load_bash_it_files() {
subdirectory="$1" subdirectory="$1"

View File

@ -2,23 +2,23 @@ cite about-plugin
about-plugin 'display info about your battery charge level' about-plugin 'display info about your battery charge level'
ac_adapter_connected(){ ac_adapter_connected(){
if command_exists upower; if _command_exists upower;
then then
upower -i $(upower -e | grep BAT) | grep 'state' | grep -q 'charging\|fully-charged' upower -i $(upower -e | grep BAT) | grep 'state' | grep -q 'charging\|fully-charged'
return $? return $?
elif command_exists acpi; elif _command_exists acpi;
then then
acpi -a | grep -q "on-line" acpi -a | grep -q "on-line"
return $? return $?
elif command_exists pmset; elif _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 _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 _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 $?
@ -26,23 +26,23 @@ ac_adapter_connected(){
} }
ac_adapter_disconnected(){ ac_adapter_disconnected(){
if command_exists upower; if _command_exists upower;
then then
upower -i $(upower -e | grep BAT) | grep 'state' | grep -q 'discharging' upower -i $(upower -e | grep BAT) | grep 'state' | grep -q 'discharging'
return $? return $?
elif command_exists acpi; elif _command_exists acpi;
then then
acpi -a | grep -q "off-line" acpi -a | grep -q "off-line"
return $? return $?
elif command_exists pmset; elif _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 _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 _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 $?
@ -53,11 +53,11 @@ battery_percentage(){
about 'displays battery charge as a percentage of full (100%)' about 'displays battery charge as a percentage of full (100%)'
group 'battery' group 'battery'
if command_exists upower; if _command_exists upower;
then then
local UPOWER_OUTPUT=$(upower --show-info $(upower --enumerate | grep BAT) | grep percentage | tail --bytes 5) local UPOWER_OUTPUT=$(upower --show-info $(upower --enumerate | grep BAT) | grep percentage | tail --bytes 5)
echo ${UPOWER_OUTPUT: : -1} echo ${UPOWER_OUTPUT: : -1}
elif command_exists acpi; elif _command_exists acpi;
then then
local ACPI_OUTPUT=$(acpi -b) local ACPI_OUTPUT=$(acpi -b)
case $ACPI_OUTPUT in case $ACPI_OUTPUT in
@ -84,7 +84,7 @@ battery_percentage(){
echo '-1' echo '-1'
;; ;;
esac esac
elif command_exists pmset; elif _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
@ -95,7 +95,7 @@ battery_percentage(){
echo $PMSET_OUTPUT | head -c 2 echo $PMSET_OUTPUT | head -c 2
;; ;;
esac esac
elif command_exists ioreg; elif _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
@ -106,7 +106,7 @@ battery_percentage(){
echo $IOREG_OUTPUT | head -c 2 echo $IOREG_OUTPUT | head -c 2
;; ;;
esac esac
elif command_exists WMIC; elif _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

View File

@ -30,6 +30,26 @@ function local_setup {
# TODO Create global __get_base_name function # TODO Create global __get_base_name function
# TODO Create global __get_enabled_name function # TODO Create global __get_enabled_name function
@test "helpers: _command_exists function exists" {
type -a _command_exists &> /dev/null
assert_success
}
@test "helpers: _command_exists function positive test ls" {
run _command_exists ls
assert_success
}
@test "helpers: _command_exists function positive test bash-it" {
run _command_exists bash-it
assert_success
}
@test "helpers: _command_exists function negative test" {
run _command_exists __addfkds_dfdsjdf
assert_failure
}
@test "helpers: bash-it help aliases ag" { @test "helpers: bash-it help aliases ag" {
run bash-it help aliases "ag" run bash-it help aliases "ag"
assert_line "0" "ag='ag --smart-case --pager=\"less -MIRFX'" assert_line "0" "ag='ag --smart-case --pager=\"less -MIRFX'"

View File

@ -2,7 +2,10 @@
load ../test_helper load ../test_helper
load ../../lib/composure load ../../lib/composure
load ../../plugins/available/base.plugin
cite _about _param _example _group _author _version
load ../../lib/helpers
load ../../themes/base.theme load ../../themes/base.theme
@test 'themes base: battery_percentage should not exist' { @test 'themes base: battery_percentage should not exist' {

View File

@ -164,7 +164,7 @@ ___atomic_prompt_clock() {
} }
___atomic_prompt_battery() { ___atomic_prompt_battery() {
! command_exists battery_percentage || ! _command_exists battery_percentage ||
[ "${THEME_SHOW_BATTERY}" != "true" ] || [ "${THEME_SHOW_BATTERY}" != "true" ] ||
[ "$(battery_percentage)" = "no" ] && return [ "$(battery_percentage)" = "no" ] && return

View File

@ -474,7 +474,7 @@ function battery_char {
fi fi
} }
if ! command_exists battery_charge ; then if ! _command_exists battery_charge ; then
# if user has installed battery plugin, skip this... # if user has installed battery plugin, skip this...
function battery_charge (){ function battery_charge (){
# no op # no op
@ -484,7 +484,7 @@ fi
# The battery_char function depends on the presence of the battery_percentage function. # The battery_char function depends on the presence of the battery_percentage function.
# If battery_percentage is not defined, then define battery_char as a no-op. # If battery_percentage is not defined, then define battery_char as a no-op.
if ! command_exists battery_percentage ; then if ! _command_exists battery_percentage ; then
function battery_char (){ function battery_char (){
# no op # no op
echo -n echo -n

View File

@ -152,7 +152,7 @@ ___brainy_prompt_clock() {
} }
___brainy_prompt_battery() { ___brainy_prompt_battery() {
! command_exists battery_percentage || ! _command_exists battery_percentage ||
[ "${THEME_SHOW_BATTERY}" != "true" ] || [ "${THEME_SHOW_BATTERY}" != "true" ] ||
[ "$(battery_percentage)" = "no" ] && return [ "$(battery_percentage)" = "no" ] && return

View File

@ -84,7 +84,7 @@ ${D_BRANCH_COLOR}%b %r ${D_CHANGES_COLOR}%m%u ${D_DEFAULT_COLOR}"
# checks if the plugin is installed before calling battery_charge # checks if the plugin is installed before calling battery_charge
safe_battery_charge() { safe_battery_charge() {
if command_exists battery_charge ; if _command_exists battery_charge ;
then then
battery_charge battery_charge
fi fi

View File

@ -43,9 +43,9 @@ function __powerline_user_info_prompt {
function __powerline_ruby_prompt { function __powerline_ruby_prompt {
local ruby_version="" local ruby_version=""
if command_exists rvm; then if _command_exists rvm; then
ruby_version="$(rvm_version_prompt)" ruby_version="$(rvm_version_prompt)"
elif command_exists rbenv; then elif _command_exists rbenv; then
ruby_version=$(rbenv_version_prompt) ruby_version=$(rbenv_version_prompt)
fi fi

View File

@ -117,7 +117,7 @@ ${D_BRANCH_COLOR}%b %r ${D_CHANGES_COLOR}%m%u ${D_DEFAULT_COLOR}"
# checks if the plugin is installed before calling battery_charge # checks if the plugin is installed before calling battery_charge
safe_battery_charge() { safe_battery_charge() {
if command_exists battery_charge ; if _command_exists battery_charge ;
then then
battery_charge battery_charge
fi fi