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..b748d384 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.atom-build.json */enabled/* .DS_Store custom/* @@ -9,3 +10,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/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 old mode 100644 new mode 100755 index e3d0ef0d..84915b9f --- 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) ] or search term(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,58 @@ _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} + 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[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/test/lib/helpers.bats b/test/lib/helpers.bats new file mode 100644 index 00000000..8e4e3468 --- /dev/null +++ b/test/lib/helpers.bats @@ -0,0 +1,19 @@ +#!/usr/bin/env bats + +load ../../lib/composure +load ../../plugins/available/base.plugin + +cite _about _param _example _group _author _version + +load ../../lib/helpers + +NO_COLOR=true +IS_DARWIN=false +[[ "$(uname -s)" == "Darwin" ]] && IS_DARWIN=true + +if [ "$IS_DARWIN" == "true" ]; then + @test "helpers search aliases" { + run _bash-it-search-component 'plugins' 'base' + [[ "${lines[0]}" =~ 'plugins' && "${lines[0]}" =~ 'base' ]] + } +fi 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}