Merge pull request #684 from kigster/bash-it-search-squashed

Add bash-it search functionality with "bash-it search term1 [term2]...."
pull/686/head
Nils Winkler 2016-03-24 08:11:22 +01:00
commit 9fc363037b
7 changed files with 87 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/* */enabled/*
.DS_Store .DS_Store
custom/* custom/*
@ -9,3 +10,5 @@ lib/custom.bash
plugins/custom.plugins.bash plugins/custom.plugins.bash
*.swp *.swp
.*.un~ .*.un~
bats
.idea

View File

@ -1,4 +1,3 @@
sudo: false sudo: false
install: git clone --depth 1 https://github.com/sstephenson/bats.git script: test/run
script: PATH="./bats/bin:$PATH" test/run
language: c language: c

View File

@ -53,7 +53,7 @@ _bash-it-comp()
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
chose_opt="${COMP_WORDS[1]}" chose_opt="${COMP_WORDS[1]}"
file_type="${COMP_WORDS[2]}" file_type="${COMP_WORDS[2]}"
opts="help show enable disable update" opts="help show enable disable update search"
case "${chose_opt}" in case "${chose_opt}" in
show) show)
local show_args="plugins aliases completions" local show_args="plugins aliases completions"

60
lib/helpers.bash 100644 → 100755
View File

@ -32,14 +32,15 @@ function reload_plugins() {
bash-it () bash-it ()
{ {
about 'Bash-it help and maintenance' about 'Bash-it help and maintenance'
param '1: verb [one of: help | show | enable | disable | update ] ' param '1: verb [one of: help | show | enable | disable | update | search ] '
param '2: component type [one of: alias(es) | completion(s) | plugin(s) ]' param '2: component type [one of: alias(es) | completion(s) | plugin(s) ] or search term(s)'
param '3: specific component [optional]' param '3: specific component [optional]'
example '$ bash-it show plugins' example '$ bash-it show plugins'
example '$ bash-it help aliases' example '$ bash-it help aliases'
example '$ bash-it enable plugin git [tmux]...' example '$ bash-it enable plugin git [tmux]...'
example '$ bash-it disable alias hg [tmux]...' example '$ bash-it disable alias hg [tmux]...'
example '$ bash-it update' example '$ bash-it update'
example '$ bash-it search ruby [rake]...'
typeset verb=${1:-} typeset verb=${1:-}
shift shift
typeset component=${1:-} typeset component=${1:-}
@ -54,6 +55,9 @@ bash-it ()
func=_disable-$component;; func=_disable-$component;;
help) help)
func=_help-$component;; func=_help-$component;;
search)
_bash-it-search $component $*
return;;
update) update)
func=_bash-it_update;; func=_bash-it_update;;
*) *)
@ -139,6 +143,58 @@ _bash-it_update() {
cd - &> /dev/null 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 () _bash-it-describe ()
{ {
_about 'summarizes available bash_it components' _about 'summarizes available bash_it components'

View File

@ -0,0 +1,21 @@
#!/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=
[[ "$(uname -s)" == "Darwin" ]] && IS_DARWIN=true
@test "helpers search aliases" {
if [ -z "$IS_DARWIN" ]; then
skip 'search test only runs on OSX'
fi
run _bash-it-search-component 'plugins' 'base'
[[ "${lines[0]}" =~ 'plugins' && "${lines[0]}" =~ 'base' ]]
}

View File

@ -1,4 +1,6 @@
#!/usr/bin/env bash #!/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 set -e
exec bats ${CI:+--tap} test/{lib,plugins} exec bats ${CI:+--tap} test/{lib,plugins}