Bash补全:分别显示不同来源的结果

Bash补全:分别显示不同来源的结果

我已关注本教程编写我的 bash 补全函数。效果很好,但我想知道是否可以单独显示来自不同来源的完成情况。

例如,假设我有一个以目录作为参数的脚本,并且我想自动完成可能的目录(由 列出get_dirs):

function _testing_command()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local prev=${COMP_WORDS[COMP_CWORD-1]}

    case "$prev" in
        testing )
            COMPREPLY=( $(compgen -W "$(get_dirs)" -- $cur) )
            ;;
    esac
}
complete -F _testing_command testing

这工作正常并正确自动完成目录:

$ testing <TAB><TAB>
dir1 dir3 dir5
$ testing

但是,如果我现在有第二组目录(由 列出get_dirs_2),我想将其包含在自动完成输出中,该怎么办?我可以执行以下操作:

function _testing_command()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local prev=${COMP_WORDS[COMP_CWORD-1]}

    case "$prev" in
        testing )
            COMPREPLY=( $(compgen -W "$(get_dirs) $(get_dirs_2)" -- $cur) )
            ;;
    esac
}
complete -F _testing_command testing

这会给我这个输出:

$ testing <TAB><TAB>
dir1 dir2 dir3 dir4 dir5 dir6
$ testing

从技术上讲,这是可以的,因为自动补全功能将适用于两个来源的目录。但我希望输出以某种方式表示哪个目录来自哪个位置,例如:

$ testing <TAB><TAB>
from loc1:
dir1 dir3 dir5
from loc2:
dir2 dir4 dir6
$ testing

我可以控制输出格式吗?或者是不可能使用complete/compgen 函数来实现类似的功能?

相关内容