Skip to content

Commit

Permalink
Merge pull request latchset#155 from sergio-correia/bash-completion
Browse files Browse the repository at this point in the history
Improve bash completion
  • Loading branch information
sergio-correia authored Jan 20, 2020
2 parents 15fc35e + 19511fd commit 94b6363
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/bash/clevis
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
# bash completion support for clevis
# bash completion support for clevis.

_clevis()
{
dir=$(dirname $(which clevis))
local dir prev cur field
dir=$(dirname "$(command -v clevis)")
prev=${COMP_WORDS[COMP_CWORD-1]}
cur=${COMP_WORDS[COMP_CWORD]}
field=$(($COMP_CWORD + 1))
field=$((COMP_CWORD + 1))

if [[ ${COMP_WORDS[COMP_CWORD-1]} == "clevis" ]]; then
name=clevis-*
case "${prev}" in
-d)
cur=${cur:=/dev/}
_filedir
return
;;
-k)
_filedir
return
;;
esac

local name suggestions
if [[ "${COMP_WORDS[COMP_CWORD-1]}" == "clevis" ]]; then
name="clevis-*"
fi

if [[ ${COMP_WORDS[COMP_CWORD-2]} == "clevis" ]]; then
name=clevis-${COMP_WORDS[COMP_CWORD-1]}-*
if [[ "${COMP_WORDS[COMP_CWORD-2]}" == "clevis" ]]; then
name="clevis-${COMP_WORDS[COMP_CWORD-1]}-*"
fi

suggestions=$(find $dir -name $name | cut -d '-' -f$field | sort | uniq)
suggestions=
if [[ -n "${name}" ]]; then
suggestions=$(find "${dir}" -name "${name}" -executable \
| cut -d '-' -f"${field}" | sort -u)

if [[ -n $cur ]]; then
suggestions=$(for word in ${suggestions[@]}; do echo $word | grep $cur; done)
local word
if [[ -n "${cur}" ]]; then
suggestions=$(for word in "${suggestions[@]}"; do \
echo "${word}" | grep -- "${cur}"; done)
fi
fi

COMPREPLY=($(compgen -W "$suggestions" -- "$cur"))
COMPREPLY=($(compgen -W "${suggestions}" -- "${cur}"))
}

complete -F _clevis clevis
# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:

0 comments on commit 94b6363

Please sign in to comment.