Skip to content

Commit

Permalink
Add support for fish completion
Browse files Browse the repository at this point in the history
  • Loading branch information
jle64 committed Nov 14, 2019
1 parent 6944450 commit 31af0fe
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions click/_bashcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,21 @@
compdef %(complete_func)s %(script_names)s
'''

COMPLETION_SCRIPT_FISH = '''
complete --no-files --command %(script_names)s --arguments "(env %(autocomplete_var)s=complete_fish COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) %(script_names)s)"
'''

_invalid_ident_char_re = re.compile(r'[^a-zA-Z0-9_]')


def get_completion_script(prog_name, complete_var, shell):
cf_name = _invalid_ident_char_re.sub('', prog_name.replace('-', '_'))
script = COMPLETION_SCRIPT_ZSH if shell == 'zsh' else COMPLETION_SCRIPT_BASH
if shell == 'zsh':
script = COMPLETION_SCRIPT_ZSH
elif shell == 'fish':
script = COMPLETION_SCRIPT_FISH
else:
script = COMPLETION_SCRIPT_BASH
return (script % {
'complete_func': '_%s_completion' % cf_name,
'script_names': prog_name,
Expand Down Expand Up @@ -287,11 +296,31 @@ def do_complete(cli, prog_name, include_descriptions):
return True


def do_complete_fish(cli, prog_name):
cwords = split_arg_string(os.environ['COMP_WORDS'])
incomplete = os.environ['COMP_CWORD']
args = cwords[1:]
for item in get_choices(cli, prog_name, args, incomplete):
if item[1]:
echo("%(arg)s\t%(desc)s" % {'arg': item[0], 'desc': item[1]})
else:
echo(item[0])

return True


def bashcomplete(cli, prog_name, complete_var, complete_instr):
if complete_instr.startswith('source'):
shell = 'zsh' if complete_instr == 'source_zsh' else 'bash'
if complete_instr == 'source_zsh':
shell = 'zsh'
elif complete_instr == 'source_fish':
shell = 'fish'
else:
shell = 'bash'
echo(get_completion_script(prog_name, complete_var, shell))
return True
elif complete_instr == 'complete_fish':
return do_complete_fish(cli, prog_name)
elif complete_instr == 'complete' or complete_instr == 'complete_zsh':
return do_complete(cli, prog_name, complete_instr == 'complete_zsh')
return False

0 comments on commit 31af0fe

Please sign in to comment.