You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dotbare/pkg/completion/bash/dotbare

154 lines
4.6 KiB
Plaintext

# shellcheck disable=SC2207
_dotbare_completion()
{
local IFS=$'\n' subcommands curr prev options verbose_options suggestions
curr="${COMP_WORDS[$COMP_CWORD]}"
prev="${COMP_WORDS[$COMP_CWORD-1]}"
if [[ "$COMP_CWORD" -eq "1" ]]; then
subcommands=$(
dotbare -h \
| awk '{
if ($0 ~ /^ f.*/) {
gsub(/^ /, "", $0)
gsub(/\t\t/, " ", $0)
print $0
}
}'
)
options=$(
dotbare -h \
| awk '{
if ($0 ~ /^ -.*/) {
gsub(/,/, " ", $0)
gsub(/^ /, "", $0)
gsub(/\t\t/, " ", $0)
$2=""
print $0
}
}'
)
verbose_options=$(dotbare -h | awk '$0 ~ /^ -.*/ {print $2}')
if [[ "${curr}" == --* ]]; then
suggestions=($(compgen -W "${verbose_options}" -- "${curr}"))
elif [[ "${curr}" == -* ]]; then
suggestions=($(compgen -W "${options}" -- "${curr}"))
else
suggestions=($(compgen -W "${subcommands}" -- "${curr}"))
fi
elif [[ "${COMP_CWORD}" -eq 2 && "${prev}" == "-g" || "${prev}" == "--git" ]]; then
subcommands=$(
dotbare -h \
| awk '{
if ($0 ~ /^ f.*/) {
if ($0 ~ /^ f(backup|init|upgrade).*/) {
next
}
gsub(/^ /, "", $0)
gsub(/\t\t/, " ", $0)
print $0
}
}'
)
suggestions=($(compgen -W "${subcommands}" -- "${curr}"))
elif [[ "${COMP_WORDS[1]}" == "-g" || "${COMP_WORDS[1]}" == "--git" ]] && [[ "${COMP_CWORD}" -gt 2 ]]; then
if [[ "${curr}" == --* && "${prev}" != "-h" && "${prev}" != "--help" ]]; then
verbose_options=$(
dotbare "${COMP_WORDS[2]}" -h 2> /dev/null \
| awk '{
if ($0 ~ /^ -p PATH/) {
next
} else if ($0 ~ /^ -u URL/) {
next
} else if ($0 ~ /^ -*/) {
print $2
}
}'
)
suggestions=($(compgen -W "${verbose_options}" -- "${curr}"))
elif [[ "${prev}" != "-h" && "${prev}" != "--help" ]]; then
options=$(
dotbare "${COMP_WORDS[2]}" -h 2> /dev/null \
| awk '{
gsub(/,/, " ", $0)
if ($0 ~ /^ -p PATH/) {
next
} else if ($0 ~ /^ -u URL/) {
next
} else if ($0 ~ /^ -*/) {
gsub(/^ /, "", $0)
gsub(/\t/, " ", $0)
$2=""
print $0
}
}'
)
suggestions=($(compgen -W "${options}" -- "${curr}"))
else
return
fi
elif [[ "${COMP_WORDS[1]}" == "fbackup" ]] && [[ "${prev}" == "-p" || "${prev}" == "--path" ]]; then
COMPREPLY=($(compgen -d -- "${curr}"))
return
elif [[ "${COMP_WORDS[1]}" == "finit" ]] && [[ "${prev}" == "-u" || "${prev}" == "--url" ]]; then
return
elif [[ "${curr}" == --* && "${prev}" != "-h" && "${prev}" != "--help" ]]; then
verbose_options=$(
dotbare "${COMP_WORDS[1]}" -h 2> /dev/null \
| awk '{
if ($0 ~ /^ -p PATH/) {
print "--path"
} else if ($0 ~ /^ -u URL/) {
print "--url"
} else if ($0 ~ /^ -c COL/) {
print "--col"
} else if ($0 ~ /^ -*/) {
print $2
}
}'
)
suggestions=($(compgen -W "${verbose_options}" -- "${curr}"))
elif [[ "${prev}" != "-h" && "${prev}" != "--help" ]]; then
options=$(
dotbare "${COMP_WORDS[1]}" -h 2> /dev/null \
| awk '{
gsub(/,/, " ", $0)
if ($0 ~ /^ -p PATH/) {
gsub(/^ -p PATH --path PATH/, "-p", $0)
gsub(/\t/, " ", $0)
print $0
} else if ($0 ~ /^ -u URL/) {
gsub(/^ -u URL --url URL/, "-u", $0)
gsub(/\t/, " ", $0)
print $0
} else if ($0 ~ /^ -c COL/) {
gsub(/^ -c COL --col COL/, "-c", $0)
gsub(/\t/, " ", $0)
print $0
} else if ($0 ~ /^ -*/) {
gsub(/^ /, "", $0)
gsub(/\t/, " ", $0)
$2=""
print $0
}
}'
)
suggestions=($(compgen -W "${options}" -- "${curr}"))
fi
if [[ "${#suggestions[*]}" -eq 1 ]]; then
COMPREPLY=("${suggestions[@]%% *}")
else
for i in "${!suggestions[@]}"; do
suggestions[$i]="$(printf '%*s' "-$COLUMNS" "${suggestions[$i]}")"
done
COMPREPLY=("${suggestions[@]}")
fi
}
complete -F _dotbare_completion dotbare