| 1 |
# cluset bash completion |
| 2 |
# |
| 3 |
# to install in /usr/share/bash-completion/completions/ or ~/.local/share/bash-completion/completions/ |
| 4 |
_cluset() |
| 5 |
{ |
| 6 |
# shellcheck disable=SC2034 # set/used by _init_completion |
| 7 |
local cur prev words cword split |
| 8 |
local word options="" skip=argv0 groupsource="" cleangroup="" |
| 9 |
|
| 10 |
_init_completion -s -n : || return |
| 11 |
|
| 12 |
# stop parsing if there had been any non-option before (or --) |
| 13 |
for word in "${words[@]}"; do |
| 14 |
case "$skip" in |
| 15 |
"") ;; |
| 16 |
groupsource) |
| 17 |
groupsource="$word" |
| 18 |
;& # fallthrough |
| 19 |
*) |
| 20 |
skip="" |
| 21 |
continue |
| 22 |
;; |
| 23 |
esac |
| 24 |
case "$word" in |
| 25 |
"") ;; |
| 26 |
--) return;; |
| 27 |
# no-arg options |
| 28 |
--version|-h|--help|-l|--list|-L|--list-all|-r|--regroup|\ |
| 29 |
--list-sources|--groupsources|-d|--debug|-q|--quiet|\ |
| 30 |
-R|--rangeset|-G|--groupbase|--contiguous|-a|--all|--completion) ;; |
| 31 |
# get source separately... |
| 32 |
--groupsource=*) groupsource="${word#*=}";; |
| 33 |
-s|--groupsource) skip=groupsource;; |
| 34 |
# assume all the rest as options... |
| 35 |
# options with = included in word |
| 36 |
--*=*) ;; |
| 37 |
-*) skip=any;; |
| 38 |
*) return;; # was non-option |
| 39 |
esac |
| 40 |
done |
| 41 |
|
| 42 |
case "$prev" in |
| 43 |
-c|--count|-e|--expand|-f|--fold|--index|\ |
| 44 |
-x|--exclude|-i|--intersection|-X|--xor) |
| 45 |
case "$cur" in |
| 46 |
*:*) |
| 47 |
groupsource="${cur%%:*}" |
| 48 |
groupsource="${groupsource#@}" |
| 49 |
;; |
| 50 |
*) |
| 51 |
if [ -n "$groupsource" ]; then |
| 52 |
cleangroup=1 |
| 53 |
fi |
| 54 |
;; |
| 55 |
esac |
| 56 |
options="$(cluset ${groupsource:+-s "$groupsource"} --completion @*)" |
| 57 |
if [ -n "$cleangroup" ]; then |
| 58 |
options=${options//@"$groupsource":/@} |
| 59 |
fi |
| 60 |
;; |
| 61 |
-s|--groupsource) |
| 62 |
options=$(cluset --groupsources --quiet) |
| 63 |
;; |
| 64 |
# no-arg options |
| 65 |
--version|-h|--help|-l|--list|-L|--list-all|-r|--regroup|\ |
| 66 |
--list-sources|--groupsources|-d|--debug|-q|--quiet|\ |
| 67 |
-R|--rangeset|-G|--groupbase|--contiguous|-a|--all|--completion) ;; |
| 68 |
# any other option: just ignore. |
| 69 |
-*) |
| 70 |
return;; |
| 71 |
esac |
| 72 |
# get all options from help text... not 100% accurate but good enough. |
| 73 |
[ -n "$options" ] || options="$(cluset --help | grep -oP -- '(?<=[ \t])(-[a-z]|--[^= \t]*)')" |
| 74 |
|
| 75 |
# append space for everything that doesn't end in `:` (likely a groupsource) |
| 76 |
mapfile -t COMPREPLY < <(compgen -W "$options" -- "$cur" | sed -e 's/[^:]$/& /') |
| 77 |
# remove the prefix from COMPREPLY if $cur contains colons and |
| 78 |
# COMP_WORDBREAKS splits on colons... |
| 79 |
__ltrim_colon_completions "$cur" |
| 80 |
} && complete -o nospace -F _cluset ${BASH_SOURCE##*/} |