Skip to content

Commit

Permalink
WIP: bash completions
Browse files Browse the repository at this point in the history
just clush for now
  • Loading branch information
martinetd committed Jun 30, 2024
1 parent 153d80e commit e738d0c
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions bash_completion.d/clush
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# clush bash completion
#
# to install in /usr/share/bash-completion/completions/ or ~/.local/share/bash-completion/completions/
_clush()
{
# shellcheck disable=SC2034 # set/used by _init_completion
local cur prev words cword split
local word options="" skip=argv0 groupsource=""

_init_completion -s -n : || return

# stop parsing if there had been any non-option before (or --)
for word in "${words[@]}"; do
case "$skip" in
"") ;;
groupsource)
groupsource="$word"
;& # fallthrough
*)
skip=""
continue
;;
esac
case "$word" in
"") ;;
--) return;;
# no-arg options
--version|-h|--help|-n|--nostdin|-a|--all|-q|--quiet|\
-v|--verbose|-d|--debug) ;;
# get source separately...
--groupsource=*) groupsource="${word#*=}";;
-s|--groupsource) skip=groupsource;;
# assume all the rest as options...
# options with = included in word
--*=*) ;;
-*) skip=any;;
*) return;; # was non-option
esac
done

case "$prev" in
-w|-x)
options="$(cluset ${groupsource:+-s "$groupsource"} -L 2>/dev/null)"
;;
_g|--group|-X)
options="$(cluset ${groupsource:+-s "$groupsource"} -L 2>/dev/null | sed -e 's/^@//')"
;;
-s|--groupsource)
options=$(cluset --groupsources | sed -e 's/ (default)//')
;;
--color)
options="never always auto"
;;
-R|--worker)
options="ssh exec rsh"
;;
# no-arg options
--version|-h|--help|-n|--nostdin|-a|--all|-q|--quiet|\
-v|--verbose|-d|--debug) ;;
# any other option: just ignore.
-*)
return;;
esac
# get all options from help text... not 100% accurate but good enough.
[ -n "$options" ] || options="$(clush --help | grep -oP -- '(?<=[ \t])(-[a-z]|--[^= \t]*)')"

mapfile -t COMPREPLY < <(compgen -W "$options" -- "$cur")
# remove the prefix from COMPREPLY if $cur contains colons and
# COMP_WORDBREAKS splits on colons...
__ltrim_colon_completions "$cur"
} && complete -F _clush clush

0 comments on commit e738d0c

Please sign in to comment.