lib/search: code style cleanup

Couldn't even `shellcheck` until I did a first pass...too much noise! ♥
pull/1932/head
John D Pell 2021-10-25 11:09:27 -07:00 committed by John D Pell
parent 7e79212dff
commit 5478617a89
1 changed files with 46 additions and 45 deletions

91
lib/search.bash 100755 → 100644
View File

@ -53,29 +53,32 @@ _bash-it-search() {
_param '2: [ term2 ]...' _param '2: [ term2 ]...'
_example '$ _bash-it-search @git ruby -rvm rake bundler' _example '$ _bash-it-search @git ruby -rvm rake bundler'
[[ -z "$(type _bash-it-array-contains-element 2>/dev/null)" ]] && source "${BASH_IT}/lib/utilities.bash"
local component local component
export BASH_IT_SEARCH_USE_COLOR=true local BASH_IT_SEARCH_USE_COLOR="${BASH_IT_SEARCH_USE_COLOR:=true}"
declare -a BASH_IT_COMPONENTS=(aliases plugins completions) local -a BASH_IT_COMPONENTS=(aliases plugins completions)
if [[ -z "$*" ]] ; then if [[ $# -eq 0 ]] ; then
_bash-it-search-help _bash-it-search-help
return 0 return 0
fi fi
local -a args=() local -a args=()
for word in $@; do for word in "$@"; do
if [[ ${word} == "--help" || ${word} == "-h" ]]; then case "${word}" in
'-h'|'--help')
_bash-it-search-help _bash-it-search-help
return 0 return 0
elif [[ ${word} == "--refresh" || ${word} == "-r" ]]; then ;;
'-r'|'--refresh')
_bash-it-clean-component-cache _bash-it-clean-component-cache
elif [[ ${word} == "--no-color" || ${word} == '-c' ]]; then ;;
export BASH_IT_SEARCH_USE_COLOR=false '-c'|'--no-color')
else BASH_IT_SEARCH_USE_COLOR=false
args=(${args[@]} ${word}) ;;
fi *)
args+=("${word}")
;;
esac
done done
if [[ ${#args} -gt 0 ]]; then if [[ ${#args} -gt 0 ]]; then
@ -196,9 +199,8 @@ _bash-it-search-component() {
local -a search_commands=(enable disable) local -a search_commands=(enable disable)
for search_command in "${search_commands[@]}"; do for search_command in "${search_commands[@]}"; do
if $(_bash-it-array-contains-element "--${search_command}" "$@"); then if $(_bash-it-array-contains-element "--${search_command}" "$@"); then
component_singular=${component} component_singular="${component/es/}" # aliases -> alias
component_singular=${component_singular/es/} # aliases -> alias component_singular="${component_singular/ns/n}" # plugins -> plugin
component_singular=${component_singular/ns/n} # plugins -> plugin
action="${search_command}" action="${search_command}"
action_func="_${action}-${component_singular}" action_func="_${action}-${component_singular}"
@ -206,7 +208,7 @@ _bash-it-search-component() {
fi fi
done done
local -a terms=($@) # passed on the command line local -a terms=("$@") # passed on the command line
unset exact_terms unset exact_terms
unset partial_terms unset partial_terms
@ -225,26 +227,26 @@ _bash-it-search-component() {
if [[ "${term:0:2}" == "--" ]] ; then if [[ "${term:0:2}" == "--" ]] ; then
continue continue
elif [[ "${term:0:1}" == "-" ]] ; then elif [[ "${term:0:1}" == "-" ]] ; then
negative_terms=(${negative_terms[@]} "${search_term}") negative_terms+=("${search_term}")
elif [[ "${term:0:1}" == "@" ]] ; then elif [[ "${term:0:1}" == "@" ]] ; then
if $(_bash-it-array-contains-element "${search_term}" "${component_list[@]}"); then if $(_bash-it-array-contains-element "${search_term}" "${component_list[@]}"); then
exact_terms=(${exact_terms[@]} "${search_term}") exact_terms+=( "${search_term}")
fi fi
else else
partial_terms=(${partial_terms[@]} $(_bash-it-component-list-matching "${component}" "${term}") ) partial_terms+=($(_bash-it-component-list-matching "${component}" "${term}") )
fi fi
done done
local -a total_matches=( $(_bash-it-array-dedup ${exact_terms[@]} ${partial_terms[@]}) ) local -a total_matches=( $(_bash-it-array-dedup "${exact_terms[@]}" "${partial_terms[@]}") )
unset matches unset matches
declare -a matches=() declare -a matches=()
for match in ${total_matches[@]}; do for match in "${total_matches[@]}"; do
local include_match=true local include_match=true
if [[ ${#negative_terms[@]} -gt 0 ]]; then if [[ ${#negative_terms[@]} -gt 0 ]]; then
( _bash-it-component-term-matches-negation "${match}" "${negative_terms[@]}" ) && include_match=false ( _bash-it-component-term-matches-negation "${match}" "${negative_terms[@]}" ) && include_match=false
fi fi
( ${include_match} ) && matches=(${matches[@]} "${match}") ( ${include_match} ) && matches+=("${match}")
done done
_bash-it-search-result "${component}" "${action}" "${action_func}" "${matches[@]}" _bash-it-search-result "${component}" "${action}" "${action_func}" "${matches[@]}"
unset matches final_matches terms unset matches final_matches terms
@ -254,38 +256,37 @@ _bash-it-search-result() {
local component="$1"; shift local component="$1"; shift
local action="$1"; shift local action="$1"; shift
local action_func="$1"; shift local action_func="$1"; shift
local -a matches=($@) local -a matches=("$@")
local color_component color_enable color_disable color_off local color_component color_enable color_disable color_off
color_sep=':' color_sep=':'
( ${BASH_IT_SEARCH_USE_COLOR} ) && { if ${BASH_IT_SEARCH_USE_COLOR}
then
color_component='\e[1;34m' color_component='\e[1;34m'
color_enable='\e[1;32m' color_enable='\e[1;32m'
suffix_enable='' suffix_enable=''
suffix_disable='' suffix_disable=''
color_disable='\e[0;0m' color_disable='\e[0;0m'
color_off='\e[0;0m' color_off='\e[0;0m'
} else
( ${BASH_IT_SEARCH_USE_COLOR} ) || {
color_component='' color_component=''
suffix_enable=' ✓ ' suffix_enable=' ✓ '
suffix_disable=' ' suffix_disable=' '
color_enable='' color_enable=''
color_disable='' color_disable=''
color_off='' color_off=''
} fi
local match local match
local modified=0 local -i modified=0
if [[ "${#matches[@]}" -gt 0 ]] ; then if [[ "${#matches[@]}" -gt 0 ]] ; then
printf "${color_component}%13s${color_sep} ${color_off}" "${component}" printf "${color_component}%13s${color_sep} ${color_off}" "${component}"
for match in "${matches[@]}"; do for match in "${matches[@]}"; do
local enabled=0 local -i enabled=0
( _bash-it-component-item-is-enabled "${component}" "${match}" ) && enabled=1 ( _bash-it-component-item-is-enabled "${component}" "${match}" ) && enabled=1
local match_color compatible_action suffix opposite_suffix local match_color compatible_action suffix opposite_suffix
@ -305,28 +306,27 @@ _bash-it-search-result() {
} }
local m="${match}${suffix}" local m="${match}${suffix}"
local len local -i len=${#m}
len=${#m}
printf " ${match_color}${match}${suffix}" # print current state printf " ${match_color}${match}${suffix}" # print current state
if [[ "${action}" == "${compatible_action}" ]]; then if [[ "${action}" == "${compatible_action}" ]]; then
if [[ ${action} == "enable" && ${BASH_IT_SEARCH_USE_COLOR} == false ]]; then if [[ "${action}" == "enable" && "${BASH_IT_SEARCH_USE_COLOR}" == false ]]; then
_bash-it-flash-term ${len} "${match}${suffix}" _bash-it-flash-term "${len}" "${match}${suffix}"
else else
_bash-it-erase-term ${len} _bash-it-erase-term ${len}
fi fi
modified=1 modified=1
result=$(${action_func} ${match}) result=$("${action_func}" "${match}")
local temp="color_${compatible_action}" local temp="color_${compatible_action}"
match_color=${!temp} match_color="${!temp}"
_bash-it-rewind ${len} _bash-it-rewind "${len}"
printf "${match_color}${match}${opposite_suffix}" printf "${match_color}${match}${opposite_suffix}"
fi fi
printf "${color_off}" printf "${color_off}"
done done
[[ ${modified} -gt 0 ]] && _bash-it-clean-component-cache ${component} [[ ${modified} -gt 0 ]] && _bash-it-clean-component-cache "${component}"
printf "\n" printf "\n"
fi fi
} }
@ -337,24 +337,25 @@ _bash-it-rewind() {
} }
_bash-it-flash-term() { _bash-it-flash-term() {
local len="$1" local -i len="${1:-0}"
local match="$2" local match="${2:-}"
local delay=0.1 local delay=0.1
local color local color
for color in ${text_black} ${echo_bold_blue} ${bold_yellow} ${bold_red} ${echo_bold_green} ; do for color in "${text_black}" "${echo_bold_blue}" "${bold_yellow}" "${bold_red}" "${echo_bold_green}"
sleep ${delay} do
sleep "${delay}"
_bash-it-rewind "${len}" _bash-it-rewind "${len}"
printf "${color}${match}" printf "${color}${match}"
done done
} }
_bash-it-erase-term() { _bash-it-erase-term() {
local len="$1" local -i len="${1:-0}"
_bash-it-rewind ${len} _bash-it-rewind ${len}
for a in {0..30}; do for a in {0..30}; do
[[ ${a} -gt ${len} ]] && break [[ ${a} -gt ${len} ]] && break
printf "%.*s" $a " " printf "%.*s" "$a" " "
sleep 0.05 sleep 0.05
done done
} }