lib/search: code cleanup

Improve `_bash-it-erase-term()`, `_bash-it-flash-term()`, `_bash-it-rewind()`, `_bash-it-search-result()`, and `_bash-it-search-component()`. Minor tweaks to `_bash-it-is-partial-match()`, and `_bash-it-search()`.
pull/1932/head
John D Pell 2021-10-20 22:42:10 -04:00 committed by John D Pell
parent b8694ee140
commit 4cf2aae36e
2 changed files with 68 additions and 60 deletions

View File

@ -55,7 +55,7 @@ function _bash-it-search() {
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
@ -168,8 +168,8 @@ ${echo_underline_yellow-}SUMMARY${echo_normal-}
} }
function _bash-it-is-partial-match() { function _bash-it-is-partial-match() {
local component="${1?}" local component="${1?${FUNCNAME[0]}: component type must be specified}"
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}"
} }
@ -191,12 +191,12 @@ function _bash-it-search-component() {
_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?${FUNCNAME[0]}: component type must be specified}"
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}" "$@"; then if _bash-it-array-contains-element "--${search_command}" "$@"; then
@ -247,30 +247,34 @@ function _bash-it-search-component() {
local -a matches=() local -a matches=()
for match in "${total_matches[@]}"; do for match in "${total_matches[@]}"; do
local include_match=true local -i include_match=1
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=0
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[@]:-}"
} }
function _bash-it-search-result() { function _bash-it-search-result() {
local component="$1" local component="${1?${FUNCNAME[0]}: component type must be specified}"
shift shift
local action="$1" local action="${1:-}"
shift shift
local action_func="$1" local action_func="${1:-}"
shift shift
local -a matches=("$@")
local color_component color_enable color_disable color_off local color_component color_enable color_disable color_off
local color_sep=':' line
color_sep=':' local -a matches=()
# Discard any empty arguments
while IFS='' read -r line; do
[[ -n "${line}" ]] && matches+=("$line")
done < <(_bash-it-array-dedup "${@}")
if ${BASH_IT_SEARCH_USE_COLOR}; then if [[ "${BASH_IT_SEARCH_USE_COLOR}" == "true" ]]; 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=''
@ -290,37 +294,35 @@ function _bash-it-search-result() {
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)) && { if ((enabled)); then
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"
} else
match_color="${color_disable}"
((enabled)) || { suffix="${suffix_disable}"
match_color=${color_disable} opposite_suffix="${suffix_enable}"
suffix=${suffix_disable}
opposite_suffix=${suffix_enable}
compatible_action="enable" compatible_action="enable"
} fi
local m="${match}${suffix}" local matched="${match}${suffix}"
local -i len=${#m} local -i len="${#matched}"
printf '%b' " ${match_color}${match}${suffix}" # print current state printf '%b' "${match_color}${matched}" # 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}" == "true" ]]; then
_bash-it-flash-term "${len}" "${match}${suffix}" _bash-it-flash-term "${len}" "${matched}"
else else
_bash-it-erase-term "${len}" _bash-it-erase-term "${len}" "${matched}"
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
@ -331,38 +333,44 @@ function _bash-it-search-result() {
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)) && _bash-it-clean-component-cache "${component}"
printf "\n" printf "\n"
fi fi
} }
function _bash-it-rewind() { function _bash-it-rewind() {
local -i len="$1" local -i len="${1:-0}"
printf '%b' "\033[${len}D" printf '%b' "\033[${len}D"
} }
function _bash-it-flash-term() { function _bash-it-flash-term() {
local -i len="${1:-0}" local -i len="${1:-0}" # redundant
local match="${2:-}" local term="${2:-}"
# as currently implemented, `$match` has already been printed to screen the first time
local delay=0.1 local delay=0.1
local color local color
[[ "${#term}" -gt 0 ]] && len="${#term}"
for color in "${echo_black-}" "${echo_bold_blue-}" "${echo_bold_yellow-}" "${echo_bold_red-}" "${echo_bold_green-}" "${echo_normal-}"; do for color in "${echo_black-}" "${echo_bold_blue-}" "${echo_bold_yellow-}" "${echo_bold_red-}" "${echo_bold_green-}" "${echo_normal-}"; do
sleep "${delay}" sleep "${delay}"
_bash-it-rewind "${len}" _bash-it-rewind "${len}"
printf '%b' "${color}${match}" printf '%b' "${color}${term}"
done done
} }
function _bash-it-erase-term() { function _bash-it-erase-term() {
local -i len="${1:-0}" local -i len="${1:-0}" i
local delay=0.05
local term="${2:-}" # calculate length ourselves
[[ "${#term}" -gt 0 ]] && len="${#term}"
_bash-it-rewind "${len}" _bash-it-rewind "${len}"
for a in {0..30}; do # white-out the already-printed term by printing blanks
[[ ${a} -gt ${len} ]] && break for ((i = 0; i <= len; i++)); do
printf "%.*s" "$a" " " printf "%.*s" "$i" " "
sleep 0.05 sleep "${delay}"
done done
} }

