Use Python sorting order for ZSH completions. Fixes #1047.
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 be28b6c · Browse files · 3 files·+30 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−9 removed
- other:+30 added−9 removed
Provenance
- Commit
b073abe- Tree
749080e- Author
Nicholas Wiles <nwiles@google.com>- Committer
David Lord <davidism@gmail.com>- Parent
be28b6c
3 files changed+30 added−9 removed
modified:click/_bashcomplete.py+18 added-4 removed
8 unmodified lines9 from .types import Choice10 11 WORDBREAK = '='12 13+# Note, only BASH version 4.4 and later have the nosort option.14 COMPLETION_SCRIPT_BASH = '''15 %(complete_func)s() {16 local IFS=$'\n'17 COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\1 unmodified line19 %(autocomplete_var)s=complete $1 ) )20 return 021 }22 23+%(complete_func)setup() {24+ local COMPLETION_OPTIONS=""25+ local BASH_VERSION_ARR=(${BASH_VERSION//./ })26+ if [ ${BASH_VERSION_ARR[0]} -ge 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ];then27+ COMPLETION_OPTIONS="-o nosort"28+ fi29+22-complete -F %(complete_func)s %(script_names)s30+ complete $COMPLETION_OPTIONS -F %(complete_func)s %(script_names)s31+}32+33+%(complete_func)setup34 '''35 36 COMPLETION_SCRIPT_ZSH = '''37 %(complete_func)s() {13 unmodified lines51 fi52 done53 54 if [ -n "$completions_with_descriptions" ]; then44- _describe '' completions_with_descriptions55+ _describe -V unsorted completions_with_descriptions -U -Q56 fi57+58 if [ -n "$completions" ]; then47- compadd -M 'r:|=* l:|=* r:|=*' -a completions59+ compadd -U -V unsorted -Q -a completions60 fi61+ compstate[insert]="automenu"62 }63 64 compdef %(complete_func)s %(script_names)s65 '''178 unmodified lines244 if is_incomplete_argument(ctx.params, param):245 return get_user_autocompletions(ctx, all_args, incomplete, param)246 247 add_subcommand_completions(ctx, incomplete, completions)235- return completions248+ # Sort before returning so that proper ordering can be enforced in custom types.249+ return sorted(completions)250 251 252 def do_complete(cli, prog_name, include_descriptions):253 cwords = split_arg_string(os.environ['COMP_WORDS'])24 unmodified linesmodified:examples/bashcompletion/bashcompletion.py+9 added-5 removed
6 unmodified lines7 pass8 9 10 def get_env_vars(ctx, args, incomplete):11+ # Completions returned as strings do not have a description displayed.12 for key in os.environ.keys():13 if incomplete in key:14 yield key15 10 unmodified lines26 pass27 28 29 def list_users(ctx, args, incomplete):29- # Here you can generate completions dynamically30- users = ['bob', 'alice']31- for user in users:32- if user.startswith(incomplete):33- yield user30+ # You can generate completions with descriptions by returning31+ # tuples in the form (completion, description).32+ users = [('bob', 'butcher'),33+ ('alice', 'baker'),34+ ('jerry', 'candlestick maker')]35+ # Ths will allow completion matches based on matches within the description string too!36+ return [user for user in users if incomplete in user[0] or incomplete in user[1]]37 38 39 @group.command(help='Choose a user')40 @click.argument("user", type=click.STRING, autocompletion=list_users)41 def subcmd(user):42 click.echo('Chosen user is %s' % user)43 44+45 cli.add_command(group)modified:CHANGES.rst+3 added
79 unmodified lines80 - Fixed issues where ``fd`` was undefined. (`#1007`_)81 - Fix formatting for short help. (`#1008`_)82 - Document how ``auto_envar_prefix`` works with command groups. (`#1011`_)83 - Don't add newlines by default for progress bars. (`#1013`_)84+- Use Python sorting order for ZSH completions. (`#1047`_, `#1059`_)85 - Document that parameter names are lowercased by default. (`#1055`_)86 - Subcommands that are named by the function now automatically have the underscore replaced with a dash. If you register a function named ``my_command`` it becomes ``my-command`` in the command line interface.87 88 .. _#202: https://github.com/pallets/click/issues/202101 unmodified lines190 .. _#1016: https://github.com/pallets/click/pull/1016191 .. _#1020: https://github.com/pallets/click/pull/1020192 .. _#1022: https://github.com/pallets/click/pull/1022193 .. _#1027: https://github.com/pallets/click/pull/1027194+.. _#1047: https://github.com/pallets/click/pull/1047195 .. _#1055: https://github.com/pallets/click/pull/1055196+.. _#1059: https://github.com/pallets/click/pull/1059197 198 199 Version 6.7200 -----------343 unmodified lines