lib/search: `shfmt`

My apologies to future `git blame` hunters ♥
pull/1932/head
John D Pell 2022-01-26 10:24:51 -08:00
parent 8939e943c5
commit b8694ee140
2 changed files with 189 additions and 191 deletions

View File

@ -83,6 +83,7 @@ completion/available/wpscan.completion.bash
lib/helpers.bash lib/helpers.bash
lib/log.bash lib/log.bash
lib/preexec.bash lib/preexec.bash
lib/search.bash
lib/utilities.bash lib/utilities.bash
# plugins # plugins

View File

@ -47,51 +47,51 @@
# completions: git # completions: git
# #
_bash-it-search() { function _bash-it-search() {
_about 'searches for given terms amongst bash-it plugins, aliases and completions' _about 'searches for given terms amongst bash-it plugins, aliases and completions'
_param '1: term1' _param '1: term1'
_param '2: [ term2 ]...' _param '2: [ term2 ]...'
_example '$ _bash-it-search @git ruby -rvm rake bundler' _example '$ _bash-it-search @git ruby -rvm rake bundler'
local component local component
local BASH_IT_SEARCH_USE_COLOR="${BASH_IT_SEARCH_USE_COLOR:=true}" local BASH_IT_SEARCH_USE_COLOR="${BASH_IT_SEARCH_USE_COLOR:=true}"
local -a BASH_IT_COMPONENTS=(aliases plugins completions) local -a BASH_IT_COMPONENTS=(aliases plugins completions)
if [[ $# -eq 0 ]] ; 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
case "${word}" in case "${word}" in
'-h'|'--help') '-h' | '--help')
_bash-it-search-help _bash-it-search-help
return 0 return 0
;; ;;
'-r'|'--refresh') '-r' | '--refresh')
_bash-it-clean-component-cache _bash-it-clean-component-cache
;; ;;
'-c'|'--no-color') '-c' | '--no-color')
BASH_IT_SEARCH_USE_COLOR=false BASH_IT_SEARCH_USE_COLOR=false
;; ;;
*) *)
args+=("${word}") args+=("${word}")
;; ;;
esac esac
done done
if [[ ${#args} -gt 0 ]]; then if [[ ${#args} -gt 0 ]]; then
for component in "${BASH_IT_COMPONENTS[@]}" ; do for component in "${BASH_IT_COMPONENTS[@]}"; do
_bash-it-search-component "${component}" "${args[@]}" _bash-it-search-component "${component}" "${args[@]}"
done done
fi fi
return 0 return 0
} }
_bash-it-search-help() { function _bash-it-search-help() {
printf '%b' "${echo_normal-} printf '%b' "${echo_normal-}
${echo_underline_yellow-}USAGE${echo_normal-} ${echo_underline_yellow-}USAGE${echo_normal-}
bash-it search [-|@]term1 [-|@]term2 ... \\ bash-it search [-|@]term1 [-|@]term2 ... \\
@ -167,205 +167,202 @@ ${echo_underline_yellow-}SUMMARY${echo_normal-}
" "
} }
_bash-it-is-partial-match() { function _bash-it-is-partial-match() {
local component="${1?}" local component="${1?}"
local term="${2?}" local term="${2?}"
_bash-it-component-help "${component}" | "_bash-it-egrep -i -q -- "${term}" _bash-it-component-help "${component}" | _bash-it-egrep -i -q -- "${term}"
} }
_bash-it-component-term-matches-negation() { function _bash-it-component-term-matches-negation() {
local match="$1"; shift local match="$1"
local negative shift
for negative in "$@"; do local negative
[[ "${match}" =~ ${negative} ]] && return 0 for negative in "$@"; do
done [[ "${match}" =~ ${negative} ]] && return 0
done
return 1 return 1
} }
_bash-it-search-component() { function _bash-it-search-component() {
_about 'searches for given terms amongst a given component' _about 'searches for given terms amongst a given component'
_param '1: component type, one of: [ aliases | plugins | completions ]' _param '1: component type, one of: [ aliases | plugins | completions ]'
_param '2: term1 term2 @term3' _param '2: term1 term2 @term3'
_param '3: [-]term4 [-]term5 ...' _param '3: [-]term4 [-]term5 ...'
_example '$ _bash-it-search-component aliases @git rake bundler -chruby' _example '$ _bash-it-search-component aliases @git rake bundler -chruby'
local component="$1" local component="$1"
shift shift
# if one of the search terms is --enable or --disable, we will apply # if one of the search terms is --enable or --disable, we will apply
# this action to the matches further ` down. # this action to the matches further ` down.
local component_singular action action_func local component_singular= action= action_func=
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}" "$@" if _bash-it-array-contains-element "--${search_command}" "$@"; then
then component_singular="${component/es/}" # aliases -> alias
component_singular="${component/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}"
break break
fi fi
done done
local -a terms=("$@") # passed on the command line local -a terms=("$@") # passed on the command line
local -a exact_terms=() # terms that should be included only if they match exactly local -a exact_terms=() # terms that should be included only if they match exactly
local -a partial_terms=() # terms that should be included if they match partially local -a partial_terms=() # terms that should be included if they match partially
local -a negative_terms=() # negated partial terms that should be excluded local -a negative_terms=() # negated partial terms that should be excluded
local term line local term line
local -a component_list=() local -a component_list=()
while IFS='' read -r line while IFS='' read -r line; do
do
component_list+=("$line") component_list+=("$line")
done < <(_bash-it-component-list "${component}") done < <(_bash-it-component-list "${component}")
for term in "${terms[@]}"; do for term in "${terms[@]}"; do
local search_term="${term:1}" local search_term="${term:1}"
if [[ "${term:0:2}" == "--" ]] ; then if [[ "${term:0:2}" == "--" ]]; then
continue continue
elif [[ "${term:0:1}" == "-" ]] ; then elif [[ "${term:0:1}" == "-" ]]; then
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[@]:-}" if _bash-it-array-contains-element "${search_term}" "${component_list[@]:-}"; then
then exact_terms+=("${search_term}")
exact_terms+=( "${search_term}") fi
fi else
else while IFS='' read -r line; do
while IFS='' read -r line partial_terms+=("$line")
do
partial_terms+=( "$line" )
done < <(_bash-it-component-list-matching "${component}" "${term}") done < <(_bash-it-component-list-matching "${component}" "${term}")
fi fi
done done
local -a total_matches=() local -a total_matches=()
while IFS='' read -r line while IFS='' read -r line; do
do
total_matches+=("$line") total_matches+=("$line")
done < <(_bash-it-array-dedup "${exact_terms[@]:-}" "${partial_terms[@]:-}") done < <(_bash-it-array-dedup "${exact_terms[@]:-}" "${partial_terms[@]:-}")
local -a matches=() local -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+=("${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[@]:-}"
} }
_bash-it-search-result() { function _bash-it-search-result() {
local component="$1"; shift local component="$1"
local action="$1"; shift shift
local action_func="$1"; shift local action="$1"
local -a matches=("$@") shift
local action_func="$1"
shift
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=':'
if ${BASH_IT_SEARCH_USE_COLOR} if ${BASH_IT_SEARCH_USE_COLOR}; then
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 else
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 fi
local match local match
local -i 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 -i 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
(( enabled )) && { ((enabled)) && {
match_color=${color_enable} match_color=${color_enable}
suffix=${suffix_enable} suffix=${suffix_enable}
opposite_suffix=${suffix_disable} opposite_suffix=${suffix_disable}
compatible_action="disable" compatible_action="disable"
} }
(( enabled )) || { ((enabled)) || {
match_color=${color_disable} match_color=${color_disable}
suffix=${suffix_disable} suffix=${suffix_disable}
opposite_suffix=${suffix_enable} opposite_suffix=${suffix_enable}
compatible_action="enable" compatible_action="enable"
} }
local m="${match}${suffix}" local m="${match}${suffix}"
local -i len=${#m} local -i len=${#m}
printf '%b' " ${match_color}${match}${suffix}" # print current state printf '%b' " ${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
# shellcheck disable=SC2034 # no idea if `$result` is ever used # shellcheck disable=SC2034 # no idea if `$result` is ever used
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 '%b' "${match_color}${match}${opposite_suffix}" printf '%b' "${match_color}${match}${opposite_suffix}"
fi fi
printf '%b' "${color_off}" printf '%b' "${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
} }
_bash-it-rewind() { function _bash-it-rewind() {
local -i len="$1" local -i len="$1"
printf '%b' "\033[${len}D" printf '%b' "\033[${len}D"
} }
_bash-it-flash-term() { function _bash-it-flash-term() {
local -i len="${1:-0}" 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 "${echo_black-}" "${echo_bold_blue-}" "${echo_bold_yellow-}" "${echo_bold_red-}" "${echo_bold_green-}" "${echo_normal-}" for color in "${echo_black-}" "${echo_bold_blue-}" "${echo_bold_yellow-}" "${echo_bold_red-}" "${echo_bold_green-}" "${echo_normal-}"; do
do sleep "${delay}"
sleep "${delay}" _bash-it-rewind "${len}"
_bash-it-rewind "${len}" printf '%b' "${color}${match}"
printf '%b' "${color}${match}" done
done
} }
_bash-it-erase-term() { function _bash-it-erase-term() {
local -i len="${1:-0}" 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
} }