30
test/lib/search.bats 100644 → 100755
View File

@ -28,42 +28,42 @@ function local_teardown {
@test "search: plugin base" { @test "search: plugin base" {
export BASH_IT_SEARCH_USE_COLOR=false export BASH_IT_SEARCH_USE_COLOR=false
run _bash-it-search-component 'plugins' 'base' run _bash-it-search-component 'plugins' 'base'
assert_line -n 0 ' plugins: base ' assert_line -n 0 ' plugins: base '
} }
@test "search: git" { @test "search: git" {
run _bash-it-search 'git' --no-color run _bash-it-search 'git' --no-color
assert_line -n 0 ' aliases: git gitsvn ' assert_line -n 0 ' aliases: git gitsvn '
assert_line -n 1 -p ' plugins:' assert_line -n 1 -p ' plugins:'
for plugin in "autojump" "git" "gitstatus" "git-subrepo" "jgitflow" "jump" for plugin in "autojump" "git" "gitstatus" "git-subrepo" "jgitflow" "jump"
do do
echo $plugin echo $plugin
assert_line -n 1 -p $plugin assert_line -n 1 -p $plugin
done done
assert_line -n 2 ' completions: git git_flow git_flow_avh github-cli ' assert_line -n 2 ' completions: git git_flow git_flow_avh github-cli '
} }
@test "search: ruby gem bundle rake rails" { @test "search: ruby gem bundle rake rails" {
run _bash-it-search rails ruby gem bundler rake --no-color run _bash-it-search rails ruby gem bundler rake --no-color
assert_line -n 0 ' aliases: bundler rails ' assert_line -n 0 ' aliases: bundler rails '
assert_line -n 1 ' plugins: chruby chruby-auto rails ruby ' assert_line -n 1 ' plugins: chruby chruby-auto rails ruby '
assert_line -n 2 ' completions: bundler gem rake ' assert_line -n 2 ' completions: bundler gem rake '
} }
@test "search: rails ruby gem bundler rake -chruby" { @test "search: rails ruby gem bundler rake -chruby" {
run _bash-it-search rails ruby gem bundler rake -chruby --no-color run _bash-it-search rails ruby gem bundler rake -chruby --no-color
assert_line -n 0 ' aliases: bundler rails ' assert_line -n 0 ' aliases: bundler rails '
assert_line -n 1 ' plugins: rails ruby ' assert_line -n 1 ' plugins: rails ruby '
assert_line -n 2 ' completions: bundler gem rake ' assert_line -n 2 ' completions: bundler gem rake '
} }
@test "search: @git" { @test "search: @git" {
run _bash-it-search '@git' --no-color run _bash-it-search '@git' --no-color
assert_line -n 0 ' aliases: git ' assert_line -n 0 ' aliases: git '
assert_line -n 1 ' plugins: git ' assert_line -n 1 ' plugins: git '
assert_line -n 2 ' completions: git ' assert_line -n 2 ' completions: git '
} }
@test "search: @git --enable / --disable" { @test "search: @git --enable / --disable" {
@ -76,7 +76,7 @@ function local_teardown {
run _bash-it-search '@git' --disable --no-color run _bash-it-search '@git' --disable --no-color
run _bash-it-search '@git' --no-color run _bash-it-search '@git' --no-color
assert_line -n 0 ' aliases: git ' assert_line -n 0 ' aliases: git '
assert_line -n 1 ' plugins: git ' assert_line -n 1 ' plugins: git '
assert_line -n 2 ' completions: git ' assert_line -n 2 ' completions: git '
} }