Skip to content

Commit

Permalink
_comp_delimited: new helper for delimited values, use here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Dec 7, 2020
1 parent 274c2fd commit 4138714
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 42 deletions.
41 changes: 41 additions & 0 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,47 @@ _variables()
return 1
}

# Complete a delimited value.
#
# Usage: [-k] DELIMITER COMPGEN_ARG...
# -k: do not filter out already present tokens in value
_comp_delimited()
{
local prefix="" delimiter=$1 deduplicate=true
shift
if [[ $delimiter == -k ]]; then
deduplicate=false
delimiter=$1
shift
fi
[[ $cur == *$delimiter* ]] && prefix=${cur%$delimiter*}$delimiter

if $deduplicate; then
# We could construct a -X pattern to feed to compgen, but that'd
# conflict with possibly already set -X in $@, as well as have
# glob char escaping issues to deal with. Do removals by hand instead.
COMPREPLY=($(compgen "$@"))
local -a existing
local x i ifs=$IFS IFS=$delimiter
existing=($cur)
IFS=$ifs
for x in "${existing[@]}"; do
for i in "${!COMPREPLY[@]}"; do
if [[ $x == "${COMPREPLY[i]}" ]]; then
unset "COMPREPLY[i]"
continue 2 # assume no dupes in COMPREPLY
fi
done
done
COMPREPLY=($(compgen -W '${COMPREPLY[@]}' -- "${cur##*$delimiter}"))
else
COMPREPLY=($(compgen "$@" -- "${cur##*$delimiter}"))
fi

((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
[[ $delimiter != : ]] || __ltrim_colon_completions "$cur"
}

# Complete assignment of various known environment variables.
# The word to be completed is expected to contain the entire
# assignment, including the variable name and the "=". See related
Expand Down
7 changes: 2 additions & 5 deletions completions/fio
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ _fio()
return
;;
--debug)
local prefix=
[[ $cur == *,* ]] && prefix="${cur%,*},"
COMPREPLY=($(compgen -W "process file io mem blktrace verify
_comp_delimited , -W "process file io mem blktrace verify
random parse diskutil job mutex profile time net rate compress
steadystate helperthread" -- "${cur##*,}"))
((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
steadystate helperthread"
return
;;
--output-format)
Expand Down
4 changes: 1 addition & 3 deletions completions/pgrep
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ _pgrep()
return
;;
--nslist)
local prefix=
[[ $cur == *,* ]] && prefix="${cur%,*},"
COMPREPLY=(
$(IFS="$IFS," compgen -W '$($1 --help 2>&1 |
command sed -ne "s/^[[:space:]]*Available namespaces://p")' \
-- "${cur##*,}"))
((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
_comp_delimited , -W '${COMPREPLY[@]}'
return
;;
esac
Expand Down
6 changes: 1 addition & 5 deletions completions/ps
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ _ps()
--format | ?(-)[Oo] | [^-]*[Oo])
# TODO: This doesn't work well when there are multiple options for
# the non-first item (similarly to useradd --groups and others).
local prefix=
[[ $cur == *,* ]] && prefix="${cur%,*},"
COMPREPLY=($(compgen -W "$("$1" L | awk '{ print $1 }')" \
-- "${cur##*,}"))
((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
_comp_delimited , -W "$("$1" L | awk '{ print $1 }')"
return
;;
esac
Expand Down
6 changes: 3 additions & 3 deletions completions/pylint
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ _pylint()
return
;;
--load-plugins | --deprecated-modules)
local prefix=
[[ $cur == *,* ]] && prefix="${cur%,*},"
local ocur=$cur
cur="${cur##*,}"
_xfunc python _python_modules $python
((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
cur=$ocur
_comp_delimited , -W '${COMPREPLY[@]}'
return
;;
--jobs | -!(-*)j)
Expand Down
8 changes: 2 additions & 6 deletions completions/ss
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ _ss()
return
;;
--query | -!(-*)A)
local prefix=
[[ $cur == *,* ]] && prefix="${cur%,*},"
COMPREPLY=($(compgen -W '$("$1" --help | \
command sed -e "s/|/ /g" -ne "s/.*QUERY := {\([^}]*\)}.*/\1/p")' \
-- "${cur##*,}"))
((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
_comp_delimited , -W "$("$1" --help |
command sed -e 's/|/ /g' -ne 's/.*QUERY := {\([^}]*\)}.*/\1/p')"
return
;;
--diag | --filter | -!(-*)[DF])
Expand Down
6 changes: 2 additions & 4 deletions completions/ssh-keygen
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ _ssh_keygen()
;;
-*n)
[[ ${words[*]} != *\ -*Y\ * ]] || return
local prefix=
[[ $cur == *,* ]] && prefix="${cur%,*},"
if [[ ${words[*]} == *\ -*h\ * ]]; then
_known_hosts_real -- "${cur##*,}"
_comp_delimited , -W '${COMPREPLY[@]}'
else
COMPREPLY=($(compgen -u -- "${cur##*,}"))
_comp_delimited , -u
fi
((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
return
;;
-*O)
Expand Down
5 changes: 1 addition & 4 deletions completions/tox
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ _tox()
command sed -e 's/,/ /g' -ne 's/^envlist[[:space:]]*=//p' \
tox.ini 2>/dev/null
)
local prefix=""
[[ $cur == *,* ]] && prefix="${cur%,*},"
COMPREPLY=($(compgen -X '*[{}]*' -W "$envs ALL" -- "${cur##*,}"))
((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
_comp_delimited , -X '*[{}]*' -W "$envs ALL"
return
;;
esac
Expand Down
5 changes: 1 addition & 4 deletions completions/tshark
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,10 @@ _tshark()
return
;;
-*O)
local prefix=
[[ $cur == *,* ]] && prefix="${cur%,*},"
[[ -v _tshark_protocols ]] ||
_tshark_protocols="$("$1" -G protocols 2>/dev/null |
cut -f 3 | tr '\n' ' ')"
COMPREPLY=($(compgen -W "$_tshark_protocols" -- "${cur##*,}"))
((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
_comp_delimited , -W "$_tshark_protocols"
return
;;
-*T)
Expand Down
5 changes: 1 addition & 4 deletions completions/useradd
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ _useradd()
return
;;
--groups | -!(-*)G)
local prefix=
[[ $cur == *,* ]] && prefix="${cur%,*},"
COMPREPLY=($(compgen -g -- "${cur##*,}"))
((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
_comp_delimited , -g
return
;;
--shell | -!(-*)s)
Expand Down
5 changes: 1 addition & 4 deletions completions/usermod
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ _usermod()
return
;;
--groups | -!(-*)G)
local prefix=
[[ $cur == *,* ]] && prefix="${cur%,*},"
COMPREPLY=($(compgen -g -- "${cur##*,}"))
((${#COMPREPLY[@]} == 1)) && COMPREPLY=(${COMPREPLY/#/$prefix})
_comp_delimited , -g
return
;;
--root | -!(-*)R)
Expand Down

0 comments on commit 4138714

Please sign in to comment.