1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-19 00:17:51 +02:00

add special options to bash completion

This commit is contained in:
Kelly Brazil
2022-06-07 11:57:48 -07:00
parent e48b99f1c1
commit 39c1470ea6

View File

@ -8,15 +8,23 @@ from .lib import all_parser_info
bash_template = Template('''\ bash_template = Template('''\
_jc() _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_commands=(${bash_commands})
jc_parsers=(${bash_arguments}) jc_parsers=(${bash_arguments})
jc_options=(${bash_options}) jc_options=(${bash_options})
jc_special_options=(${bash_special_opts})
COMPREPLY=() COMPREPLY=()
_get_comp_words_by_ref cur prev words cword _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 # if magic command is found anywhere in the line, use called command's autocompletion
for i in "$${words[@]}"; do for i in "$${words[@]}"; do
if [[ " $${jc_commands[*]} " =~ " $${i} " ]]; then if [[ " $${jc_commands[*]} " =~ " $${i} " ]]; then
@ -35,7 +43,7 @@ _jc()
done done
# default completion # default completion
COMPREPLY=( $$( compgen -W "$${jc_options[*]} $${jc_parsers[*]} $${jc_commands[*]}" \\ COMPREPLY=( $$( compgen -W "$${jc_options[*]} $${jc_special_options[*]} $${jc_parsers[*]} $${jc_commands[*]}" \\
-- "$${cur}" ) ) -- "$${cur}" ) )
} && } &&
complete -F _jc jc complete -F _jc jc
@ -46,23 +54,30 @@ zsh_template = Template('''\
#compdef jc #compdef jc
_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 # autogenerate completions based on jc --help output
_arguments -- # _arguments --
# add commands supported by magic syntax # add commands supported by magic syntax
local -a commands # local -a commands
commands=( # commands=(
# e.g. 'arp:run arp with magic syntax.' # # e.g. 'arp:run arp with magic syntax.'
${zsh_commands} # ${zsh_commands}
) # )
_describe -t commands 'commands' commands # _describe -t commands 'commands' commands
return 0 # return 0
} }
_jc _jc
''') ''')
special_options = ['--about', '-a', '--version', '-v', '--bash-comp', '-B', '--zsh-comp', '-Z']
def get_commands(): def get_commands():
command_list = [] command_list = []
@ -101,9 +116,14 @@ def gen_zsh_command_descriptions(command_list):
def bash_completion(): def bash_completion():
args = ' '.join(get_arguments()) 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()) commands = ' '.join(get_commands())
return bash_template.substitute(bash_arguments=args, return bash_template.substitute(bash_arguments=args,
bash_special_opts=s_options,
bash_options=options, bash_options=options,
bash_commands=commands) bash_commands=commands)