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.
yt-dlp/devscripts/bash-completion.in

85 lines
2.7 KiB
Plaintext

_comp_cmd_yt-dlp() {
# Not using "$2" and "$3" to partially workaround YouTube keyword completions.
local have_bashcomp=0 cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD - 1]}"
# Long options with equals and YouTube keyword completions
# do not work without bash-completion.
if declare -F _comp_initialize &> /dev/null; then
have_bashcomp=1
# Unused variables assigned by _comp_initialize.
# shellcheck disable=SC2034
local words cword comp_args was_split
# ':' must be excluded from COMP_WORDBREAKS to handle :yt* keywords
_comp_initialize -n : -s -- "$@" || return
fi
local filedir_action
case "$prev" in
YT_DLP_FILE_OPTS_CASE )
filedir_action='file'
;;
YT_DLP_DIR_OPTS_CASE )
filedir_action='directory'
;;
* )
;;
esac
local compreply_lines
if [[ "$filedir_action" ]]; then
if (( "$have_bashcomp" )); then
if [[ "$filedir_action" = directory ]]; then
_comp_compgen_filedir -d
else
_comp_compgen_filedir
fi
else
# macOS Bash does not have compopt.
if type compopt &>/dev/null; then
compopt -o filenames
fi
compreply_lines="$(compgen -A "$filedir_action" -- "$cur")"
fi
elif [[ "$cur" = :* ]]; then
# Adding keywords one by one since macOS Bash
# can't initialise a local array from brace expansions.
local k keywords
for k in :ytnotif{,ication}{,s} \
:ytfav{,o{,u}rite}{,s} \
:ytwatchlater \
:ytrec{,ommended} \
:ytsub{,scription}{,s} \
:ythis{,tory}; do
keywords+=("$k")
done
if (( "$have_bashcomp" )); then
_comp_compgen -- -W "${keywords[*]}"
_comp_ltrim_colon_completions "$cur"
else
compreply_lines="$(compgen -W "${keywords[*]}" -- "$cur")"
compreply_lines="${compreply_lines//:/}"
fi
else
local -r opts=( YT_DLP_OPTS_ARRAY )
if type compopt &> /dev/null; then
compopt -o nosort
fi
if (( "$have_bashcomp" )); then
_comp_compgen -- -W "${opts[*]}"
else
compreply_lines="$(compgen -W "${opts[*]}" -- "$cur")"
fi
fi
if [[ "$have_bashcomp" != 1 && "$compreply_lines" ]]; then
COMPREPLY=()
local line
while IFS='' read -r line; do
COMPREPLY+=("$line")
done <<< "$compreply_lines"
fi
} \
&& complete -F _comp_cmd_yt-dlp yt-dlp