From ffbeeacc54ae4b8dbaebd213caedf6d8ffd43add Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Sat, 7 May 2016 04:19:03 -0700 Subject: [PATCH 1/6] Moving search into its own module before more work --- lib/helpers.bash | 54 -------------------------- lib/search.bash | 54 ++++++++++++++++++++++++++ test/lib/{helpers.bats => search.bats} | 1 + 3 files changed, 55 insertions(+), 54 deletions(-) create mode 100644 lib/search.bash rename test/lib/{helpers.bats => search.bats} (96%) diff --git a/lib/helpers.bash b/lib/helpers.bash index 775eb43f..6d7104dd 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -146,60 +146,6 @@ _bash-it_update() { cd - &> /dev/null } -# This function returns list of aliases, plugins and completions in bash-it, -# whose name or description matches one of the search terms provided as arguments. -# -# Usage: -# ❯ bash-it search term1 [term2]... -# Example: -# ❯ bash-it search ruby rbenv rvm gem rake -# aliases: bundler -# plugins: chruby chruby-auto rbenv ruby rvm -# completions: gem rake -# - -_bash-it-search() { - _about 'searches for given terms amongst bash-it plugins, aliases and completions' - _param '1: term1' - _param '2: [ term2 ]...' - _example '$ _bash-it-search ruby rvm rake bundler' - - declare -a _components=(aliases plugins completions) - for _component in "${_components[@]}" ; do - _bash-it-search-component "${_component}" "$*" - done -} - -_bash-it-search-component() { - _about 'searches for given terms amongst a given component' - _param '1: component type, one of: [ aliases | plugins | completions ]' - _param '2: term1' - _param '3: [ term2 ]...' - _example '$ _bash-it-search-component aliases rake bundler' - - _component=$1 - local func=_bash-it-${_component} - local help=$($func) - - shift - declare -a terms=($@) - declare -a matches=() - local _grep=$(which egrep || which grep) - for term in "${terms[@]}"; do - local term_match=($(echo "${help}"| ${_grep} -i -- ${term} | cut -d ' ' -f 1 | tr '\n' ' ')) - [[ "${#term_match[@]}" -gt 0 ]] && { - matches=(${matches[@]} ${term_match[@]}) - } - done - [[ -n "$NO_COLOR" && color_on="" ]] || color_on="\e[1;32m" - [[ -n "$NO_COLOR" && color_off="" ]] || color_off="\e[0;0m" - - if [[ "${#matches[*]}" -gt 0 ]] ; then - printf "%-12s: ${color_on}%s${color_off}\n" "${_component}" "$(echo -n ${matches[*]} | tr ' ' '\n' | sort | uniq | tr '\n' ' ' | sed 's/ $//g')" - fi - unset matches -} - _bash-it-describe () { _about 'summarizes available bash_it components' diff --git a/lib/search.bash b/lib/search.bash new file mode 100644 index 00000000..5d7d9bb0 --- /dev/null +++ b/lib/search.bash @@ -0,0 +1,54 @@ + +# This function returns list of aliases, plugins and completions in bash-it, +# whose name or description matches one of the search terms provided as arguments. +# +# Usage: +# ❯ bash-it search term1 [term2]... +# Example: +# ❯ bash-it search ruby rbenv rvm gem rake +# aliases: bundler +# plugins: chruby chruby-auto rbenv ruby rvm +# completions: gem rake +# + +_bash-it-search() { + _about 'searches for given terms amongst bash-it plugins, aliases and completions' + _param '1: term1' + _param '2: [ term2 ]...' + _example '$ _bash-it-search ruby rvm rake bundler' + + declare -a _components=(aliases plugins completions) + for _component in "${_components[@]}" ; do + _bash-it-search-component "${_component}" "$*" + done +} + +_bash-it-search-component() { + _about 'searches for given terms amongst a given component' + _param '1: component type, one of: [ aliases | plugins | completions ]' + _param '2: term1' + _param '3: [ term2 ]...' + _example '$ _bash-it-search-component aliases rake bundler' + + _component=$1 + local func=_bash-it-${_component} + local help=$($func) + + shift + declare -a terms=($@) + declare -a matches=() + local _grep=$(which egrep || which grep) + for term in "${terms[@]}"; do + local term_match=($(echo "${help}"| ${_grep} -i -- ${term} | cut -d ' ' -f 1 | tr '\n' ' ')) + [[ "${#term_match[@]}" -gt 0 ]] && { + matches=(${matches[@]} ${term_match[@]}) + } + done + [[ -n "$NO_COLOR" && color_on="" ]] || color_on="\e[1;32m" + [[ -n "$NO_COLOR" && color_off="" ]] || color_off="\e[0;0m" + + if [[ "${#matches[*]}" -gt 0 ]] ; then + printf "%-12s: ${color_on}%s${color_off}\n" "${_component}" "$(echo -n ${matches[*]} | tr ' ' '\n' | sort | uniq | tr '\n' ' ' | sed 's/ $//g')" + fi + unset matches +} diff --git a/test/lib/helpers.bats b/test/lib/search.bats similarity index 96% rename from test/lib/helpers.bats rename to test/lib/search.bats index af9ae6b6..7a3efc84 100644 --- a/test/lib/helpers.bats +++ b/test/lib/search.bats @@ -6,6 +6,7 @@ load ../../plugins/available/base.plugin cite _about _param _example _group _author _version load ../../lib/helpers +load ../../lib/search NO_COLOR=true From fb6a6c80c00dd4a20889cbeedcb04ecfd33a45f8 Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Sat, 7 May 2016 04:23:03 -0700 Subject: [PATCH 2/6] Highlighting color of enabled components --- lib/helpers.bash | 0 lib/history.bash | 0 lib/search.bash | 79 ++++++++++++++++++++++++++++++++++++++------ test/lib/search.bats | 6 ++-- 4 files changed, 72 insertions(+), 13 deletions(-) mode change 100755 => 100644 lib/helpers.bash mode change 100755 => 100644 lib/history.bash diff --git a/lib/helpers.bash b/lib/helpers.bash old mode 100755 new mode 100644 diff --git a/lib/history.bash b/lib/history.bash old mode 100755 new mode 100644 diff --git a/lib/search.bash b/lib/search.bash index 5d7d9bb0..1d7b302f 100644 --- a/lib/search.bash +++ b/lib/search.bash @@ -1,4 +1,3 @@ - # This function returns list of aliases, plugins and completions in bash-it, # whose name or description matches one of the search terms provided as arguments. # @@ -18,11 +17,23 @@ _bash-it-search() { _example '$ _bash-it-search ruby rvm rake bundler' declare -a _components=(aliases plugins completions) + for _component in "${_components[@]}" ; do - _bash-it-search-component "${_component}" "$*" + _bash-it-search-component "${_component}" "$@" done } +# +# array=("something to search for" "a string" "test2000") +# _bash-it-array-contains-element "a string" "${array[@]}" +# ( prints "true" or "false" ) +_bash-it-array-contains-element () { + local e + local r=false + for e in "${@:2}"; do [[ "$e" == "$1" ]] && r=true; done + echo -n $r +} + _bash-it-search-component() { _about 'searches for given terms amongst a given component' _param '1: component type, one of: [ aliases | plugins | completions ]' @@ -31,24 +42,72 @@ _bash-it-search-component() { _example '$ _bash-it-search-component aliases rake bundler' _component=$1 + local func=_bash-it-${_component} local help=$($func) - shift + + # if one of the search terms is --enable or --disable, we will apply + # this action to the result set interactively. + local cmd action component_singular + declare -a _search_commands=(enable disable) + for _search_command in "${_search_commands[@]}"; do + if [[ $(_bash-it-array-contains-element "--${_search_command}" "$@") == "true" ]]; then + cmd=$_search_command + component_singular=${_component} + component_singular=${component_singular/es/} # aliases -> alias + component_singular=${component_singular/ns/n} # plugins -> plugin + action="_${cmd}-${component_singular}" + break + fi + done + declare -a terms=($@) declare -a matches=() local _grep=$(which egrep || which grep) for term in "${terms[@]}"; do - local term_match=($(echo "${help}"| ${_grep} -i -- ${term} | cut -d ' ' -f 1 | tr '\n' ' ')) + if [[ "${term}" =~ "--" ]]; then + continue + fi + # print asterisk next to each command that is already enabled + local term_match=($(echo "${help}"| ${_grep} -i -- ${term} | cut -b -30 | sed 's/ *\[ \]//g;s/ *\[x\]/*/g;' )) [[ "${#term_match[@]}" -gt 0 ]] && { matches=(${matches[@]} ${term_match[@]}) } done - [[ -n "$NO_COLOR" && color_on="" ]] || color_on="\e[1;32m" - [[ -n "$NO_COLOR" && color_off="" ]] || color_off="\e[0;0m" - - if [[ "${#matches[*]}" -gt 0 ]] ; then - printf "%-12s: ${color_on}%s${color_off}\n" "${_component}" "$(echo -n ${matches[*]} | tr ' ' '\n' | sort | uniq | tr '\n' ' ' | sed 's/ $//g')" - fi + _bash-it-search-result $action # "${matches[@]}" unset matches } + +_bash-it-search-result() { + local action=shift + local color_component color_enable color_disable color_off + + [[ -z "$NO_COLOR" ]] && { + color_component='\e[0;33m' + color_enable='\e[1;32m' + color_disable='\e[0;0m' + color_off='\e[0;0m' + } + + [[ -n "$NO_COLOR" ]] && { + color_enable='*' + } + + if [[ "${#matches[*]}" -gt 0 ]] ; then + printf "${color_component}%-12s:${color_off} " "${_component}" + sorted_matches=($(echo "${matches[*]}" | tr ' ' '\n' | sort | uniq)) + for match in "${sorted_matches[@]}"; do + local match_color + if [[ $match =~ "*" ]]; then + match_color=$color_enable + else + match_color=$color_disable + fi + printf " ${match_color}${match/\*/}${color_off}" + done + + printf "\n" + fi + +} diff --git a/test/lib/search.bats b/test/lib/search.bats index 7a3efc84..9baede99 100644 --- a/test/lib/search.bats +++ b/test/lib/search.bats @@ -17,7 +17,7 @@ NO_COLOR=true @test "helpers search all ruby et al" { run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' - [[ "${lines[0]}" == 'aliases : bundler rails' ]] - [[ "${lines[1]}" == 'plugins : chruby chruby-auto ruby' ]] - [[ "${lines[2]}" == 'completions : bundler gem rake' ]] + [[ "${lines[0]/\*/}" == 'aliases : bundler rails' ]] + [[ "${lines[1]/\*/}" == 'plugins : chruby chruby-auto ruby' ]] + [[ "${lines[2]/\*/}" == 'completions : bundler gem rake' ]] } From 9bb2d377a86fe1a1525b4748c6b7f03eb9190445 Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Sat, 7 May 2016 06:26:30 -0700 Subject: [PATCH 3/6] Enable/Disable with negated search feature --- lib/helpers.bash | 2 +- lib/search.bash | 127 +++++++++++++++++++++++++++++++++---------- test/lib/search.bats | 15 ++++- 3 files changed, 112 insertions(+), 32 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index 6d7104dd..c95f8d96 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -40,7 +40,7 @@ bash-it () example '$ bash-it enable plugin git [tmux]...' example '$ bash-it disable alias hg [tmux]...' example '$ bash-it update' - example '$ bash-it search ruby [rake]...' + example '$ bash-it search ruby [[-]rake]... [--enable | --disable]' typeset verb=${1:-} shift typeset component=${1:-} diff --git a/lib/search.bash b/lib/search.bash index 1d7b302f..f5b66096 100644 --- a/lib/search.bash +++ b/lib/search.bash @@ -1,13 +1,27 @@ +# +# Search by Konstantin Gredeskoul «github.com/kigster» +#——————————————————————————————————————————————————————————————————————————————— # This function returns list of aliases, plugins and completions in bash-it, # whose name or description matches one of the search terms provided as arguments. # # Usage: -# ❯ bash-it search term1 [term2]... +# ❯ bash-it search term1 [[-]term2] ... [[-]termN] [ --enable | --disable ] +# +# Exmplanation: +# Single dash, as in "-chruby", indicates a negative search term. +# Double dash indicates a command that is to be applied to the search result. +# At the moment only --enable and --disable are supported. +# # Example: -# ❯ bash-it search ruby rbenv rvm gem rake -# aliases: bundler -# plugins: chruby chruby-auto rbenv ruby rvm -# completions: gem rake +# ❯ bash-it search ruby rbenv rvm gem rake +# aliases : bundler +# plugins : chruby chruby-auto rbenv ruby rvm +# completions : gem rake + +# ❯ bash-it search ruby rbenv rvm gem rake -chruby +# aliases : bundler +# plugins : rbenv ruby rvm +# completions : gem rake # _bash-it-search() { @@ -23,7 +37,7 @@ _bash-it-search() { done } -# +#——————————————————————————————————————————————————————————————————————————————— # array=("something to search for" "a string" "test2000") # _bash-it-array-contains-element "a string" "${array[@]}" # ( prints "true" or "false" ) @@ -37,9 +51,9 @@ _bash-it-array-contains-element () { _bash-it-search-component() { _about 'searches for given terms amongst a given component' _param '1: component type, one of: [ aliases | plugins | completions ]' - _param '2: term1' - _param '3: [ term2 ]...' - _example '$ _bash-it-search-component aliases rake bundler' + _param '2: term1 ' + _param '3: [-]term2 [-]term3 ...' + _example '$ _bash-it-search-component aliases rake bundler -chruby' _component=$1 @@ -48,63 +62,120 @@ _bash-it-search-component() { shift # if one of the search terms is --enable or --disable, we will apply - # this action to the result set interactively. - local cmd action component_singular + # this action to the matches further down. + local action action_func component_singular declare -a _search_commands=(enable disable) for _search_command in "${_search_commands[@]}"; do if [[ $(_bash-it-array-contains-element "--${_search_command}" "$@") == "true" ]]; then - cmd=$_search_command + action=$_search_command component_singular=${_component} component_singular=${component_singular/es/} # aliases -> alias component_singular=${component_singular/ns/n} # plugins -> plugin - action="_${cmd}-${component_singular}" + action_func="_${action}-${component_singular}" break fi done - declare -a terms=($@) - declare -a matches=() local _grep=$(which egrep || which grep) + + declare -a terms=($@) # passed on the command line + declare -a matches=() # results that we found + declare -a negative_terms=() # terms that began with a dash + for term in "${terms[@]}"; do - if [[ "${term}" =~ "--" ]]; then - continue - fi - # print asterisk next to each command that is already enabled + # -- can only be used for the actions: enable/disable + [[ "${term:0:2}" == "--" ]] && continue + [[ "${term:0:1}" == "-" ]] && negative_terms=(${negative_terms[@]} ${term:1}) && continue + + # print asterisk next to each result that is already enabled by the user local term_match=($(echo "${help}"| ${_grep} -i -- ${term} | cut -b -30 | sed 's/ *\[ \]//g;s/ *\[x\]/*/g;' )) [[ "${#term_match[@]}" -gt 0 ]] && { - matches=(${matches[@]} ${term_match[@]}) + matches=(${matches[@]} ${term_match[@]}) # append to the list of results } done - _bash-it-search-result $action # "${matches[@]}" - unset matches + + # now check if we found any negative terms, and subtract them + [[ ${#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 filtered_matches terms } _bash-it-search-result() { - local action=shift + local action=$1; shift + local action_func=$1; shift local color_component color_enable color_disable color_off [[ -z "$NO_COLOR" ]] && { - color_component='\e[0;33m' + color_component='\e[1;34m' color_enable='\e[1;32m' color_disable='\e[0;0m' color_off='\e[0;0m' + color_sep=':' } [[ -n "$NO_COLOR" ]] && { - color_enable='*' + color_component='' + color_sep=' => ' + color_enable='✓' + color_disable='' + color_off='' } if [[ "${#matches[*]}" -gt 0 ]] ; then - printf "${color_component}%-12s:${color_off} " "${_component}" + printf "${color_component}%13s${color_sep} ${color_off}" "${_component}" + sorted_matches=($(echo "${matches[*]}" | tr ' ' '\n' | sort | uniq)) + for match in "${sorted_matches[@]}"; do - local match_color + local match_color compatible_action if [[ $match =~ "*" ]]; then match_color=$color_enable + compatible_action="disable" else match_color=$color_disable + compatible_action="enable" fi - printf " ${match_color}${match/\*/}${color_off}" + + match_value=${match/\*/} # remove asterisk + len=${#match_value} + if [[ -n $NO_COLOR ]]; then + local m="${match_color}${match_value}" + len=${#m} + fi + + printf " ${match_color}${match_value}" # print current state + + if [[ "${action}" == "${compatible_action}" ]]; then + # oh, i see – we need to either disable enabled, or enable disabled + # component. Let's start with the most important part: redrawing + # the search result backwards. Because style. + + printf "\033[${len}D" + for a in {0..30}; do + [[ $a -gt $len ]] && break + 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}" + match_color=${!temp} + printf "${match_color}${match_value}" + fi + + printf "${color_off}" done printf "\n" diff --git a/test/lib/search.bats b/test/lib/search.bats index 9baede99..f991d2f5 100644 --- a/test/lib/search.bats +++ b/test/lib/search.bats @@ -17,7 +17,16 @@ NO_COLOR=true @test "helpers search all ruby et al" { run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' - [[ "${lines[0]/\*/}" == 'aliases : bundler rails' ]] - [[ "${lines[1]/\*/}" == 'plugins : chruby chruby-auto ruby' ]] - [[ "${lines[2]/\*/}" == 'completions : bundler gem rake' ]] + [[ "${lines[0]/✓/}" == ' aliases => bundler rails' ]] + [[ "${lines[1]/✓/}" == ' plugins => chruby chruby-auto ruby' ]] + [[ "${lines[2]/✓/}" == ' completions => bundler gem rake' ]] +} + +@test "search enable and disable" { + run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' + run _enable-alias 'rails' + run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' + [[ "${lines[0]}" == ' aliases => bundler ✓rails' ]] + [[ "${lines[1]}" == ' plugins => chruby chruby-auto ruby' ]] + [[ "${lines[2]}" == ' completions => bundler gem rake' ]] } From 1a02db13bc56e66279e936a7cca92a56cbb3dbdf Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Sat, 7 May 2016 06:39:40 -0700 Subject: [PATCH 4/6] Adding documentation --- README.md | 60 +++++++++++++++++++++++++++++++++++++++---------- lib/search.bash | 30 ++++++++++++++++--------- 2 files changed, 68 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index bd92e849..36779e83 100644 --- a/README.md +++ b/README.md @@ -56,25 +56,61 @@ bash-it help plugins # shows help for installed plugins ## Search If you need to quickly find out which of the plugins, aliases or completions -are available for a specific task or an environment, you can search for -multiple terms related to the commands you use frequently. Search will -find and output modules with the name or description matching the terms +are available for a specific framework, programming language, or an environment, you can _search_ for +multiple terms related to the commands you use frequently. Search will +find and print out modules with the name or description matching the terms provided. -``` -bash-it search term1 [term2] [term3].... +#### Syntax + +```bash + bash-it search term1 [[-]term2] [[-]term3].... ``` -For example, if you are a ruby developer, you might want to enable everything -related to the commands such as `ruby`, `rake`, `gem`, `bundler` and `rails`. -Search command helps you find related modules, so that you can decide which +As an example, a ruby developer might want to enable everything +related to the commands such as `ruby`, `rake`, `gem`, `bundler` and `rails`. +Search command helps you find related modules, so that you can decide which of them you'd like to use: +```bash +❯ bash-it search ruby rake gem bundle irb rails + aliases: bundler rails + plugins: chruby chruby-auto ruby + completions: bundler gem rake ``` -> bash-it search ruby rake gem bundle irb rails -aliases : bundler rails -plugins : chruby chruby-auto ruby -completions : bundler gem rake + +Currently enabled modules will be shown in green. + +#### Search with Negations + +You can prefix a search term with a "-" to exclude it from the results. In the above +example, if we wanted to hide `chruby` and `chruby-auto`, we could change the command as +follows: + +```bash +❯ bash-it search ruby rake gem bundle irb rails -chruby + aliases: bundler rails + plugins: ruby + completions: bundler gem rake +``` + +#### Using Search to Enable or Disable Components + +By adding a `--enable` or `--disable` to the search command, you can automatically +enable all modules that come up as a result of a search query. This could be quite +handy if you like to enable a bunch of components related to the same topic. + +#### Disabling ASCII Color + +To remove non-printing non-ASCII characters responsible for the coloring of the +search output, you can set environment variable `NO_COLOR`. Enabled components will +then be shown with a checkmark: + +```bash +❯ NO_COLOR=1 bash-it search ruby rake gem bundle irb rails -chruby + aliases => ✓bundler ✓rails + plugins => ✓ruby + completions => bundler gem rake ``` ## Your Custom scripts, aliases, themes, and functions diff --git a/lib/search.bash b/lib/search.bash index f5b66096..9afe3a3c 100644 --- a/lib/search.bash +++ b/lib/search.bash @@ -12,18 +12,28 @@ # Double dash indicates a command that is to be applied to the search result. # At the moment only --enable and --disable are supported. # -# Example: -# ❯ bash-it search ruby rbenv rvm gem rake -# aliases : bundler -# plugins : chruby chruby-auto rbenv ruby rvm -# completions : gem rake +# Examples: +# ❯ bash-it search ruby rbenv rvm gem rake +# aliases : bundler +# plugins : chruby chruby-auto rbenv ruby rvm +# completions : gem rake -# ❯ bash-it search ruby rbenv rvm gem rake -chruby -# aliases : bundler -# plugins : rbenv ruby rvm -# completions : gem rake +# ❯ bash-it search ruby rbenv rvm gem rake -chruby +# aliases : bundler +# plugins : rbenv ruby rvm +# completions : gem rake +# +# Examples of enabling or disabling results of the search: +# +# ❯ bash-it search ruby +# aliases => bundler +# plugins => chruby chruby-auto ruby +# +# ❯ bash-it search ruby -chruby --enable +# aliases => ✓bundler +# plugins => ✓ruby +# # - _bash-it-search() { _about 'searches for given terms amongst bash-it plugins, aliases and completions' _param '1: term1' From 81e3f1121b1cf1152eb682ac05ea9e08b86fbc3a Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Sat, 7 May 2016 06:42:48 -0700 Subject: [PATCH 5/6] Get the test in a known state --- test/lib/search.bats | 1 + 1 file changed, 1 insertion(+) diff --git a/test/lib/search.bats b/test/lib/search.bats index f991d2f5..35b68d86 100644 --- a/test/lib/search.bats +++ b/test/lib/search.bats @@ -16,6 +16,7 @@ NO_COLOR=true } @test "helpers search all ruby et al" { + run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' [[ "${lines[0]/✓/}" == ' aliases => bundler rails' ]] [[ "${lines[1]/✓/}" == ' plugins => chruby chruby-auto ruby' ]] From db5061cf804c7ebe6924b4a8f6d5725718e1c7b9 Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Thu, 12 May 2016 04:04:51 -0700 Subject: [PATCH 6/6] Better tests, more resilience and a bug fix --- lib/search.bash | 2 +- test/lib/helpers.bats | 10 ++++++++++ test/lib/search.bats | 34 +++++++++++++++++++++++++++------- 3 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 test/lib/helpers.bats diff --git a/lib/search.bash b/lib/search.bash index 9afe3a3c..aa9a13ea 100644 --- a/lib/search.bash +++ b/lib/search.bash @@ -98,7 +98,7 @@ _bash-it-search-component() { [[ "${term:0:1}" == "-" ]] && negative_terms=(${negative_terms[@]} ${term:1}) && continue # print asterisk next to each result that is already enabled by the user - local term_match=($(echo "${help}"| ${_grep} -i -- ${term} | cut -b -30 | sed 's/ *\[ \]//g;s/ *\[x\]/*/g;' )) + local term_match=($(echo "${help}"| ${_grep} -i -- ${term} | egrep '\[( |x)\]' | cut -b -30 | sed 's/ *\[ \]//g;s/ *\[x\]/*/g;' )) [[ "${#term_match[@]}" -gt 0 ]] && { matches=(${matches[@]} ${term_match[@]}) # append to the list of results } diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats new file mode 100644 index 00000000..4f3e4ffa --- /dev/null +++ b/test/lib/helpers.bats @@ -0,0 +1,10 @@ +#!/usr/bin/env bats + +load ../../lib/composure +load ../../plugins/available/base.plugin + +cite _about _param _example _group _author _version + +load ../../lib/helpers + +## TODO: write some tests. diff --git a/test/lib/search.bats b/test/lib/search.bats index 35b68d86..dc4ba59a 100644 --- a/test/lib/search.bats +++ b/test/lib/search.bats @@ -15,19 +15,39 @@ NO_COLOR=true [[ "${lines[0]}" =~ 'plugins' && "${lines[0]}" =~ 'base' ]] } -@test "helpers search all ruby et al" { +@test "helpers search ruby gem bundle rake rails" { + # first disable them all, so that the output does not appear with a checkbox + # and we can compoare the result run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' + # Now perform the search run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' - [[ "${lines[0]/✓/}" == ' aliases => bundler rails' ]] - [[ "${lines[1]/✓/}" == ' plugins => chruby chruby-auto ruby' ]] + # And verify + [[ "${lines[0]/✓/}" == ' aliases => bundler rails' ]] && \ + [[ "${lines[1]/✓/}" == ' plugins => chruby chruby-auto ruby' ]] && \ [[ "${lines[2]/✓/}" == ' completions => bundler gem rake' ]] } -@test "search enable and disable" { +@test "search ruby gem bundle -chruby rake rails" { + run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' + run _bash-it-search 'ruby' 'gem' 'bundle' '-chruby' 'rake' 'rails' + [[ "${lines[0]/✓/}" == ' aliases => bundler rails' ]] && \ + [[ "${lines[1]/✓/}" == ' plugins => ruby' ]] && \ + [[ "${lines[2]/✓/}" == ' completions => bundler gem rake' ]] +} + +@test "search (rails enabled) ruby gem bundle rake rails" { run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' run _enable-alias 'rails' run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' - [[ "${lines[0]}" == ' aliases => bundler ✓rails' ]] - [[ "${lines[1]}" == ' plugins => chruby chruby-auto ruby' ]] - [[ "${lines[2]}" == ' completions => bundler gem rake' ]] + [[ "${lines[0]}" == ' aliases => bundler ✓rails' ]] && \ + [[ "${lines[1]}" == ' plugins => chruby chruby-auto ruby' ]] && \ + [[ "${lines[2]}" == ' completions => bundler gem rake' ]] +} + +@test "search (all enabled) ruby gem bundle rake rails" { + run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' '-chruby' 'rails' '--enable' + run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' '-chruby' 'rails' + [[ "${lines[0]}" == ' aliases => ✓bundler ✓rails' ]] && \ + [[ "${lines[1]}" == ' plugins => ✓ruby' ]] && \ + [[ "${lines[2]}" == ' completions => ✓bundler ✓gem ✓rake' ]] }