completion/bash-it: use existing functions

pull/2029/head
John D Pell 2022-01-09 00:58:08 -08:00 committed by John D Pell
parent d7695d5456
commit 3874ad85c2
2 changed files with 25 additions and 68 deletions

View File

@ -1,74 +1,36 @@
#!/usr/bin/env bash # shellcheck shell=bash
_bash-it-comp-enable-disable() { function _bash-it-comp-list-available-not-enabled() {
local enable_disable_args="alias completion plugin"
COMPREPLY=($(compgen -W "${enable_disable_args}" -- "${cur}"))
}
_bash-it-comp-list-available-not-enabled() {
local subdirectory="$1" local subdirectory="$1"
COMPREPLY=($(compgen -W "all $(_bash-it-component-list-disabled "${subdirectory}")" -- "${cur}"))
local enabled_components all_things available_things
all_things=($(compgen -G "${BASH_IT}/$subdirectory/available/*.bash"))
all_things=("${all_things[@]##*/}")
enabled_components=($(command ls "${BASH_IT}"/{"$subdirectory"/,}enabled/*.bash 2> /dev/null))
enabled_components=("${enabled_components[@]##*/}")
enabled_components="${enabled_components[@]##*---}"
available_things=($(sort -d <(
for i in ${enabled_components}; do
all_things=("${all_things[@]//$i/}")
done
printf '%s\n' "${all_things[@]}"
)))
available_things="${available_things[@]%.*.bash}"
COMPREPLY=($(compgen -W "all ${available_things}" -- "${cur}"))
} }
_bash-it-comp-list-enabled() { function _bash-it-comp-list-enabled() {
local subdirectory="$1" local subdirectory="$1"
local suffix enabled_things COMPREPLY=($(compgen -W "all $(_bash-it-component-list-enabled "${subdirectory}")" -- "${cur}"))
suffix="${subdirectory/plugins/plugin}"
enabled_things=($(sort -d <(compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash") <(compgen -G "${BASH_IT}/enabled/*.${suffix}.bash")))
enabled_things=("${enabled_things[@]##*/}")
enabled_things=("${enabled_things[@]##*---}")
enabled_things="${enabled_things[@]%.*.bash}"
COMPREPLY=($(compgen -W "all ${enabled_things}" -- "${cur}"))
} }
_bash-it-comp-list-available() { function _bash-it-comp-list-available() {
local subdirectory="$1" local subdirectory="$1"
COMPREPLY=($(compgen -W "all $(_bash-it-component-list "${subdirectory}")" -- "${cur}"))
local enabled_things
enabled_things=($(sort -d <(compgen -G "${BASH_IT}/$subdirectory/available/*.bash")))
enabled_things=("${enabled_things[@]##*/}")
enabled_things="${enabled_things[@]%.*.bash}"
COMPREPLY=($(compgen -W "${enabled_things}" -- "${cur}"))
} }
_bash-it-comp-list-profiles() { function _bash-it-comp-list-profiles() {
local profiles local profiles
profiles=$(for f in $(compgen -G "${BASH_IT}/profiles/*.bash_it" | sort -d); do profiles=("${BASH_IT}/profiles"/*.bash_it)
basename $f | sed -e 's/.bash_it//g' profiles=("${profiles[@]##*/}")
done)
COMPREPLY=($(compgen -W "${profiles}" -- ${cur})) COMPREPLY=($(compgen -W "${profiles[*]%%.bash_it}" -- "${cur}"))
} }
_bash-it-comp() { function _bash-it-comp() {
local cur prev opts local cur prev opts
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]}" chose_opt="${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" opts="disable enable help migrate reload restart profile doctor search show update version"
case "${chose_opt}" in case "${chose_opt}" in
show) show)
@ -77,7 +39,7 @@ _bash-it-comp() {
return 0 return 0
;; ;;
help) help)
if [ x"${prev}" == x"aliases" ]; then if [[ "${prev}" == "aliases" ]]; then
_bash-it-comp-list-available aliases _bash-it-comp-list-available aliases
return 0 return 0
else else
@ -108,7 +70,7 @@ _bash-it-comp() {
;; ;;
*) *)
local profile_args="load save list rm" local profile_args="load save list rm"
COMPREPLY=($(compgen -W "${profile_args}" -- ${cur})) COMPREPLY=($(compgen -W "${profile_args}" -- "${cur}"))
return 0 return 0
;; ;;
esac esac
@ -131,26 +93,19 @@ _bash-it-comp() {
return 0 return 0
;; ;;
enable | disable) enable | disable)
if [ x"${chose_opt}" == x"enable" ]; then if [[ "${chose_opt}" == "enable" ]]; then
suffix="available-not-enabled" suffix="available-not-enabled"
else else
suffix="enabled" suffix="enabled"
fi fi
case "${file_type}" in case "${file_type}" in
alias) alias | completion | plugin)
_bash-it-comp-list-"${suffix}" aliases _bash-it-comp-list-"${suffix}" "${file_type}"
return 0
;;
plugin)
_bash-it-comp-list-"${suffix}" plugins
return 0
;;
completion)
_bash-it-comp-list-"${suffix}" completion
return 0 return 0
;; ;;
*) *)
_bash-it-comp-enable-disable local enable_disable_args="alias completion plugin"
COMPREPLY=($(compgen -W "${enable_disable_args}" -- "${cur}"))
return 0 return 0
;; ;;
esac esac

View File

@ -1,6 +1,8 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load ../../lib/utilities
load ../../lib/helpers
load ../../completion/available/bash-it.completion load ../../completion/available/bash-it.completion
function local_setup { function local_setup {
@ -290,7 +292,7 @@ function __check_completion () {
assert_link_exist "$BASH_IT/aliases/enabled/docker-compose.aliases.bash" assert_link_exist "$BASH_IT/aliases/enabled/docker-compose.aliases.bash"
run __check_completion 'bash-it enable plugin docker' run __check_completion 'bash-it enable plugin docker'
assert_line -n 0 "docker-compose docker-machine docker" assert_line -n 0 "docker docker-compose docker-machine"
} }
@test "completion bash-it: enable - provide the docker-* plugins when nothing is enabled with the old location and priority-based name" { @test "completion bash-it: enable - provide the docker-* plugins when nothing is enabled with the old location and priority-based name" {
@ -298,7 +300,7 @@ function __check_completion () {
assert_link_exist "$BASH_IT/aliases/enabled/150---docker-compose.aliases.bash" assert_link_exist "$BASH_IT/aliases/enabled/150---docker-compose.aliases.bash"
run __check_completion 'bash-it enable plugin docker' run __check_completion 'bash-it enable plugin docker'
assert_line -n 0 "docker-compose docker-machine docker" assert_line -n 0 "docker docker-compose docker-machine"
} }
@test "completion bash-it: enable - provide the docker-* plugins when nothing is enabled with the new location and priority-based name" { @test "completion bash-it: enable - provide the docker-* plugins when nothing is enabled with the new location and priority-based name" {
@ -306,7 +308,7 @@ function __check_completion () {
assert_link_exist "$BASH_IT/enabled/150---docker-compose.aliases.bash" assert_link_exist "$BASH_IT/enabled/150---docker-compose.aliases.bash"
run __check_completion 'bash-it enable plugin docker' run __check_completion 'bash-it enable plugin docker'
assert_line -n 0 "docker-compose docker-machine docker" assert_line -n 0 "docker docker-compose docker-machine"
} }
@test "completion bash-it: enable - provide the docker-* completions when nothing is enabled with the old location and name" { @test "completion bash-it: enable - provide the docker-* completions when nothing is enabled with the old location and name" {