如何给正确的建议,以选项卡完成后,我的文字中包含冒号 [英] How to give correct suggestions to tab complete when my words contains colons
问题描述
我写一个bash标签完成文件一种实用工具,有时需要在表格上完整的URL:协议://主机:端口
。这包含两个冒号,这已经被证明是tab完成有问题的。这是因为冒号被视为断字处理。我已阅读,我不应该改变 COMP_WORDBREAKS
直接,所以我想用 _get_comp_words_by_ref
和 __ ltrim_colon_completions
的建议在这里:<一href=\"http://stackoverflow.com/questions/10528695/how-to-reset-comp-wordbreaks-without-effecting-other-completion-script/12495480#12495480\">How不影响其他完成脚本来重置COMP_WORDBREAKS?
I am writing a bash tab completion file for a utility that some times requires full URLs on the form: protocol://host:port
. This contains two colons, which have proven to be problematic for tab completion. This is because the colons are treated as word breaks. I have read that I should not change COMP_WORDBREAKS
directly, so I want to use the _get_comp_words_by_ref
and __ltrim_colon_completions
as suggested here: How to reset COMP_WORDBREAKS without effecting other completion script?
这适用于单个冒号,但第二个冒号引起正如本小例子,一个小问题:
这个例子说明了问题。它发生在任何建议数量冒号。
This example shows the problem. It occurs for any number of colons in the suggestions.
[root@2e3e8853cc0c /]# cat /etc/bash_completion.d/foo
_foo()
{
local cur
COMPREPLY=()
_get_comp_words_by_ref -n : -c cur
COMPREPLY=( $(compgen -W "http://host:1234/aaa http://host:1234/bbb http://host:1234/ccc" -- ${cur}) )
__ltrim_colon_completions "$cur"
return 0
}
complete -F _foo foo
击中后富
设置页成功完成公共部分。后按Tab两次,产生了以下建议:
Hitting tab after foo
successfully completes the common part. Hitting tab twice after that, yields the following suggestions:
[root@2e3e8853cc0c /]# foo http://host:1234/
1234/aaa 1234/bbb 1234/ccc
期望的结果是ofcourse:
The desired result is ofcourse:
[root@2e3e8853cc0c /]# foo http://host:1234/
http://host:1234/aaa http://host:1234/bbb http://host:1234/ccc
在此之后,下探,b或c加标签按预期方式工作,它完成完整的URL。
After that, hitting a, b, or c plus tab works as expected, it completes the full URL.
要我怎么能产生正确的输出有什么建议?我是否需要手动更改 COM prePLY
变量,还是我只是使用函数错了?
Any suggestions to how I can produce the right output? Do I need to manually change the COMPREPLY
variable, or am I just using the functions wrong?
推荐答案
我一个解决方案想出了基于我总是用一招。希望这将有助于。
I came up with a solution based on one trick I'm always using. Hope it would help.
_common_prefix()
{
local vname=$1
local first prefix v
shift
if [[ $# -eq 0 ]]; then
local "$vname" && _upvar "$vname" ""
return 0
fi
first=$1
shift
for ((i = 0; i < ${#first}; ++i)); do
prefix=${first:0:i+1}
for v; do
if [[ ${v:0:i+1} != "$prefix" ]]; then
local "$vname" && _upvar "$vname" "${first:0:i}"
return 0
fi
done
done
local "$vname" && _upvar "$vname" "$first"
return 0
}
_bar()
{
local CUR=$2
local cur
local -a compreply=()
local -a urls=(ftp://gnu.org \
http://host1:1234/aaa \
http://host2:1234/bbb \
http://host2:1234/ccc)
_get_comp_words_by_ref -n : -c cur
compreply=( $(compgen -W "${urls[*]}" -- "$cur") )
COMPREPLY=( "${compreply[@]}" )
__ltrim_colon_completions "$cur"
if [[ ${#COMPREPLY[@]} -gt 1 ]]; then
local common_prefix
_common_prefix common_prefix "${COMPREPLY[@]}"
if [[ $common_prefix == "$CUR" ]]; then
COMPREPLY=( "${compreply[@]}" " " )
fi
fi
return 0
}
complete -F _bar bar
以下是它的样子(与测试击
33年4月3日
)
[STEP 101] $ bar <TAB><TAB>
http://host1:1234/aaa http://host2:1234/ccc
ftp://gnu.org http://host2:1234/bbb
[STEP 101] $ bar f<TAB>
[STEP 101] $ bar ftp://gnu.org␣
[STEP 101] $ bar ftp://gnu.org <ENTER>
bash: bar: command not found
[STEP 102] $ bar h<TAB>
[STEP 102] $ bar http://host
[STEP 102] $ bar http://host<TAB><TAB>
http://host2:1234/bbb
http://host1:1234/aaa http://host2:1234/ccc
[STEP 102] $ bar http://host2<TAB>
[STEP 102] $ bar http://host2:1234/
[STEP 102] $ bar http://host2:1234/<TAB><TAB>
http://host2:1234/bbb http://host2:1234/ccc
[STEP 102] $ bar http://host2:1234/b<TAB>
[STEP 102] $ bar http://host2:1234/bbb␣
[STEP 102] $ bar http://host2:1234/bbb <ENTER>
bash: bar: command not found
[STEP 103] $
和实际问题不在于具体谈谈两个或多个冒号。一个冒号有类似的问题了。
And actually the problem is not specific about two or more colons. One colon has the similar problem too.
这篇关于如何给正确的建议,以选项卡完成后,我的文字中包含冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!