Enable native completion for zsh
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicholas Wiles · committed by David Lord · parent bf30737 · Browse files · 2 files·+31 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+31 added−16 removed
- other:+31 added−16 removed
Provenance
- Commit
f17dead- Tree
f7a7312- Author
Nicholas Wiles <nwiles@google.com>- Committer
David Lord <davidism@gmail.com>- Parent
bf30737
2 files changed+31 added−16 removed
modified:click/_bashcomplete.py+22 added-6 removed
8 unmodified lines9 from .types import Choice10 11 WORDBREAK = '='12 13-COMPLETION_SCRIPT = '''13+COMPLETION_SCRIPT_BASH = '''14 %(complete_func)s() {15 local IFS=$'\n'16 COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\17 COMP_CWORD=$COMP_CWORD \\18 %(autocomplete_var)s=complete $1 ) )19 return 020 }21 22-complete -F %(complete_func)s -o default %(script_names)s22+complete -F %(complete_func)s %(script_names)s23 '''24 25+COMPLETION_SCRIPT_ZSH = '''26+%(complete_func)s() {27+ emulate -L zsh28+ local IFS=$'\n'29+ local completions=( $( env COMP_WORDS="${words[*]}" \\30+ COMP_CWORD=$((CURRENT-1)) \\31+ %(autocomplete_var)s="complete" \\32+ %(script_names)s ) )33+ compadd -M 'r:|=* l:|=* r:|=*' -a -- completions 34+}35+36+compdef %(complete_func)s %(script_names)s37+'''38+39 _invalid_ident_char_re = re.compile(r'[^a-zA-Z0-9_]')40 41 28-def get_completion_script(prog_name, complete_var):42+def get_completion_script(prog_name, complete_var, shell):43 cf_name = _invalid_ident_char_re.sub('', prog_name.replace('-', '_'))30- return (COMPLETION_SCRIPT % {44+ script = COMPLETION_SCRIPT_ZSH if shell == 'zsh' else COMPLETION_SCRIPT_BASH45+ return (script % {46 'complete_func': '_%s_completion' % cf_name,47 'script_names': prog_name,48 'autocomplete_var': complete_var,49 }).strip() + ';'173 unmodified lines223 return True224 225 226 def bashcomplete(cli, prog_name, complete_var, complete_instr):212- if complete_instr == 'source':213- echo(get_completion_script(prog_name, complete_var))227+ if complete_instr.startswith('source'):228+ shell = 'zsh' if complete_instr.endswith('zsh') else 'bash'229+ echo(get_completion_script(prog_name, complete_var, shell))230 return True231 elif complete_instr == 'complete':232 return do_complete(cli, prog_name)233 return Falsemodified:docs/bashcomplete.rst+9 added-10 removed
73 unmodified lines74 is what you would need to put into your ``.bashrc``::75 76 eval "$(_FOO_BAR_COMPLETE=source foo-bar)"77 78-From this point onwards, your script will have Bash completion enabled.78+For zsh users add this to your ``.zshrc``::79 80+ eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"81+82+From this point onwards, your script will have autocompletion enabled.83+84 Activation Script85 -----------------86 87 The above activation example will always invoke your application on5 unmodified lines93 This can be easily accomplished::94 95 _FOO_BAR_COMPLETE=source foo-bar > foo-bar-complete.sh96 93-And then you would put this into your bashrc instead::97+For zsh:98 95- . /path/to/foo-bar-complete.sh99+ _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh100 97-Zsh Compatibility98------------------101+And then you would put this into your .bashrc or .zshrc instead::102 100-To enable Bash completion in Zsh, add the following lines to your .zshrc:103+ . /path/to/foo-bar-complete.sh104 102- autoload bashcompinit103- bashcompinit105 105-See https://github.com/pallets/click/issues/323 for more information on106-this issue.