Skip to content

Instantly share code, notes, and snippets.

@kyontan
Created July 19, 2021 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyontan/af77049f424876dcffdbd0827b1b4389 to your computer and use it in GitHub Desktop.
Save kyontan/af77049f424876dcffdbd0827b1b4389 to your computer and use it in GitHub Desktop.
envchain completion (わからん)
function _delegate() {
local cur subs
cur="${COMP_WORDS[COMP_CWORD]}" # partial word, if any
subs=$(envchain -l)
# echo "cword: $COMP_CWORD"
# echo "cw: ${COMP_WORDS[COMP_CWORD]}"
# echo "cl: ${COMP_LINE}, ccw: ${COMP_CWORD}, cp: ${COMP_POINT}, cw3: ${COMP_WORDS[2]}"
if [[ $COMP_CWORD == 1 ]]; then
# complete with the list of subcommands
COMPREPLY=( $(compgen -W "${subs}" -- ${cur}) )
elif [[ $COMP_CWORD == 2 ]]; then
# Two whole words before the cursor - delegate to the second arg
# echo "Delegate $1, $2"
# _command $2
# complete -F _executables $1
local exclude=$(compgen -abkA function | sort)
local executables=$(compgen -c <(echo $exclude))
#local executables=$(
# comm -23 <(compgen -c) <(echo $exclude)
# type -tP $( comm -12 <(compgen -c) <(echo $exclude) )
# type -tP $( comm -12 <(compgen -c) <(echo $exclude) ) #)
COMPREPLY=( $(compgen -W "$executables" -- ${COMP_WORDS[COMP_CWORD]}) )
elif [[ $COMP_CWORD -ge 3 ]] && [[ "${COMP_WORDS[2]}" == "aws" ]]; then
aws_pos=$(($(echo "$COMP_LINE" | ggrep -abo " aws " | cut -d: -f1) + 1))
# echo "pos $aws_pos"
COMP_POINT=$((${COMP_POINT} - ${aws_pos}))
# echo "${COMP_LINE:aws_pos}"
# for bash:
# COMP_LINE="${COMP_LINE:aws_pos}"
# for zsh:
COMP_LINE="${COMP_LINE:$aws_pos}"
# echo "cl: ${COMP_LINE}, ccw: ${COMP_CWORD}, cp: ${COMP_POINT}"
# COMP_LINE=(${COMP_LINE:2})
# COMP_CWORD=$((${COMP_CWORD} - 2))
# COMP_POINT=$((${COMP_POINT} - 10))
COMPREPLY=($(COMP_POINT=$COMP_POINT COMP_LINE=$COMP_LINE aws_completer))
# COMPREPLY=( $(compgen -Cc $3 -- ${COMP_WORDS[COMP_CWORD]}) )
else
COMPREPLY=()
fi
}
complete -C aws_completer aws
complete -F _delegate envchain
function _executables {
local exclude=$(compgen -abkA function | sort)
local executables=$(
comm -23 <(compgen -c) <(echo $exclude)
type -tP $( comm -12 <(compgen -c) <(echo $exclude) )
)
COMPREPLY=( $(compgen -W "$executables" -- ${COMP_WORDS[COMP_CWORD]}) )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment