mirror of https://github.com/yt-dlp/yt-dlp
Merge 1ef64f14a9 into 27afb31edc
commit
e0dbf8dbd0
@ -1,33 +1,84 @@
|
||||
__yt_dlp()
|
||||
{
|
||||
local cur prev opts fileopts diropts keywords
|
||||
COMPREPLY=()
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||
opts="{{flags}}"
|
||||
keywords=":ytfavorites :ytrecommended :ytsubscriptions :ytwatchlater :ythistory"
|
||||
fileopts="-a|--batch-file|--download-archive|--cookies|--load-info-json"
|
||||
diropts="--cache-dir"
|
||||
_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]}"
|
||||
|
||||
if [[ ${prev} =~ ${fileopts} ]]; then
|
||||
local IFS=$'\n'
|
||||
type compopt &>/dev/null && compopt -o filenames
|
||||
COMPREPLY=( $(compgen -f -- ${cur}) )
|
||||
return 0
|
||||
elif [[ ${prev} =~ ${diropts} ]]; then
|
||||
local IFS=$'\n'
|
||||
type compopt &>/dev/null && compopt -o dirnames
|
||||
COMPREPLY=( $(compgen -d -- ${cur}) )
|
||||
return 0
|
||||
# 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
|
||||
|
||||
if [[ ${cur} =~ : ]]; then
|
||||
COMPREPLY=( $(compgen -W "${keywords}" -- ${cur}) )
|
||||
return 0
|
||||
elif [[ ${cur} == * ]] ; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||
return 0
|
||||
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
|
||||
}
|
||||
|
||||
complete -F __yt_dlp yt-dlp
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue