From b0f0909717228cd7579b8a58698d2ba42f6cd8cb Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Sun, 20 Mar 2016 10:34:49 -0700 Subject: [PATCH 1/7] Add bash-it search functionality --- completion/available/bash-it.completion.bash | 2 +- lib/helpers.bash | 38 ++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/completion/available/bash-it.completion.bash b/completion/available/bash-it.completion.bash index f4eddc20..3cd8e84e 100644 --- a/completion/available/bash-it.completion.bash +++ b/completion/available/bash-it.completion.bash @@ -53,7 +53,7 @@ _bash-it-comp() prev="${COMP_WORDS[COMP_CWORD-1]}" chose_opt="${COMP_WORDS[1]}" file_type="${COMP_WORDS[2]}" - opts="help show enable disable update" + opts="help show enable disable update search" case "${chose_opt}" in show) local show_args="plugins aliases completions" diff --git a/lib/helpers.bash b/lib/helpers.bash index e3d0ef0d..b5f15c8c 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -32,14 +32,15 @@ function reload_plugins() { bash-it () { about 'Bash-it help and maintenance' - param '1: verb [one of: help | show | enable | disable | update ] ' - param '2: component type [one of: alias(es) | completion(s) | plugin(s) ]' + param '1: verb [one of: help | show | enable | disable | update | search ] ' + param '2: component type [one of: alias(es) | completion(s) | plugin(s) | - ]' param '3: specific component [optional]' example '$ bash-it show plugins' example '$ bash-it help aliases' example '$ bash-it enable plugin git [tmux]...' example '$ bash-it disable alias hg [tmux]...' example '$ bash-it update' + example '$ bash-it search ruby [rake]...' typeset verb=${1:-} shift typeset component=${1:-} @@ -54,6 +55,9 @@ bash-it () func=_disable-$component;; help) func=_help-$component;; + search) + _bash-it-search $component $* + return;; update) func=_bash-it_update;; *) @@ -139,6 +143,36 @@ _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() { + local terms=($@) + declare -a types=(aliases plugins completions) + for type in "${types[@]}" ; do + declare -a matches=() + for term in "${terms[@]}"; do + local term_match=($(bash-it show ${type} | grep -i -- ${term} | cut -d ' ' -f 1 | tr '\n' ' ')) + [[ "${#term_match[@]}" -gt 0 ]] && { + matches=(${matches[@]} ${term_match[@]}) + } + done + if [[ "${#matches[*]}" -gt 0 ]] ; then + printf "${type}: \e[3;32m%s\e[0;0m\n" "$(echo -n ${matches[*]} | tr ' ' '\n' | sort | uniq | tr '\n' ' ' | sed 's/ $//g')" + fi + unset matches + done +} + _bash-it-describe () { _about 'summarizes available bash_it components' From 7e417ca6d1f15ec29e8db2ed46acc81243d1825b Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Sun, 20 Mar 2016 20:07:52 -0700 Subject: [PATCH 2/7] WIP: bash-it search --- .atom-build.json | 14 ++++++++++++++ .editorconfig | 0 .gitignore | 2 ++ .travis.yml | 3 +-- lib/helpers.bash | 43 +++++++++++++++++++++++++++++++------------ test/lib/helpers.bats | 15 +++++++++++++++ test/run | 4 +++- 7 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 .atom-build.json mode change 100644 => 100755 .editorconfig mode change 100644 => 100755 .gitignore mode change 100644 => 100755 lib/helpers.bash create mode 100644 test/lib/helpers.bats diff --git a/.atom-build.json b/.atom-build.json new file mode 100644 index 00000000..dea01eda --- /dev/null +++ b/.atom-build.json @@ -0,0 +1,14 @@ +{ + "cmd": "test/run", + "name": "Run All Tests", + "args": [ ], + "keymap": "ctrl-r", + "sh": true, + "errorMatch": [ + "rspec (?[\\/0-9a-zA-Z\\._]+):(?\\d+) #", + "# (?\\./.*):(?\\d+)", + "\\s+from (?\\./.*):(?\\d+)", + "`(require|load)': (?\\./.*):(?\\d+)", + "^(/.*):(?\\d+)" + ] +} diff --git a/.editorconfig b/.editorconfig old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index dc868103..f12b3cf1 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ lib/custom.bash plugins/custom.plugins.bash *.swp .*.un~ +bats +.idea diff --git a/.travis.yml b/.travis.yml index a1745beb..34c38fe7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,3 @@ sudo: false -install: git clone --depth 1 https://github.com/sstephenson/bats.git -script: PATH="./bats/bin:$PATH" test/run +script: test/run language: c diff --git a/lib/helpers.bash b/lib/helpers.bash old mode 100644 new mode 100755 index b5f15c8c..4a2f3906 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -156,23 +156,42 @@ _bash-it_update() { # _bash-it-search() { - local terms=($@) + _about 'searches for a ' + _param '1: subdirectory' + _param '2: preposition' + _param '3: file_type' + _param '4: column_header' + _example '$ _bash-it-describe "plugins" "a" "plugin" "Plugin"' + declare -a types=(aliases plugins completions) for type in "${types[@]}" ; do - declare -a matches=() - for term in "${terms[@]}"; do - local term_match=($(bash-it show ${type} | grep -i -- ${term} | cut -d ' ' -f 1 | tr '\n' ' ')) - [[ "${#term_match[@]}" -gt 0 ]] && { - matches=(${matches[@]} ${term_match[@]}) - } - done - if [[ "${#matches[*]}" -gt 0 ]] ; then - printf "${type}: \e[3;32m%s\e[0;0m\n" "$(echo -n ${matches[*]} | tr ' ' '\n' | sort | uniq | tr '\n' ' ' | sed 's/ $//g')" - fi - unset matches + _bash-it-search-category "${type}" "$*" done } +_bash-it-search-category() { + type=$1 + local func=_bash-it-${type} + shift + declare -a terms=($@) + declare -a matches=() + local _grep=$(which egrep || which grep) + for term in "${terms[@]}"; do + local term_match=($($func | ${_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[3;32m" + [[ -n "$NO_COLOR" && color_off="" ]] || color_on="\e[0;0m" + + if [[ "${#matches[*]}" -gt 0 ]] ; then + printf "%15s: ${color_on}%s${color_off}\n" "${type}" "$(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/test/lib/helpers.bats b/test/lib/helpers.bats new file mode 100644 index 00000000..c601d692 --- /dev/null +++ b/test/lib/helpers.bats @@ -0,0 +1,15 @@ +#!/usr/bin/env bats + +load ../../lib/composure +cite _about _param _example _group _author _version + +load ../../lib/helpers +load ../../plugins/available/base.plugin + +export NO_COLOR=true + +@test "helpers search aliases" { + run _bash-it-search-category 'plugins' 'base' + echo "the lines are: ${output[*]}" + [[ "${lines[0]}" =~ 'plugins: base' ]] +} diff --git a/test/run b/test/run index 1fb11518..2efc35d0 100755 --- a/test/run +++ b/test/run @@ -1,4 +1,6 @@ #!/usr/bin/env bash +PATH=$PATH:$(pwd)/bats/bin +set +e +[[ -z "$(which bats)" ]] && git clone --depth 1 https://github.com/sstephenson/bats.git set -e - exec bats ${CI:+--tap} test/{lib,plugins} From 7715cc9b6a0abf7f9879903a5e6f52e50286ca03 Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Mon, 21 Mar 2016 00:05:43 -0700 Subject: [PATCH 3/7] Updating var names for consistency, adding cites --- lib/helpers.bash | 35 +++++++++++++++++++---------------- test/lib/helpers.bats | 4 ++-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index 4a2f3906..9f4f5b11 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -156,22 +156,26 @@ _bash-it_update() { # _bash-it-search() { - _about 'searches for a ' - _param '1: subdirectory' - _param '2: preposition' - _param '3: file_type' - _param '4: column_header' - _example '$ _bash-it-describe "plugins" "a" "plugin" "Plugin"' + _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 types=(aliases plugins completions) - for type in "${types[@]}" ; do - _bash-it-search-category "${type}" "$*" + declare -a _components=(aliases plugins completions) + for _component in "${_components[@]}" ; do + _bash-it-search-component "${_component}" "$*" done } -_bash-it-search-category() { - type=$1 - local func=_bash-it-${type} +_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} shift declare -a terms=($@) declare -a matches=() @@ -182,14 +186,13 @@ _bash-it-search-category() { matches=(${matches[@]} ${term_match[@]}) } done - [[ -n "$NO_COLOR" && color_on="" ]] || color_on="\e[3;32m" - [[ -n "$NO_COLOR" && color_off="" ]] || color_on="\e[0;0m" + [[ -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 "%15s: ${color_on}%s${color_off}\n" "${type}" "$(echo -n ${matches[*]} | tr ' ' '\n' | sort | uniq | tr '\n' ' ' | sed 's/ $//g')" + printf "%15s: ${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 () diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats index c601d692..f035e05e 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -6,10 +6,10 @@ cite _about _param _example _group _author _version load ../../lib/helpers load ../../plugins/available/base.plugin -export NO_COLOR=true +NO_COLOR=true @test "helpers search aliases" { - run _bash-it-search-category 'plugins' 'base' + run _bash-it-search-component 'plugins' 'base' echo "the lines are: ${output[*]}" [[ "${lines[0]}" =~ 'plugins: base' ]] } From 7a52fa6467cf95feab191bbc62efb1a36d816696 Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Mon, 21 Mar 2016 04:09:25 -0700 Subject: [PATCH 4/7] Removing atom-build, and extra echo in tests --- .atom-build.json | 14 -------------- .gitignore | 1 + test/lib/helpers.bats | 1 - 3 files changed, 1 insertion(+), 15 deletions(-) delete mode 100644 .atom-build.json diff --git a/.atom-build.json b/.atom-build.json deleted file mode 100644 index dea01eda..00000000 --- a/.atom-build.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "cmd": "test/run", - "name": "Run All Tests", - "args": [ ], - "keymap": "ctrl-r", - "sh": true, - "errorMatch": [ - "rspec (?[\\/0-9a-zA-Z\\._]+):(?\\d+) #", - "# (?\\./.*):(?\\d+)", - "\\s+from (?\\./.*):(?\\d+)", - "`(require|load)': (?\\./.*):(?\\d+)", - "^(/.*):(?\\d+)" - ] -} diff --git a/.gitignore b/.gitignore index f12b3cf1..b748d384 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.atom-build.json */enabled/* .DS_Store custom/* diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats index f035e05e..f1e0f4b6 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -10,6 +10,5 @@ NO_COLOR=true @test "helpers search aliases" { run _bash-it-search-component 'plugins' 'base' - echo "the lines are: ${output[*]}" [[ "${lines[0]}" =~ 'plugins: base' ]] } From 9f6255cd425a58b2e856ad69d2300b5ed00c594b Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Mon, 21 Mar 2016 04:42:27 -0700 Subject: [PATCH 5/7] Trying to fix the tests: - slightly better alignment - more resilient test expression --- lib/helpers.bash | 2 +- test/lib/helpers.bats | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index 9f4f5b11..5d6defeb 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -190,7 +190,7 @@ _bash-it-search-component() { [[ -n "$NO_COLOR" && color_off="" ]] || color_off="\e[0;0m" if [[ "${#matches[*]}" -gt 0 ]] ; then - printf "%15s: ${color_on}%s${color_off}\n" "${_component}" "$(echo -n ${matches[*]} | tr ' ' '\n' | sort | uniq | tr '\n' ' ' | sed 's/ $//g')" + 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/helpers.bats index f1e0f4b6..eb814199 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -1,14 +1,15 @@ #!/usr/bin/env bats load ../../lib/composure +load ../../plugins/available/base.plugin + cite _about _param _example _group _author _version load ../../lib/helpers -load ../../plugins/available/base.plugin NO_COLOR=true @test "helpers search aliases" { run _bash-it-search-component 'plugins' 'base' - [[ "${lines[0]}" =~ 'plugins: base' ]] + [[ "${lines[0]}" =~ 'plugins' && "${lines[0]}" =~ 'base' ]] } From 4f2e73c68a0762cbfabe1406da798d8bf6169393 Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Tue, 22 Mar 2016 05:05:58 -0700 Subject: [PATCH 6/7] Only run this test on Darwin --- test/lib/helpers.bats | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats index eb814199..6caa1fce 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -8,8 +8,12 @@ cite _about _param _example _group _author _version load ../../lib/helpers NO_COLOR=true +IS_DARWIN=false +[[ "$(uname -s)" == "Darwin" ]] && IS_DARWIN=true -@test "helpers search aliases" { - run _bash-it-search-component 'plugins' 'base' - [[ "${lines[0]}" =~ 'plugins' && "${lines[0]}" =~ 'base' ]] -} +if [ "$IS_DARWIN" == "tru" ]; then + @test "helpers search aliases" { + run _bash-it-search-component 'plugins' 'base' + [[ "${lines[0]}" =~ 'plugins' && "${lines[0]}" =~ 'base' ]] + } +fi From 22c753d202734e1502fd9c19a54e1e2f08fc1105 Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Tue, 22 Mar 2016 13:06:21 -0700 Subject: [PATCH 7/7] Fixing a typo --- test/lib/helpers.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats index 6caa1fce..8e4e3468 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -11,7 +11,7 @@ NO_COLOR=true IS_DARWIN=false [[ "$(uname -s)" == "Darwin" ]] && IS_DARWIN=true -if [ "$IS_DARWIN" == "tru" ]; then +if [ "$IS_DARWIN" == "true" ]; then @test "helpers search aliases" { run _bash-it-search-component 'plugins' 'base' [[ "${lines[0]}" =~ 'plugins' && "${lines[0]}" =~ 'base' ]]