create working file completion

This commit is contained in:
Isaac 2023-05-04 20:17:26 -07:00
parent f12a10d888
commit 639e88262e
No known key found for this signature in database
GPG Key ID: E69FB5A841448A48
1 changed files with 40 additions and 16 deletions

View File

@ -76,11 +76,17 @@ def conditionally_emit_file_completion(action: Action):
''' '''
if not action.metavar: if not action.metavar:
return '' return ''
args = ' '.join(action.option_strings) args = ' '.join(action.option_strings)
return dedent(f''' return dedent(
complete -c borgmatic -a {args} -Fr -n "__borgmatic_last_arg {args}"''') f'''
complete -c borgmatic -a '{args}' -Fr -n "__borgmatic_last_arg {args}"'''
)
def dedent_strip_as_tuple(string: str):
return (dedent(string).strip("\n"),)
def fish_completion(): def fish_completion():
@ -94,22 +100,40 @@ def fish_completion():
# Avert your eyes. # Avert your eyes.
return '\n'.join( return '\n'.join(
( dedent_strip_as_tuple(
'function __borgmatic_check_version', f'''
' set this_filename (status current-filename)', function __borgmatic_check_version
' set this_script (cat $this_filename 2> /dev/null)', set this_filename (status current-filename)
' set installed_script (borgmatic --fish-completion 2> /dev/null)', set this_script (cat $this_filename 2> /dev/null)
' if [ "$this_script" != "$installed_script" ] && [ "$installed_script" != "" ]', set installed_script (borgmatic --fish-completion 2> /dev/null)
f''' echo "{upgrade_message( if [ "$this_script" != "$installed_script" ] && [ "$installed_script" != "" ]
echo "{upgrade_message(
'fish', 'fish',
'borgmatic --fish-completion | sudo tee $this_filename', 'borgmatic --fish-completion | sudo tee $this_filename',
'$this_filename', '$this_filename',
)}"''', )}"
' end', end
'end', end
'__borgmatic_check_version &', __borgmatic_check_version &
function __borgmatic_last_arg --description 'Check if any of the given arguments are the last on the command line'
set -l all_args (commandline -poc)
# premature optimization to avoid iterating all args if there aren't enough
# to have a last arg beyond borgmatic
if [ (count $all_args) -lt 2 ]
return 1
end
for arg in $argv
if [ "$arg" = "$all_args[-1]" ]
return 0
end
end
return 1
end
set --local subparser_condition "not __fish_seen_subcommand_from {all_subparsers}"
'''
) )
+ (f'''set --local subparser_condition "not __fish_seen_subcommand_from {all_subparsers}"''',)
+ ('\n# subparser completions',) + ('\n# subparser completions',)
+ tuple( + tuple(
f'''complete -c borgmatic -f -n "$subparser_condition" -a '{action_name}' -d {shlex.quote(subparser.description)}''' f'''complete -c borgmatic -f -n "$subparser_condition" -a '{action_name}' -d {shlex.quote(subparser.description)}'''