3

我目前正在为 Bash 中的 svn 添加对远程 url 完成的支持。我目前正在使用修改后的 bash-completion-svn 来确定何时触发 URls 的完成,看起来像

  svn+ssh://$HOST/$DIR/prefix_string

我设法提取了 URL ( ) 的父目录部分并在其中svn+ssh://$HOST/$DIR调用svn ls

  cur_suffix=prefix_string

现在剩下的就是如何使用 URL 的目录部分和prefix_string在它后面输入的用户(可以是空的)来完成。我可以用compgen它来完成这项工作吗?

如果 url 中的最后一个字符是斜杠,则获取可能完成的列表,如果后缀为空,则获取完整目录。

目前我正在使用

  fix=( $(compgen -o nospace -W "$remote_fileds" -- $cur_suffix))

为 URL 完成完成,例如

svn://HOST/DIR/a 

完成成例如

svn://HIST/DIR/a_file

然而,无论如何它总是选择第一个选项,并且总是插入一个空格,即使我已经写了-o nospace。如果还有其他可能的匹配项(前缀a不是唯一的),我如何防止完成 compgen 完成,以及如何在不移动光标的情况下使这些匹配项打印在光标下方。只能complete这样做吗?

4

1 回答 1

2

For my bash completion scripts, which necessarily run on across a variety of versions of bash with differing levels of support for completion, I use the following idiom to attempt the best "complete" possible, then gracefully fallback to at least something that should work; e.g., assuming a command svn2, and a completion function called _complete_svn2:

complete -o bashdefault -o default -o nospace -F _complete_svn2 svn2 2>/dev/null \
 || complete -o default -o nospace -F _complete_svn2 svn2 2>/dev/null \
 || complete -o default -F _complete_svn2 svn2 2>/dev/null \
 || complete -F _complete_svn2 svn2

Ideally "-o nospace" exists and works, but if it doesn't, you can fallback to "-o default".

Also, the above example uses a function to generate the completions, arbitrarily called _complete_svn2. Perhaps you can try using something like that also. The following example works for me, showing matching options, not adding in the extra space, etc (it intentionally does not complete "-" options):

_complete_svn2() {
    local options="one two three four five"
    local cur="${COMP_WORDS[COMP_CWORD]}"
    COMPREPLY=()
    [[ ${cur} != -* ]] && COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
}

E.g., (and no space is added after "five")

$ svn2 f[tab]
five four
$ svn2 fi[tab]
$ svn2 five
于 2013-12-31T09:55:25.657 回答