__gc_debug()
{
if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then
echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
fi
}
__gc_init_completion()
{
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}
__gc_get_completion_results() {
local requestComp lastParam lastChar args
args=("${words[@]:1}")
requestComp="${words[0]} __complete ${args[*]}"
lastParam=${words[$((${#words[@]}-1))]}
lastChar=${lastParam:$((${#lastParam}-1)):1}
__gc_debug "lastParam ${lastParam}, lastChar ${lastChar}"
if [[ -z ${cur} && ${lastChar} != = ]]; then
__gc_debug "Adding extra empty parameter"
requestComp="${requestComp} ''"
fi
if [[ ${cur} == -*=* ]]; then
cur="${cur#*=}"
fi
__gc_debug "Calling ${requestComp}"
out=$(eval "${requestComp}" 2>/dev/null)
directive=${out##*:}
out=${out%:*}
if [[ ${directive} == "${out}" ]]; then
directive=0
fi
__gc_debug "The completion directive is: ${directive}"
__gc_debug "The completions are: ${out}"
}
__gc_process_completion_results() {
local shellCompDirectiveError=1
local shellCompDirectiveNoSpace=2
local shellCompDirectiveNoFileComp=4
local shellCompDirectiveFilterFileExt=8
local shellCompDirectiveFilterDirs=16
local shellCompDirectiveKeepOrder=32
if (((directive & shellCompDirectiveError) != 0)); then
__gc_debug "Received error from custom completion go code"
return
else
if (((directive & shellCompDirectiveNoSpace) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then
__gc_debug "Activating no space"
compopt -o nospace
else
__gc_debug "No space directive not supported in this version of bash"
fi
fi
if (((directive & shellCompDirectiveKeepOrder) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then
if [[ ${BASH_VERSINFO[0]} -lt 4 || ( ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 4 ) ]]; then
__gc_debug "No sort directive not supported in this version of bash"
else
__gc_debug "Activating keep order"
compopt -o nosort
fi
else
__gc_debug "No sort directive not supported in this version of bash"
fi
fi
if (((directive & shellCompDirectiveNoFileComp) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then
__gc_debug "Activating no file completion"
compopt +o default
else
__gc_debug "No file completion directive not supported in this version of bash"
fi
fi
fi
local completions=()
local activeHelp=()
__gc_extract_activeHelp
if (((directive & shellCompDirectiveFilterFileExt) != 0)); then
local fullFilter filter filteringCmd
for filter in ${completions[*]}; do
fullFilter+="$filter|"
done
filteringCmd="_filedir $fullFilter"
__gc_debug "File filtering command: $filteringCmd"
$filteringCmd
elif (((directive & shellCompDirectiveFilterDirs) != 0)); then
local subdir
subdir=${completions[0]}
if [[ -n $subdir ]]; then
__gc_debug "Listing directories in $subdir"
pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return
else
__gc_debug "Listing directories in ."
_filedir -d
fi
else
__gc_handle_completion_types
fi
__gc_handle_special_char "$cur" :
__gc_handle_special_char "$cur" =
if ((${#activeHelp[*]} != 0)); then
printf "\n";
printf "%s\n" "${activeHelp[@]}"
printf "\n"
if (x=${PS1@P}) 2> /dev/null; then
printf "%s" "${PS1@P}${COMP_LINE[@]}"
else
printf "%s" "${COMP_LINE[@]}"
fi
fi
}
__gc_extract_activeHelp() {
local activeHelpMarker="_activeHelp_ "
local endIndex=${#activeHelpMarker}
while IFS='' read -r comp; do
if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then
comp=${comp:endIndex}
__gc_debug "ActiveHelp found: $comp"
if [[ -n $comp ]]; then
activeHelp+=("$comp")
fi
else
completions+=("$comp")
fi
done <<<"${out}"
}
__gc_handle_completion_types() {
__gc_debug "__gc_handle_completion_types: COMP_TYPE is $COMP_TYPE"
case $COMP_TYPE in
37|42)
local tab=$'\t' comp
while IFS='' read -r comp; do
[[ -z $comp ]] && continue
comp=${comp%%$tab*}
if [[ $comp == "$cur"* ]]; then
COMPREPLY+=("$comp")
fi
done < <(printf "%s\n" "${completions[@]}")
;;
*)
__gc_handle_standard_completion_case
;;
esac
}
__gc_handle_standard_completion_case() {
local tab=$'\t' comp
if [[ "${completions[*]}" != *$tab* ]]; then
IFS=$'\n' read -ra COMPREPLY -d '' < <(compgen -W "${completions[*]}" -- "$cur")
return 0
fi
local longest=0
local compline
while IFS='' read -r compline; do
[[ -z $compline ]] && continue
comp=${compline%%$tab*}
[[ $comp == "$cur"* ]] || continue
COMPREPLY+=("$compline")
if ((${#comp}>longest)); then
longest=${#comp}
fi
done < <(printf "%s\n" "${completions[@]}")
if ((${#COMPREPLY[*]} == 1)); then
__gc_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
comp="${COMPREPLY[0]%%$tab*}"
__gc_debug "Removed description from single completion, which is now: ${comp}"
COMPREPLY[0]=$comp
else
__gc_format_comp_descriptions $longest
fi
}
__gc_handle_special_char()
{
local comp="$1"
local char=$2
if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then
local word=${comp%"${comp##*${char}}"}
local idx=${#COMPREPLY[*]}
while ((--idx >= 0)); do
COMPREPLY[idx]=${COMPREPLY[idx]#"$word"}
done
fi
}
__gc_format_comp_descriptions()
{
local tab=$'\t'
local comp desc maxdesclength
local longest=$1
local i ci
for ci in ${!COMPREPLY[*]}; do
comp=${COMPREPLY[ci]}
if [[ "$comp" == *$tab* ]]; then
__gc_debug "Original comp: $comp"
desc=${comp#*$tab}
comp=${comp%%$tab*}
maxdesclength=$(( COLUMNS - longest - 4 ))
if ((maxdesclength > 8)); then
for ((i = ${#comp} ; i < longest ; i++)); do
comp+=" "
done
else
maxdesclength=$(( COLUMNS - ${#comp} - 4 ))
fi
if ((maxdesclength > 0)); then
if ((${#desc} > maxdesclength)); then
desc=${desc:0:$(( maxdesclength - 1 ))}
desc+="…"
fi
comp+=" ($desc)"
fi
COMPREPLY[ci]=$comp
__gc_debug "Final comp: $comp"
fi
done
}
__start_gc()
{
local cur prev words cword split
COMPREPLY=()
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n =: || return
else
__gc_init_completion -n =: || return
fi
__gc_debug
__gc_debug "========= starting completion logic =========="
__gc_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword"
words=("${words[@]:0:$cword+1}")
__gc_debug "Truncated words[*]: ${words[*]},"
local out directive
__gc_get_completion_results
__gc_process_completion_results
}
if [[ $(type -t compopt) = "builtin" ]]; then
complete -o default -F __start_gc gc
else
complete -o default -o nospace -F __start_gc gc
fi