Search code examples
fishtab-completionhttpie

How can I enable tab-completion for `@` path options to HTTPie in fish?


HTTPie accepts paths as arguments with options that include the @ sign. Unfortunately, they don't seem to work with shell completions in fish. Instead, the option is treated as an opaque string.

To stick with the file upload example from the HTTPie documentation with a file at ~/files/data.xml, I would expect to be able to tab complete the file name when typing:

http -f POST pie.dev/post name='John Smith' cv@~/files/da<TAB>

However, no completion is offered.

I have installed the completions for fish from the HTTPie project and they work for short and long arguments. This file does not specify how to complete the @ arguments though.

In addition, I looked into specifying my own completions but I am not able to find a way of getting to work file completions with the arbitrary prefix.

How could I implement a completion for these path arguments for HTTPie?


Solution

  • I managed to get the tab completion of the path arguments working with some caveats.

    This adds the completion:

    complete -c http --condition "__is_httpie_path_argument" -a "(__complete_httpie_path_argument (commandline -t))"
    

    With the following functions:

    function __is_httpie_path_argument
        set -l arg (commandline -t)
        __match_httpie_path_argument --quiet -- $arg
    end
    
    function __match_httpie_path_argument
        string match --entire --regex '^([^@:=]*)(@|=@|:=@)(.*)$' $argv
    end
    
    function __complete_httpie_path_argument
        __complete_httpie_path_argument_helper (__match_httpie_path_argument -- $argv[1])
    end
    
    function __complete_httpie_path_argument_helper
        set -l arg $argv[1]
        set -l field $argv[2]
        set -l operator $argv[3]
        set -l path $argv[4]
    
        string collect $field$operator(__fish_complete_path $path)
    end
    

    The caveat is that this does not expand any variables nor the tilde ~. It essentially only works for plain paths — relative or absolute.