如何为fish中HTTPie的`@`路径选项启用制表符补全?

问题描述

HTTPie 接受路径作为带有包含 @ 符号的选项的参数。不幸的是,它们似乎不适用于 fish 中的 shell 补全。相反,该选项被视为一个不透明的字符串。

为了坚持使用 the file upload example from the HTTPie documentation~/files/data.xml文件,我希望能够在键入时使用 tab 完成文件名:

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

但是,不提供完成。

我已经安装了 completions for fish from the HTTPie project 并且它们适用于短参数和长参数。不过,此文件并未指定如何完成 @ 参数。

此外,我考虑过指定自己的补全,但找不到使用任意前缀进行文件补全的方法

如何为 HTTPie 实现这些路径参数的补全?

解决方法

目前,HTTPie 的fish 补全没有用@ 补全文件路径参数。对此有一个更一般的 GitHub Issue open

如果这是您想为自己或为项目而做的事情,您可以从 HTTPie plugin for zsh+ohmyzsh 中获得一些实现您所需行为的鱼实现的灵感.

,

我设法使路径参数的选项卡完成与一些警告一起工作。

这将添加完成:

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

具有以下功能:

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

需要注意的是,这不会扩展任何变量,也不会扩展波浪号 ~。它基本上只适用于普通路径 - 相对或绝对。