diff --git a/lib/utilities.bash b/lib/utilities.bash index dbefbc27..7994a731 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -2,6 +2,51 @@ # # A collection of reusable functions. +########################################################################### +# Generic utilies +########################################################################### + +# This function searches an array for an exact match against the term passed +# as the first argument to the function. This function exits as soon as +# a match is found. +# +# Returns: +# 0 when a match is found, otherwise 1. +# +# Examples: +# $ declare -a fruits=(apple orange pear mandarin) +# +# $ _bash-it-array-contains-element apple "@{fruits[@]}" && echo 'contains apple' +# contains apple +# +# $ if $(_bash-it-array-contains-element pear "${fruits[@]}"); then +# echo "contains pear!" +# fi +# contains pear! +# +# +_bash-it-array-contains-element() { + local e + for e in "${@:2}"; do + [[ "$e" == "$1" ]] && return 0 + done + return 1 +} + +# Dedupe a simple array of words without spaces. +_bash-it-array-dedup() { + echo "$*" | tr ' ' '\n' | sort -u | tr '\n' ' ' +} + +# Outputs a full path of the grep found on the filesystem +_bash-it-grep() { + if [[ -z "${BASH_IT_GREP}" ]] ; then + export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')" + fi + printf "%s " "${BASH_IT_GREP}" +} + + ########################################################################### # Component-specific functions (component is either an alias, a plugin, or a # completion). @@ -50,51 +95,6 @@ _bash-it-clean-component-cache() { fi } -########################################################################### -# Generic utilies -########################################################################### - -# This function searches an array for an exact match against the term passed -# as the first argument to the function. This function exits as soon as -# a match is found. -# -# Returns: -# 0 when a match is found, otherwise 1. -# -# Examples: -# $ declare -a fruits=(apple orange pear mandarin) -# -# $ _bash-it-array-contains-element apple "@{fruits[@]}" && echo 'contains apple' -# contains apple -# -# $ if $(_bash-it-array-contains-element pear "${fruits[@]}"); then -# echo "contains pear!" -# fi -# contains pear! -# -# -_bash-it-array-contains-element() { - local e - for e in "${@:2}"; do - [[ "$e" == "$1" ]] && return 0 - done - return 1 -} - -# Dedupe a simple array of words without spaces. -_bash-it-array-dedup() { - echo "$*" | tr ' ' '\n' | sort -u | tr '\n' ' ' -} - -# Outputs a full path of the grep found on the filesystem -_bash-it-grep() { - if [[ -z "${BASH_IT_GREP}" ]] ; then - export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')" - fi - printf "%s " "${BASH_IT_GREP}" -} - - # Returns an array of items within each compoenent. _bash-it-component-list() { local component="$1"