From 39c1470ea6a6fc869cfc219c1ecbc1870f84d069 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 7 Jun 2022 11:57:48 -0700 Subject: [PATCH] add special options to bash completion --- jc/shell_completions.py | 42 ++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/jc/shell_completions.py b/jc/shell_completions.py index 198e2922..6edef54d 100644 --- a/jc/shell_completions.py +++ b/jc/shell_completions.py @@ -8,15 +8,23 @@ from .lib import all_parser_info bash_template = Template('''\ _jc() { - local cur prev words cword jc_commands jc_parsers jc_options + local cur prev words cword jc_commands jc_parsers jc_options jc_special_options jc_commands=(${bash_commands}) jc_parsers=(${bash_arguments}) jc_options=(${bash_options}) + jc_special_options=(${bash_special_opts}) COMPREPLY=() _get_comp_words_by_ref cur prev words cword + # if special options are found anywhere in the line, then no more completions + for i in "$${words[@]}"; do + if [[ " $${jc_special_options[*]} " =~ " $${i} " ]]; then + return 0 + fi + done + # if magic command is found anywhere in the line, use called command's autocompletion for i in "$${words[@]}"; do if [[ " $${jc_commands[*]} " =~ " $${i} " ]]; then @@ -35,7 +43,7 @@ _jc() done # default completion - COMPREPLY=( $$( compgen -W "$${jc_options[*]} $${jc_parsers[*]} $${jc_commands[*]}" \\ + COMPREPLY=( $$( compgen -W "$${jc_options[*]} $${jc_special_options[*]} $${jc_parsers[*]} $${jc_commands[*]}" \\ -- "$${cur}" ) ) } && complete -F _jc jc @@ -46,23 +54,30 @@ zsh_template = Template('''\ #compdef jc _jc() { + + # if magic command is found anywhere in the line, use called command's autocompletion + + # if a parser arg is found anywhere in the line, only show options + + # default completion # autogenerate completions based on jc --help output - _arguments -- + # _arguments -- # add commands supported by magic syntax - local -a commands - commands=( - # e.g. 'arp:run arp with magic syntax.' - ${zsh_commands} - ) + # local -a commands + # commands=( + # # e.g. 'arp:run arp with magic syntax.' + # ${zsh_commands} + # ) - _describe -t commands 'commands' commands - return 0 + # _describe -t commands 'commands' commands + # return 0 } _jc ''') +special_options = ['--about', '-a', '--version', '-v', '--bash-comp', '-B', '--zsh-comp', '-Z'] def get_commands(): command_list = [] @@ -101,9 +116,14 @@ def gen_zsh_command_descriptions(command_list): def bash_completion(): args = ' '.join(get_arguments()) - options = ' '.join(get_options()) + opts_no_special = get_options() + for option in special_options: + opts_no_special.remove(option) + options = ' '.join(opts_no_special) + s_options = ' '.join(special_options) commands = ' '.join(get_commands()) return bash_template.substitute(bash_arguments=args, + bash_special_opts=s_options, bash_options=options, bash_commands=commands)