Speed up bash-it Search & support exact matches
This commit improves Bash-It search functionality in a couple of ways: * bash-it search (with no arguments) will print detailed help. * bash-it search now accepts terms prefixed with '@' sign, indicating an exact match. * bash-it search now performs smarter caching of the component listings/status New search syntax is as follows: bash-it search [-|@]term1 [-|@]term2 [ --enable | --disable | --help ]pull/1273/head
parent
8feebc0aa9
commit
baae0305b6
|
|
@ -70,7 +70,7 @@ bash-it ()
|
||||||
example '$ bash-it disable alias hg [tmux]...'
|
example '$ bash-it disable alias hg [tmux]...'
|
||||||
example '$ bash-it migrate'
|
example '$ bash-it migrate'
|
||||||
example '$ bash-it update'
|
example '$ bash-it update'
|
||||||
example '$ bash-it search ruby [[-]rake]... [--enable | --disable]'
|
example '$ bash-it search [-|@]term1 [-|@]term2 ... [--enable | --disable | --help | --refresh | --no-color ]'
|
||||||
example '$ bash-it version'
|
example '$ bash-it version'
|
||||||
example '$ bash-it reload'
|
example '$ bash-it reload'
|
||||||
typeset verb=${1:-}
|
typeset verb=${1:-}
|
||||||
|
|
@ -393,6 +393,9 @@ _disable-thing ()
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
local flag="DEFER_CACHE_CLEANUP_FOR_${file_type}"
|
||||||
|
[[ -z ${!flag} ]] && _bash_it_search_cache_clean "${file_type}"
|
||||||
|
|
||||||
if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then
|
if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then
|
||||||
exec ${0/-/}
|
exec ${0/-/}
|
||||||
fi
|
fi
|
||||||
|
|
@ -488,6 +491,9 @@ _enable-thing ()
|
||||||
ln -s ../$subdirectory/available/$to_enable "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}"
|
ln -s ../$subdirectory/available/$to_enable "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
local flag="DEFER_CACHE_CLEANUP_FOR_${file_type}"
|
||||||
|
[[ -z ${!flag} ]] && _bash_it_search_cache_clean "${file_type}"
|
||||||
|
|
||||||
if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then
|
if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then
|
||||||
exec ${0/-/}
|
exec ${0/-/}
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
453
lib/search.bash
453
lib/search.bash
|
|
@ -1,3 +1,4 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
#
|
#
|
||||||
# Search by Konstantin Gredeskoul «github.com/kigster»
|
# Search by Konstantin Gredeskoul «github.com/kigster»
|
||||||
#———————————————————————————————————————————————————————————————————————————————
|
#———————————————————————————————————————————————————————————————————————————————
|
||||||
|
|
@ -5,46 +6,172 @@
|
||||||
# whose name or description matches one of the search terms provided as arguments.
|
# whose name or description matches one of the search terms provided as arguments.
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# ❯ bash-it search term1 [[-]term2] ... [[-]termN] [ --enable | --disable ]
|
# ❯ bash-it search [-|@]term1 [-|@]term2 ... \
|
||||||
|
# [ --enable | -e ] \
|
||||||
|
# [ --disable | -d ] \
|
||||||
|
# [ --refresh | -r ]
|
||||||
|
# [ --help | -h ]
|
||||||
#
|
#
|
||||||
# Exmplanation:
|
|
||||||
# Single dash, as in "-chruby", indicates a negative search term.
|
# Single dash, as in "-chruby", indicates a negative search term.
|
||||||
# Double dash indicates a command that is to be applied to the search result.
|
# Double dash indicates a command that is to be applied to the search result.
|
||||||
# At the moment only --enable and --disable are supported.
|
# At the moment only --help, --enable and --disable are supported.
|
||||||
|
# An '@' sign indicates an exact (not partial) match.
|
||||||
#
|
#
|
||||||
# Examples:
|
# Examples:
|
||||||
# ❯ bash-it search ruby rbenv rvm gem rake
|
# ❯ bash-it search ruby rbenv rvm gem rake
|
||||||
# aliases : bundler
|
# aliases: bundler
|
||||||
# plugins : chruby chruby-auto rbenv ruby rvm
|
# plugins: chruby chruby-auto ruby rbenv rvm ruby
|
||||||
# completions : gem rake
|
# completions: rvm gem rake
|
||||||
|
#
|
||||||
# ❯ bash-it search ruby rbenv rvm gem rake -chruby
|
# ❯ bash-it search ruby rbenv rvm gem rake -chruby
|
||||||
# aliases : bundler
|
# aliases: bundler
|
||||||
# plugins : rbenv ruby rvm
|
# plugins: ruby rbenv rvm ruby
|
||||||
# completions : gem rake
|
# completions: rvm gem rake
|
||||||
#
|
#
|
||||||
# Examples of enabling or disabling results of the search:
|
# Examples of enabling or disabling results of the search:
|
||||||
#
|
#
|
||||||
# ❯ bash-it search ruby
|
# ❯ bash-it search ruby
|
||||||
# aliases => bundler
|
# aliases: bundler
|
||||||
# plugins => chruby chruby-auto ruby
|
# plugins: chruby chruby-auto ruby
|
||||||
#
|
#
|
||||||
# ❯ bash-it search ruby -chruby --enable
|
# ❯ bash-it search ruby -chruby --enable
|
||||||
# aliases => ✓bundler
|
# aliases: bundler
|
||||||
# plugins => ✓ruby
|
# plugins: ruby
|
||||||
#
|
#
|
||||||
|
# Examples of using exact match:
|
||||||
|
|
||||||
|
# ❯ bash-it search @git @ruby
|
||||||
|
# aliases: git
|
||||||
|
# plugins: git ruby
|
||||||
|
# completions: git
|
||||||
#
|
#
|
||||||
|
|
||||||
_bash-it-search() {
|
_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 ruby rvm rake bundler'
|
_example '$ _bash-it-search @git ruby -rvm rake bundler'
|
||||||
|
|
||||||
declare -a _components=(aliases plugins completions)
|
local component
|
||||||
|
export BASH_IT_SEARCH_USE_COLOR=true
|
||||||
|
export BASH_IT_GREP=${BASH_IT_GREP:-$(which egrep)}
|
||||||
|
declare -a BASH_IT_COMPONENTS=(aliases plugins completions)
|
||||||
|
|
||||||
for _component in "${_components[@]}" ; do
|
if [[ -z "$*" ]] ; then
|
||||||
_bash-it-search-component "${_component}" "$@"
|
_bash-it-search-help
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
local -a args=()
|
||||||
|
for word in $@; do
|
||||||
|
if [[ ${word} == "--help" || ${word} == "-h" ]]; then
|
||||||
|
_bash-it-search-help
|
||||||
|
return 0
|
||||||
|
elif [[ ${word} == "--refresh" || ${word} == "-r" ]]; then
|
||||||
|
_bash_it_search_cache_clean
|
||||||
|
elif [[ ${word} == "--no-color" ]]; then
|
||||||
|
export BASH_IT_SEARCH_USE_COLOR=false
|
||||||
|
else
|
||||||
|
args=(${args[@]} ${word})
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
for component in "${BASH_IT_COMPONENTS[@]}" ; do
|
||||||
|
_bash-it-search-component "${component}" "${args[@]}"
|
||||||
|
done
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-search-help() {
|
||||||
|
printf "${echo_normal}
|
||||||
|
${echo_underline_yellow}USAGE${echo_normal}
|
||||||
|
|
||||||
|
bash-it search [-|@]term1 [-|@]term2 ... \\
|
||||||
|
[ --enable | --disable | --help | --refresh | --no-color ]
|
||||||
|
|
||||||
|
${echo_underline_yellow}DESCRIPTION${echo_normal}
|
||||||
|
|
||||||
|
Use ${echo_bold_green}search${echo_normal} bash-it command to search for a list of terms or term negations
|
||||||
|
across all components: aliases, completions and plugins. Components that are
|
||||||
|
enabled are shown in green (or with a check box if --no-color option is used).
|
||||||
|
|
||||||
|
In addition to simply finding the right component, you can use the results
|
||||||
|
of the search to enable or disable all components that the search returns.
|
||||||
|
|
||||||
|
When search is used to enable/disable components it becomes clear that
|
||||||
|
you must be able to perform not just a partial match, but an exact match,
|
||||||
|
as well as be able to exclude some components.
|
||||||
|
|
||||||
|
* To exclude a component (or all components matching a substring) use
|
||||||
|
a search term with minus as a prefix, eg '-flow'
|
||||||
|
|
||||||
|
* To perform an exact match, use character '@' in front of the term,
|
||||||
|
eg. '@git' would only match aliases, plugins and completions named 'git'.
|
||||||
|
|
||||||
|
${echo_underline_yellow}FLAGS${echo_normal}
|
||||||
|
--enable ${echo_purple}Enable all matching componenents.${echo_normal}
|
||||||
|
--disable ${echo_purple}Disable all matching componenents.${echo_normal}
|
||||||
|
--help ${echo_purple}Print this help.${echo_normal}
|
||||||
|
--refresh ${echo_purple}Force a refresh of the search cache.${echo_normal}
|
||||||
|
--no-color ${echo_purple}Disable color output and use monochrome text.${echo_normal}
|
||||||
|
|
||||||
|
${echo_underline_yellow}EXAMPLES${echo_normal}
|
||||||
|
|
||||||
|
For example, ${echo_bold_green}bash-it search git${echo_normal} would match any alias, completion
|
||||||
|
or plugin that has the word 'git' in either the module name or
|
||||||
|
it's description. You should see something like this when you run this
|
||||||
|
command:
|
||||||
|
|
||||||
|
${echo_bold_green}❯ bash-it search git${echo_bold_blue}
|
||||||
|
${echo_bold_yellow}aliases: ${echo_bold_green}git ${echo_normal}gitsvn
|
||||||
|
${echo_bold_yellow}plugins: ${echo_normal}autojump fasd ${echo_bold_green}git ${echo_normal}git-subrepo jgitflow jump
|
||||||
|
${echo_bold_yellow}completions: ${echo_bold_green}git ${echo_normal}git_flow git_flow_avh${echo_normal}
|
||||||
|
|
||||||
|
You can exclude some terms by prefixing a term with a minus, eg:
|
||||||
|
|
||||||
|
${echo_bold_green}❯ bash-it search git -flow -svn${echo_bold_blue}
|
||||||
|
${echo_bold_yellow}aliases: ${echo_normal}git
|
||||||
|
${echo_bold_yellow}plugins: ${echo_normal}autojump fasd git git-subrepo jump
|
||||||
|
${echo_bold_yellow}completions: ${echo_normal}git${echo_normal}
|
||||||
|
|
||||||
|
Finally, if you prefix a term with '@' symbol, that indicates an exact
|
||||||
|
match. Note, that we also pass the '--enable' flag, which would ensure
|
||||||
|
that all matches are automatically enabled. The example is below:
|
||||||
|
|
||||||
|
${echo_bold_green}❯ bash-it search @git --enable${echo_bold_blue}
|
||||||
|
${echo_bold_yellow}aliases: ${echo_normal}git
|
||||||
|
${echo_bold_yellow}plugins: ${echo_normal}git
|
||||||
|
${echo_bold_yellow}completions: ${echo_normal}git${echo_normal}
|
||||||
|
|
||||||
|
${echo_underline_yellow}SUMMARY${echo_normal}
|
||||||
|
|
||||||
|
Take advantage of the search functionality to discover what Bash-It can do
|
||||||
|
for you. Try searching for partial term matches, mix and match with the
|
||||||
|
negative terms, or specify an exact matches of any number of terms. Once
|
||||||
|
you created the search command that returns ONLY the modules you need,
|
||||||
|
simply append '--enable' or '--disable' at the end to activate/deactivate
|
||||||
|
each module.
|
||||||
|
|
||||||
|
"
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-cache-file() {
|
||||||
|
local component="${1}"
|
||||||
|
local file="/tmp/bash_it/${component}.status"
|
||||||
|
mkdir -p $(dirname ${file})
|
||||||
|
printf ${file}
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash_it_search_cache_clean() {
|
||||||
|
local component="$1"
|
||||||
|
if [[ -z ${component} ]] ; then
|
||||||
|
for component in "${BASH_IT_COMPONENTS[@]}" ; do
|
||||||
|
_bash_it_search_cache_clean "${component}"
|
||||||
|
done
|
||||||
|
else
|
||||||
|
rm -f $(_bash-it-cache-file ${component})
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
#———————————————————————————————————————————————————————————————————————————————
|
#———————————————————————————————————————————————————————————————————————————————
|
||||||
|
|
@ -58,137 +185,261 @@ _bash-it-array-contains-element () {
|
||||||
echo -n $r
|
echo -n $r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_bash-it-array-dedup() {
|
||||||
|
echo "$*" | tr ' ' '\n' | sort -u | tr '\n' ' '
|
||||||
|
}
|
||||||
|
|
||||||
|
_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}"
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-is-partial-match() {
|
||||||
|
local component="$1"
|
||||||
|
local term="$2"
|
||||||
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -i -q -- "${term}"
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-component-term-matches-negation() {
|
||||||
|
local match="$1"; shift
|
||||||
|
local negative
|
||||||
|
for negative in "$@"; do
|
||||||
|
[[ "${match}" =~ "${negative}" ]] && return 0
|
||||||
|
done
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-component-help() {
|
||||||
|
local component="$1"
|
||||||
|
local file=$(_bash-it-cache-file ${component})
|
||||||
|
if [[ ! -s "${file}" || -z $(find "${file}" -mmin -2) ]] ; then
|
||||||
|
rm -f "${file}" 2>/dev/null
|
||||||
|
local func="_bash-it-${component}"
|
||||||
|
${func} | $(_bash-it-grep) -E ' \[' | cat > ${file}
|
||||||
|
fi
|
||||||
|
cat "${file}"
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-component-list() {
|
||||||
|
local component="$1"
|
||||||
|
_bash-it-component-help "${component}" | awk '{print $1}' | uniq | sort | tr '\n' ' '
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-component-list-matching() {
|
||||||
|
local component="$1"; shift
|
||||||
|
local term="$1"
|
||||||
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -- "${term}" | awk '{print $1}' | sort | uniq
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-component-list-enabled() {
|
||||||
|
local component="$1"
|
||||||
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' '
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-component-list-disabled() {
|
||||||
|
local component="$1"
|
||||||
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' '
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-component-item-is-enabled() {
|
||||||
|
local component="$1"
|
||||||
|
local item="$2"
|
||||||
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s"
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-component-item-is-disabled() {
|
||||||
|
local component="$1"
|
||||||
|
local item="$2"
|
||||||
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
_bash-it-search-component() {
|
_bash-it-search-component() {
|
||||||
_about 'searches for given terms amongst a given component'
|
local component="$1"
|
||||||
_param '1: component type, one of: [ aliases | plugins | completions ]'
|
|
||||||
_param '2: term1 '
|
|
||||||
_param '3: [-]term2 [-]term3 ...'
|
|
||||||
_example '$ _bash-it-search-component aliases rake bundler -chruby'
|
|
||||||
|
|
||||||
_component=$1
|
|
||||||
|
|
||||||
local func=_bash-it-${_component}
|
|
||||||
local help=$($func)
|
|
||||||
shift
|
shift
|
||||||
|
|
||||||
|
_about 'searches for given terms amongst a given component'
|
||||||
|
_param '1: component type, one of: [ aliases | plugins | completions ]'
|
||||||
|
_param '2: term1 term2 @term3'
|
||||||
|
_param '3: [-]term4 [-]term5 ...'
|
||||||
|
_example '$ _bash-it-search-component aliases @git rake bundler -chruby'
|
||||||
|
|
||||||
# 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 action action_func component_singular
|
local component_singular action action_func
|
||||||
declare -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}" "$@") == "true" ]]; then
|
if [[ $(_bash-it-array-contains-element "--${search_command}" "$@") == "true" ]]; then
|
||||||
action=$_search_command
|
component_singular=${component}
|
||||||
component_singular=${_component}
|
|
||||||
component_singular=${component_singular/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_func="_${action}-${component_singular}"
|
action_func="_${action}-${component_singular}"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
local _grep=$((which --skip-alias grep 2> /dev/null || which grep) | tail -n 1)
|
local -a terms=($@) # passed on the command line
|
||||||
|
|
||||||
declare -a terms=($@) # passed on the command line
|
unset exact_terms
|
||||||
declare -a matches=() # results that we found
|
unset partial_terms
|
||||||
declare -a negative_terms=() # terms that began with a dash
|
unset negative_terms
|
||||||
|
|
||||||
|
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 negative_terms=() # negated partial terms that should be excluded
|
||||||
|
|
||||||
|
unset component_list
|
||||||
|
local -a component_list=( $(_bash-it-component-list "${component}") )
|
||||||
|
local term
|
||||||
|
|
||||||
for term in "${terms[@]}"; do
|
for term in "${terms[@]}"; do
|
||||||
# -- can only be used for the actions: enable/disable
|
local search_term="${term:1}"
|
||||||
[[ "${term:0:2}" == "--" ]] && continue
|
if [[ "${term:0:2}" == "--" ]] ; then
|
||||||
[[ "${term:0:1}" == "-" ]] && negative_terms=(${negative_terms[@]} ${term:1}) && continue
|
continue
|
||||||
|
elif [[ "${term:0:1}" == "-" ]] ; then
|
||||||
# print asterisk next to each result that is already enabled by the user
|
negative_terms=(${negative_terms[@]} "${search_term}")
|
||||||
local term_match=($(echo "${help}"| ${_grep} -i -- ${term} | ${_grep} -E '\[( |x)\]' | cut -b -30 | sed 's/ *\[ \]//g;s/ *\[x\]/*/g;' ))
|
elif [[ "${term:0:1}" == "@" ]] ; then
|
||||||
[[ "${#term_match[@]}" -gt 0 ]] && {
|
if [[ $(_bash-it-array-contains-element "${search_term}" "${component_list[@]}") == "true" ]]; then
|
||||||
matches=(${matches[@]} ${term_match[@]}) # append to the list of results
|
exact_terms=(${exact_terms[@]} "${search_term}")
|
||||||
}
|
fi
|
||||||
|
else
|
||||||
|
partial_terms=(${partial_terms[@]} $(_bash-it-component-list-matching "${component}" "${term}") )
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# now check if we found any negative terms, and subtract them
|
local -a total_matches=( $(_bash-it-array-dedup ${exact_terms[@]} ${partial_terms[@]}) )
|
||||||
[[ ${#negative_terms} -gt 0 ]] && {
|
|
||||||
declare -a filtered_matches=()
|
|
||||||
for match in "${matches[@]}"; do
|
|
||||||
local negations=0
|
|
||||||
for nt in "${negative_terms[@]}"; do
|
|
||||||
[[ "${match}" =~ "${nt}" ]] && negations=$(($negations+1))
|
|
||||||
done
|
|
||||||
[[ $negations -eq 0 ]] && filtered_matches=(${filtered_matches[@]} ${match})
|
|
||||||
done
|
|
||||||
matches=(${filtered_matches[@]})
|
|
||||||
}
|
|
||||||
|
|
||||||
_bash-it-search-result $action $action_func
|
unset matches
|
||||||
|
declare -a matches=()
|
||||||
unset matches filtered_matches terms
|
for match in ${total_matches[@]}; do
|
||||||
|
local include_match=true
|
||||||
|
if [[ ${#negative_terms[@]} -gt 0 ]]; then
|
||||||
|
( _bash-it-component-term-matches-negation "${match}" "${negative_terms[@]}" ) && include_match=false
|
||||||
|
fi
|
||||||
|
( ${include_match} ) && matches=(${matches[@]} "${match}")
|
||||||
|
done
|
||||||
|
_bash-it-search-result "${component}" "${action}" "${action_func}" "${matches[@]}"
|
||||||
|
unset matches final_matches terms
|
||||||
}
|
}
|
||||||
|
|
||||||
_bash-it-search-result() {
|
_bash-it-search-result() {
|
||||||
local action=$1; shift
|
local component="$1"; shift
|
||||||
local action_func=$1; shift
|
local action="$1"; 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
|
||||||
|
|
||||||
[[ -z "$NO_COLOR" ]] && {
|
color_sep=':'
|
||||||
|
|
||||||
|
( ${BASH_IT_SEARCH_USE_COLOR} ) && {
|
||||||
color_component='\e[1;34m'
|
color_component='\e[1;34m'
|
||||||
color_enable='\e[1;32m'
|
color_enable='\e[1;32m'
|
||||||
|
suffix_enable=''
|
||||||
|
suffix_disable=''
|
||||||
color_disable='\e[0;0m'
|
color_disable='\e[0;0m'
|
||||||
color_off='\e[0;0m'
|
color_off='\e[0;0m'
|
||||||
color_sep=':'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[ -n "$NO_COLOR" ]] && {
|
( ${BASH_IT_SEARCH_USE_COLOR} ) || {
|
||||||
color_component=''
|
color_component=''
|
||||||
color_sep=' => '
|
suffix_enable=' ✓ ︎'
|
||||||
color_enable='✓'
|
suffix_disable=' '
|
||||||
|
color_enable=''
|
||||||
color_disable=''
|
color_disable=''
|
||||||
color_off=''
|
color_off=''
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ "${#matches[*]}" -gt 0 ]] ; then
|
local match
|
||||||
printf "${color_component}%13s${color_sep} ${color_off}" "${_component}"
|
local modified=0
|
||||||
|
|
||||||
sorted_matches=($(echo "${matches[*]}" | tr ' ' '\n' | sort | uniq))
|
local flag="DEFER_CACHE_CLEANUP_FOR_${component}"
|
||||||
|
eval "export ${flag}=true"
|
||||||
|
|
||||||
for match in "${sorted_matches[@]}"; do
|
if [[ "${#matches[@]}" -gt 0 ]] ; then
|
||||||
local match_color compatible_action
|
printf "${color_component}%13s${color_sep} ${color_off}" "${component}"
|
||||||
if [[ $match =~ "*" ]]; then
|
|
||||||
match_color=$color_enable
|
for match in "${matches[@]}"; do
|
||||||
|
local enabled=0
|
||||||
|
( _bash-it-component-item-is-enabled "${component}" "${match}" ) && enabled=1
|
||||||
|
|
||||||
|
local match_color compatible_action suffix opposite_suffix
|
||||||
|
|
||||||
|
(( ${enabled} )) && {
|
||||||
|
match_color=${color_enable}
|
||||||
|
suffix=${suffix_enable}
|
||||||
|
opposite_suffix=${suffix_disable}
|
||||||
compatible_action="disable"
|
compatible_action="disable"
|
||||||
else
|
}
|
||||||
match_color=$color_disable
|
|
||||||
compatible_action="enable"
|
|
||||||
fi
|
|
||||||
|
|
||||||
match_value=${match/\*/} # remove asterisk
|
(( ${enabled} )) || {
|
||||||
len=${#match_value}
|
match_color=${color_disable}
|
||||||
if [[ -n $NO_COLOR ]]; then
|
suffix=${suffix_disable}
|
||||||
local m="${match_color}${match_value}"
|
opposite_suffix=${suffix_enable}
|
||||||
|
compatible_action="enable"
|
||||||
|
}
|
||||||
|
|
||||||
|
local len
|
||||||
|
if ( ${BASH_IT_SEARCH_USE_COLOR} ); then
|
||||||
|
local m="${match_color}${match}${suffix}"
|
||||||
|
len=${#m}
|
||||||
|
else
|
||||||
|
local m="${match}${suffix}"
|
||||||
len=${#m}
|
len=${#m}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf " ${match_color}${match_value}" # print current state
|
printf " ${match_color}${match}${suffix}" # print current state
|
||||||
|
|
||||||
if [[ "${action}" == "${compatible_action}" ]]; then
|
if [[ "${action}" == "${compatible_action}" ]]; then
|
||||||
# oh, i see – we need to either disable enabled, or enable disabled
|
if [[ ${action} == "enable" && ${BASH_IT_SEARCH_USE_COLOR} == false ]]; then
|
||||||
# component. Let's start with the most important part: redrawing
|
_bash-it-flash-term ${len} "${match}${suffix}"
|
||||||
# the search result backwards. Because style.
|
else
|
||||||
|
_bash-it-erase-term ${len}
|
||||||
printf "\033[${len}D"
|
fi
|
||||||
for a in {0..30}; do
|
modified=1
|
||||||
[[ $a -gt $len ]] && break
|
result=$(${action_func} ${match})
|
||||||
printf "%.*s" $a " "
|
|
||||||
sleep 0.07 # who knew you could sleep for fraction of the cost :)
|
|
||||||
done
|
|
||||||
printf "\033[${len}D"
|
|
||||||
result=$(${action_func} ${match_value})
|
|
||||||
local temp="color_${compatible_action}"
|
local temp="color_${compatible_action}"
|
||||||
match_color=${!temp}
|
match_color=${!temp}
|
||||||
printf "${match_color}${match_value}"
|
_bash-it-rewind ${len}
|
||||||
|
printf "${match_color}${match}${opposite_suffix}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "${color_off}"
|
printf "${color_off}"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
[[ ${modified} -gt 0 ]] && _bash_it_search_cache_clean ${component}
|
||||||
printf "\n"
|
printf "\n"
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-rewind() {
|
||||||
|
local len="$1"
|
||||||
|
printf "\033[${len}D"
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-flash-term() {
|
||||||
|
local len="$1"
|
||||||
|
local match="$2"
|
||||||
|
local delay=0.1
|
||||||
|
local color
|
||||||
|
|
||||||
|
for color in ${text_black} ${echo_bold_blue} ${bold_yellow} ${bold_red} ${echo_bold_green} ; do
|
||||||
|
sleep ${delay}
|
||||||
|
_bash-it-rewind "${len}"
|
||||||
|
printf "${color}${match}"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
_bash-it-erase-term() {
|
||||||
|
local len="$1"
|
||||||
|
_bash-it-rewind ${len}
|
||||||
|
for a in {0..30}; do
|
||||||
|
[[ ${a} -gt ${len} ]] && break
|
||||||
|
printf "%.*s" $a " "
|
||||||
|
sleep 0.05
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
load ../test_helper
|
load ../test_helper
|
||||||
load ../../lib/composure
|
load ../../lib/composure
|
||||||
|
load ../../lib/search
|
||||||
load ../../plugins/available/base.plugin
|
load ../../plugins/available/base.plugin
|
||||||
|
|
||||||
cite _about _param _example _group _author _version
|
cite _about _param _example _group _author _version
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,20 @@
|
||||||
#!/usr/bin/env bats
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
load ../test_helper
|
load ../test_helper
|
||||||
|
|
||||||
load ../../lib/composure
|
load ../../lib/composure
|
||||||
|
load ../../lib/helpers
|
||||||
|
load ../../lib/search
|
||||||
load ../../plugins/available/base.plugin
|
load ../../plugins/available/base.plugin
|
||||||
|
load ../../aliases/available/git.aliases
|
||||||
|
load ../../plugins/available/ruby.plugin
|
||||||
|
load ../../plugins/available/rails.plugin
|
||||||
|
load ../../completion/available/bundler.completion
|
||||||
|
load ../../completion/available/gem.completion
|
||||||
|
load ../../completion/available/rake.completion
|
||||||
|
|
||||||
cite _about _param _example _group _author _version
|
cite _about _param _example _group _author _version
|
||||||
|
|
||||||
load ../../lib/helpers
|
load ../../lib/helpers
|
||||||
load ../../lib/search
|
|
||||||
|
|
||||||
NO_COLOR=true
|
|
||||||
|
|
||||||
function local_setup {
|
function local_setup {
|
||||||
mkdir -p "$BASH_IT"
|
mkdir -p "$BASH_IT"
|
||||||
|
|
@ -23,46 +27,68 @@ function local_setup {
|
||||||
rm -rf "$BASH_IT"/aliases/enabled
|
rm -rf "$BASH_IT"/aliases/enabled
|
||||||
rm -rf "$BASH_IT"/completion/enabled
|
rm -rf "$BASH_IT"/completion/enabled
|
||||||
rm -rf "$BASH_IT"/plugins/enabled
|
rm -rf "$BASH_IT"/plugins/enabled
|
||||||
|
|
||||||
|
mkdir -p "$BASH_IT"/enabled
|
||||||
|
mkdir -p "$BASH_IT"/aliases/enabled
|
||||||
|
mkdir -p "$BASH_IT"/completion/enabled
|
||||||
|
mkdir -p "$BASH_IT"/plugins/enabled
|
||||||
|
|
||||||
|
export OLD_PATH="$PATH"
|
||||||
|
export PATH="/usr/bin:/bin:/usr/sbin"
|
||||||
|
}
|
||||||
|
|
||||||
|
function local_teardown {
|
||||||
|
export PATH="$OLD_PATH"
|
||||||
|
unset OLD_PATH
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "search: plugin base" {
|
@test "search: plugin base" {
|
||||||
|
export BASH_IT_SEARCH_USE_COLOR=false
|
||||||
run _bash-it-search-component 'plugins' 'base'
|
run _bash-it-search-component 'plugins' 'base'
|
||||||
[[ "${lines[0]}" =~ 'plugins' && "${lines[0]}" =~ 'base' ]]
|
assert_line -n 0 ' plugins: base '
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "search: git" {
|
||||||
|
run _bash-it-search 'git' --no-color
|
||||||
|
assert_line -n 0 ' aliases: git gitsvn '
|
||||||
|
assert_line -n 1 ' plugins: autojump fasd git git-subrepo jgitflow jump '
|
||||||
|
assert_line -n 2 ' completions: git git_flow git_flow_avh '
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "search: ruby gem bundle rake rails" {
|
@test "search: ruby gem bundle rake rails" {
|
||||||
# first disable them all, so that the output does not appear with a checkbox
|
run _bash-it-search rails ruby gem bundler rake --no-color
|
||||||
# and we can compare the result
|
|
||||||
run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable'
|
assert_line -n 0 ' aliases: bundler rails '
|
||||||
# Now perform the search
|
assert_line -n 1 ' plugins: chruby chruby-auto rails ruby '
|
||||||
run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails'
|
assert_line -n 2 ' completions: bundler gem rake '
|
||||||
# And verify
|
|
||||||
assert [ "${lines[0]/✓/}" == ' aliases => bundler rails' ]
|
|
||||||
assert [ "${lines[1]/✓/}" == ' plugins => chruby chruby-auto rails ruby' ]
|
|
||||||
assert [ "${lines[2]/✓/}" == ' completions => bundler gem rake' ]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "search: ruby gem bundle -chruby rake rails" {
|
@test "search: rails ruby gem bundler rake -chruby" {
|
||||||
run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable'
|
run _bash-it-search rails ruby gem bundler rake -chruby --no-color
|
||||||
run _bash-it-search 'ruby' 'gem' 'bundle' '-chruby' 'rake' 'rails'
|
|
||||||
assert [ "${lines[0]/✓/}" == ' aliases => bundler rails' ]
|
assert_line -n 0 ' aliases: bundler rails '
|
||||||
assert [ "${lines[1]/✓/}" == ' plugins => rails ruby' ]
|
assert_line -n 1 ' plugins: rails ruby '
|
||||||
assert [ "${lines[2]/✓/}" == ' completions => bundler gem rake' ]
|
assert_line -n 2 ' completions: bundler gem rake '
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "search: (rails enabled) ruby gem bundle rake rails" {
|
@test "search: @git" {
|
||||||
run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable'
|
run _bash-it-search '@git' --no-color
|
||||||
run _enable-alias 'rails'
|
assert_line -n 0 ' aliases: git '
|
||||||
run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails'
|
assert_line -n 1 ' plugins: git '
|
||||||
assert_line -n 0 ' aliases => bundler ✓rails'
|
assert_line -n 2 ' completions: git '
|
||||||
assert_line -n 1 ' plugins => chruby chruby-auto rails ruby'
|
|
||||||
assert_line -n 2 ' completions => bundler gem rake'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "search: (all enabled) ruby gem bundle rake rails" {
|
@test "search: @git --enable / --disable" {
|
||||||
run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' '-chruby' 'rails' '--enable'
|
set -e
|
||||||
run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' '-chruby' 'rails'
|
run _bash-it-search '@git' --enable --no-color
|
||||||
assert_line -n 0 ' aliases => ✓bundler ✓rails'
|
run _bash-it-search '@git' --no-color
|
||||||
assert_line -n 1 ' plugins => ✓rails ✓ruby'
|
|
||||||
assert_line -n 2 ' completions => ✓bundler ✓gem ✓rake'
|
[[ "${lines[0]}" =~ '✓' ]]
|
||||||
|
|
||||||
|
run _bash-it-search '@git' --disable --no-color
|
||||||
|
run _bash-it-search '@git' --no-color
|
||||||
|
|
||||||
|
assert_line -n 0 ' aliases: git '
|
||||||
|
assert_line -n 0 ' aliases: git '
|
||||||
|
assert_line -n 2 ' completions: git '
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue