@@ -1958,3 +1958,67 @@ async def check_img_mimetype(img_url: str) -> tuple[bool, str, str]:
19581958 return True , response .content_type , ""
19591959 except aiohttp .ClientError :
19601960 return False , "" , "Could not fetch image"
1961+
1962+
1963+ # Note, only BASH version 4.4 and later have the nosort option.
1964+ COMPLETION_SCRIPT_BASH = """
1965+ %(complete_func)s() {
1966+ local IFS=$'\n '
1967+ COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\
1968+ COMP_CWORD=$COMP_CWORD \\
1969+ %(autocomplete_var)s=complete $1 ) )
1970+ return 0
1971+ }
1972+ %(complete_func)setup() {
1973+ local COMPLETION_OPTIONS=""
1974+ local BASH_VERSION_ARR=(${BASH_VERSION//./ })
1975+ # Only BASH version 4.4 and later have the nosort option.
1976+ if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then
1977+ COMPLETION_OPTIONS="-o nosort"
1978+ fi
1979+ complete $COMPLETION_OPTIONS -F %(complete_func)s %(script_names)s
1980+ }
1981+ %(complete_func)setup
1982+ """
1983+
1984+ COMPLETION_SCRIPT_ZSH = """
1985+ %(complete_func)s() {
1986+ local -a completions
1987+ local -a completions_with_descriptions
1988+ local -a response
1989+ response=("${(@f)$( env COMP_WORDS=\" ${words[*]}\" \\
1990+ COMP_CWORD=$((CURRENT-1)) \\
1991+ %(autocomplete_var)s=\" complete_zsh\" \\
1992+ %(script_names)s )}")
1993+ for key descr in ${(kv)response}; do
1994+ if [[ "$descr" == "_" ]]; then
1995+ completions+=("$key")
1996+ else
1997+ completions_with_descriptions+=("$key":"$descr")
1998+ fi
1999+ done
2000+ if [ -n "$completions_with_descriptions" ]; then
2001+ _describe -V unsorted completions_with_descriptions -U -Q
2002+ fi
2003+ if [ -n "$completions" ]; then
2004+ compadd -U -V unsorted -Q -a completions
2005+ fi
2006+ compstate[insert]="automenu"
2007+ }
2008+ compdef %(complete_func)s %(script_names)s
2009+ """
2010+
2011+ _invalid_ident_char_re = re .compile (r"[^a-zA-Z0-9_]" )
2012+
2013+
2014+ def get_completion_script (prog_name , complete_var , shell ):
2015+ cf_name = _invalid_ident_char_re .sub ("" , prog_name .replace ("-" , "_" ))
2016+ script = COMPLETION_SCRIPT_ZSH if shell == "zsh" else COMPLETION_SCRIPT_BASH
2017+ return (
2018+ script
2019+ % {
2020+ "complete_func" : "_%s_completion" % cf_name ,
2021+ "script_names" : prog_name ,
2022+ "autocomplete_var" : complete_var ,
2023+ }
2024+ ).strip () + ";"
0 commit comments