Merge pull request #865 from stopthatcow/feature/323
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add native ZSH completion script
authored by David Lord · committed by GitHub · parents bf30737, c99bbc0 · Browse files · 4 files·+33 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+33 added−17 removed
- tests:+1 added−1 removed
- other:+32 added−16 removed
Provenance
- Commit
39e46e0- Tree
7b32614- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
bf30737- Parent 2
c99bbc0
4 files changed+33 added−17 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 == 'source_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.modified:tests/test_compat.py+1 added-1 removed
18 unmodified lines19 20 21 def test_bash_func_name():22 from click._bashcomplete import get_completion_script23- script = get_completion_script('foo-bar baz_blah', '_COMPLETE_VAR').strip()23+ script = get_completion_script('foo-bar baz_blah', '_COMPLETE_VAR', 'bash').strip()24 assert script.startswith('_foo_barbaz_blah_completion()')25 assert '_COMPLETE_VAR=complete $1' in scriptmodified:CHANGES+1 added
31 unmodified lines32 chained commands when a required option/argument was being completed.33 See #790.34 - Allow autocompletion function to determine whether or not to return35 completions that start with the incomplete argument.36+- Add native ZSH autocompletion support See #323.37 - Subcommands that are named by the function now automatically have the38 underscore replaced with a dash. So if you register a function named39 `my_command` it becomes `my-command` in the command line interface.40 - Stdout is now automatically set to non blocking.365 unmodified lines