Bash completions: clean up basic functions

This commit is contained in:
Maxim Belkin 2023-02-07 05:55:58 -07:00
parent 8fdf6aca11
commit 262acf114f
2 changed files with 16 additions and 44 deletions

View File

@ -14,42 +14,28 @@
# Bash completion script for brew(1) # Bash completion script for brew(1)
__brewcomp_words_include() { __brewcomp_words_include() {
local i=1 local element idx
while [[ "${i}" -lt "${COMP_CWORD}" ]] for (( idx = 1; idx < COMP_CWORD; idx++ ))
do do
if [[ "${COMP_WORDS[i]}" = "$1" ]] element=${COMP_WORDS[idx]}
then [[ -n ${element} && ${element} == "$1" ]] && return 0
return 0
fi
(( i++ ))
done done
return 1 return 1
} }
# Find the previous non-switch word
__brewcomp_prev() {
local idx="$((COMP_CWORD - 1))"
local prv="${COMP_WORDS[idx]}"
while [[ "${prv}" = -* ]]
do
(( idx-- ))
prv="${COMP_WORDS[idx]}"
done
echo "${prv}"
}
__brewcomp() { __brewcomp() {
# break $1 on space, tab, and newline characters, # break $1 on space, tab, and newline characters,
# and turn it into a newline separated list of words # and turn it into a newline separated list of words
local list s sep=$'\n' IFS=$' \t\n' local list s sep=$'\n' IFS=$' \t\n'
local cur="${COMP_WORDS[COMP_CWORD]}" local cur=${COMP_WORDS[COMP_CWORD]}
for s in $1 for s in $1
do do
__brewcomp_words_include "${s}" && continue __brewcomp_words_include "${s}" || list+="${s}${sep}"
list="${list}${s}${sep}"
done done
list=${list%"${sep}"}
IFS="${sep}" IFS="${sep}"
while read -r line; do COMPREPLY+=("${line}"); done < <(compgen -W "${list}" -- "${cur}") while read -r line; do COMPREPLY+=("${line}"); done < <(compgen -W "${list}" -- "${cur}")
} }

View File

@ -1,42 +1,28 @@
# Bash completion script for brew(1) # Bash completion script for brew(1)
__brewcomp_words_include() { __brewcomp_words_include() {
local i=1 local element idx
while [[ "${i}" -lt "${COMP_CWORD}" ]] for (( idx = 1; idx < COMP_CWORD; idx++ ))
do do
if [[ "${COMP_WORDS[i]}" = "$1" ]] element=${COMP_WORDS[idx]}
then [[ -n ${element} && ${element} == "$1" ]] && return 0
return 0
fi
(( i++ ))
done done
return 1 return 1
} }
# Find the previous non-switch word
__brewcomp_prev() {
local idx="$((COMP_CWORD - 1))"
local prv="${COMP_WORDS[idx]}"
while [[ "${prv}" = -* ]]
do
(( idx-- ))
prv="${COMP_WORDS[idx]}"
done
echo "${prv}"
}
__brewcomp() { __brewcomp() {
# break $1 on space, tab, and newline characters, # break $1 on space, tab, and newline characters,
# and turn it into a newline separated list of words # and turn it into a newline separated list of words
local list s sep=$'\n' IFS=$' \t\n' local list s sep=$'\n' IFS=$' \t\n'
local cur="${COMP_WORDS[COMP_CWORD]}" local cur=${COMP_WORDS[COMP_CWORD]}
for s in $1 for s in $1
do do
__brewcomp_words_include "${s}" && continue __brewcomp_words_include "${s}" || list+="${s}${sep}"
list="${list}${s}${sep}"
done done
list=${list%"${sep}"}
IFS="${sep}" IFS="${sep}"
while read -r line; do COMPREPLY+=("${line}"); done < <(compgen -W "${list}" -- "${cur}") while read -r line; do COMPREPLY+=("${line}"); done < <(compgen -W "${list}" -- "${cur}")
} }