Add bash-it search functionality

- "bash-it search term1 [term2]...."

 - we are using existing 'bash-it show plugins|aliases|completions'
   commands output, to search (with grep) for lines that match the
   search terms, and then output the matches.

 - wrote a simple unit test that for whatever reason fails on Travis,
   so wrapped it in 'if "Darwin"'...
pull/684/head
Konstantin Gredeskoul 2016-03-20 10:34:49 -07:00
parent 5ec816342f
commit c0a657270c
7 changed files with 85 additions and 6 deletions

0
.editorconfig 100644 → 100755
View File

3
.gitignore vendored 100644 → 100755
View File

@ -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

View File

@ -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

View File

@ -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"

60
lib/helpers.bash 100644 → 100755
View File

@ -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'

View File

@ -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

View File

@ -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}