From 7f17c8f8276ddbb1e9dd8421f31ae2ecf59247a4 Mon Sep 17 00:00:00 2001 From: Gurkirat Singh Date: Tue, 15 Mar 2022 07:23:17 +0530 Subject: [PATCH] fix (completion): remove file dependencies and add more completion for options --- .../available/capistrano.completion.bash | 50 ++++++++++++------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/completion/available/capistrano.completion.bash b/completion/available/capistrano.completion.bash index d5fda06f..1428fff8 100644 --- a/completion/available/capistrano.completion.bash +++ b/completion/available/capistrano.completion.bash @@ -1,24 +1,36 @@ -#!/usr/bin/env bash +# shellcheck shell=bash # Bash completion support for Capistrano. export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/} -_capcomplete() { - if [ -f Capfile ]; then - recent=`ls -t .cap_tasks~ Capfile **/*.cap 2> /dev/null | head -n 1` - if [[ $recent != '.cap_tasks~' ]]; then - cap --version | grep 'Capistrano v2.' > /dev/null - if [ $? -eq 0 ]; then - # Capistrano 2.x - cap --tool --verbose --tasks | cut -d " " -f 2 > .cap_tasks~ - else - # Capistrano 3.x - cap --all --tasks | cut -d " " -f 2 > .cap_tasks~ - fi - fi - COMPREPLY=($(compgen -W "`cat .cap_tasks~`" -- ${COMP_WORDS[COMP_CWORD]})) - return 0 - fi -} +if _command_exists cap; then + function __cap_completions() { + [[ ! -f Capfile ]] && return 1 -complete -o default -o nospace -F _capcomplete cap + local cword + cword=$(_get_cword) + + case $cword in + -*) + # Complete options + COMPREPLY=(--{suppress-,}backtrace --comments --job-stats --rules -A --all -B --build-all -C --directory -D --describe -e -E --execute{,-continue,-print} -f --rakefile + -G --{no{-,},}system -g -I --libdir -j --jobs -m --multitask -N --no{-,}search -P --prereqs --require -R --rakelib{dir,} -t --trace -T --tasks -W --where -X --no-deprecation-warning + -V --version -n --dry-run -r --roles -z --hosts -p --print-config-variables -h -H --help) + ;; + + *) + # Complete tasks + if cap --version | grep 'Capistrano v2.' > /dev/null; then + # Capistrano 2.x + read -ra COMPREPLY < <(cap --tool --verbose --tasks | awk '{print $2}' | tr '\n' ' ') + else + # Capistrano 3.x + read -ra COMPREPLY < <(cap --all --tasks | awk '{print $2}' | tr '\n' ' ') + fi + ;; + + esac + } + + complete -F __cap_completions -X "!&*" cap +fi