completion/bash-it: adopt `_compreply_candidates()`

pull/2029/head
John D Pell 2022-01-07 21:03:30 -08:00 committed by John D Pell
parent e1e971c0ea
commit 2f4ed49a71
1 changed files with 38 additions and 85 deletions

View File

@ -1,132 +1,85 @@
# shellcheck shell=bash # shellcheck shell=bash
function _bash-it-comp-list-available-not-enabled() { function _compreply_candidates() {
local subdirectory="$1" IFS=$'\n' REPL local IFS=$'\n'
COMPREPLY=('all')
while read -ra REPL; do
COMPREPLY+=("${REPL[@]}")
done < <(compgen -W "$(_bash-it-component-list-disabled "${subdirectory}")" -- "${cur}")
}
function _bash-it-comp-list-enabled() { read -d '' -ra COMPREPLY < <(compgen -W "${candidates[*]}" -- "${cur}")
local subdirectory="$1" IFS=$'\n' REPL
COMPREPLY=('all')
while read -ra REPL; do
COMPREPLY+=("${REPL[@]}")
done < <(compgen -W "$(_bash-it-component-list-enabled "${subdirectory}")" -- "${cur}")
}
function _bash-it-comp-list-available() {
local subdirectory="$1" IFS=$'\n' REPL
COMPREPLY=('all')
while read -ra REPL; do
COMPREPLY+=("${REPL[@]}")
done < <(compgen -W "$(_bash-it-component-list "${subdirectory}")" -- "${cur}")
}
function _bash-it-comp-list-profiles() {
local profiles IFS=$'\n' REPL
COMPREPLY=()
profiles=("${BASH_IT}/profiles"/*.bash_it)
profiles=("${profiles[@]##*/}")
while read -ra REPL; do
COMPREPLY+=("${REPL[@]}")
done < <(compgen -W "${profiles[*]%%.bash_it}" -- "${cur}")
} }
function _bash-it-comp() { function _bash-it-comp() {
local cur prev opts chose_opt file_type local cur prev verb file_type candidates suffix
COMPREPLY=() COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD - 1]}" prev="${COMP_WORDS[COMP_CWORD - 1]}"
chose_opt="${COMP_WORDS[1]}" verb="${COMP_WORDS[1]}"
file_type="${COMP_WORDS[2]:-}" file_type="${COMP_WORDS[2]:-}"
opts="disable enable help migrate reload restart profile doctor search show update version" candidates=('disable' 'enable' 'help' 'migrate' 'reload' 'restart' 'profile' 'doctor' 'search' 'show' 'update' 'version')
case "${chose_opt}" in case "${verb}" in
show) show)
local show_args="aliases completions plugins" candidates=('aliases' 'completions' 'plugins')
COMPREPLY=($(compgen -W "${show_args}" -- "${cur}")) _compreply_candidates
return 0
;; ;;
help) help)
if [[ "${prev}" == "aliases" ]]; then if [[ "${prev}" == "aliases" ]]; then
_bash-it-comp-list-available aliases candidates=('all' "$(_bash-it-component-list "${file_type}")")
return 0 _compreply_candidates
else else
local help_args="aliases completions migrate plugins update" candidates=('aliases' 'completions' 'migrate' 'plugins' 'update')
COMPREPLY=($(compgen -W "${help_args}" -- "${cur}")) _compreply_candidates
return 0
fi fi
;; ;;
profile) profile)
case "${file_type}" in case "${file_type}" in
load) load | rm)
if [[ "load" == "$prev" ]]; then if [[ "${file_type}" == "$prev" ]]; then
_bash-it-comp-list-profiles candidates=("${BASH_IT}/profiles"/*.bash_it)
candidates=("${candidates[@]##*/}")
candidates=("${candidates[@]%%.bash_it}")
_compreply_candidates
fi fi
return 0
;;
rm)
if [[ "rm" == "$prev" ]]; then
_bash-it-comp-list-profiles
fi
return 0
;;
save)
return 0
;;
list)
return 0
;; ;;
save | list) ;;
*) *)
local profile_args="load save list rm" candidates=('load' 'save' 'list' 'rm')
COMPREPLY=($(compgen -W "${profile_args}" -- "${cur}")) _compreply_candidates
return 0
;; ;;
esac esac
;; ;;
doctor) doctor)
local doctor_args="errors warnings all" candidates=('errors' 'warnings' 'all')
COMPREPLY=($(compgen -W "${doctor_args}" -- "${cur}")) _compreply_candidates
return 0
;; ;;
update) update)
if [[ "${cur}" == -* ]]; then if [[ "${cur}" == -* ]]; then
local update_args="-s --silent" candidates=('-s' '--silent')
else else
local update_args="stable dev" candidates=('stable' 'dev')
fi fi
COMPREPLY=($(compgen -W "${update_args}" -- "${cur}")) _compreply_candidates
return 0
;;
migrate | reload | restart | search | version)
return 0
;; ;;
migrate | reload | restart | search | version) ;;
enable | disable) enable | disable)
if [[ "${chose_opt}" == "enable" ]]; then if [[ "${verb}" == "enable" ]]; then
suffix="available-not-enabled" suffix="disabled"
else else
suffix="enabled" suffix="enabled"
fi fi
case "${file_type}" in case "${file_type}" in
alias | completion | plugin) alias | completion | plugin)
_bash-it-comp-list-"${suffix}" "${file_type}" candidates=('all' "$("_bash-it-component-list-${suffix}" "${file_type}")")
return 0 _compreply_candidates
;; ;;
*) *)
local enable_disable_args="alias completion plugin" candidates=('alias' 'completion' 'plugin')
COMPREPLY=($(compgen -W "${enable_disable_args}" -- "${cur}")) _compreply_candidates
return 0
;; ;;
esac esac
;; ;;
*)
_compreply_candidates
;;
esac esac
COMPREPLY=($(compgen -W "${opts}" -- "${cur}"))
return 0
} }
# Activate completion for bash-it and its common misspellings # Activate completion for bash-it and its common misspellings