Skip to content
Myr

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

4 files+33 added−17 removed

modified:click/_bashcomplete.py+22 added-6 removed
8 unmodified lines
9from .types import Choice1011WORDBREAK = '='1213COMPLETION_SCRIPT = '''13COMPLETION_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}2122complete -F %(complete_func)s -o default %(script_names)s22complete -F %(complete_func)s %(script_names)s23'''2425COMPLETION_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}3536compdef %(complete_func)s %(script_names)s37'''3839_invalid_ident_char_re = re.compile(r'[^a-zA-Z0-9_]')404128def get_completion_script(prog_name, complete_var):42def 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 lines
223 return True224225226def 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 False
modified:docs/bashcomplete.rst+9 added-10 removed
73 unmodified lines
74is what you would need to put into your ``.bashrc``::7576 eval "$(_FOO_BAR_COMPLETE=source foo-bar)"7778From this point onwards, your script will have Bash completion enabled.78For zsh users add this to your ``.zshrc``::7980 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"8182From this point onwards, your script will have autocompletion enabled.8384Activation Script85-----------------8687The above activation example will always invoke your application on
5 unmodified lines
93This can be easily accomplished::9495 _FOO_BAR_COMPLETE=source foo-bar > foo-bar-complete.sh9693And then you would put this into your bashrc instead::97For zsh:9895 . /path/to/foo-bar-complete.sh99 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh10097Zsh Compatibility98-----------------101And then you would put this into your .bashrc or .zshrc instead::102100To enable Bash completion in Zsh, add the following lines to your .zshrc:103 . /path/to/foo-bar-complete.sh104102 autoload bashcompinit103 bashcompinit105105See https://github.com/pallets/click/issues/323 for more information on106this issue.
modified:tests/test_compat.py+1 added-1 removed
18 unmodified lines
192021def 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 script
modified:CHANGES+1 added
31 unmodified lines
32 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