问题
我正在用C语言编写一个命令行Twitter客户端。我目前正在尝试为Twitter用户名实现TAB自动补全功能,如下所示:
tweet "@s<TAB>
@sourcebits @spolsky
但是,当字符串中间存在其他单词时,如下所示:
tweet "Foo bar @s<TAB>
Bash会将整个字符串视为一个单词,因此无法实现自动补全。在Bash手册中没有找到任何简单的解决方法,所以我决定绕过它。以下是我尚未完成的解决方案。我只是按空格拆分输入的字符串,取最后一个单词(暂时简化),然后将其发送给compgen
(下面的变量$last_word
)。但是,我需要在TAB自动补全产生的字符串开头附加$prefix
,因为它替换了整个输入字符串(记住:它被视为一个单词)。这就是我的困境所在。
问题
如何解决这个问题?
代码等
__cltwitter_complete () {
local cache="$HOME/.cltwitter_users.cache"
local string=${COMP_WORDS[COMP_CWORD]}
local last_word=${string##* }
local prefix=${string% *}
COMPREPLY=()
#if [ ! -f ${cache} ] || [ "`find ${cache} -mmin +60`" != "" ]; then
# cltwitter-update-cache
#fi
if [[ "$last_word" == \"@* ]]; then # if word is beginning of a quotation
last_word=${last_word:2}
elif [[ "$last_word" == @* ]]; then # if user put '@' in front
last_word=${last_word:1}
fi
COMPREPLY=( $( compgen -W "`cat $cache`" -P @ -- $last_word ) )
}
complete -F __cltwitter_complete tweet
Bash手册相关部分:
COMP_WORDS
一个数组变量(参见下面的Arrays),由当前命令行中单独的单词组成。这些单词是在shell元字符上进行分割,就像shell解析器将它们分开一样。此变量仅在由可编程完成工具调用的shell函数中可用。