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

217 Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

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.

1
On

Currently, the fish completions for HTTPie do not have completion for file path arguments with @. There is a more general GitHub Issue open about this.

If this is something you'd like to work on, either for yourself or for the project, you might be able draw some inspiration for the fish implementation from an HTTPie plugin for zsh+ohmyzsh that achieves your desired behaviour.