[英]How to make two programs to use only one auto-completion function in bash?
I followed this tutorial to setup auto-completion functions for figlet
/ toilet
. 我按照本教程设置了
figlet
/ toilet
自动完成功能。
# bash completion for figlet/toilet
have figlet &&
_figlet()
{
local prev cur opts
_get_comp_words_by_ref cur prev
opts="-f"
COMPREPLY=()
case $prev in
-f)
local running=$(find /usr/share/figlet -name '*.flf' -printf '%P\n' | sed 's/\.flf$//')
COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
return 0
;;
*)
;;
esac
COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
return 0
} &&
complete -F _figlet figlet
###################################################################################################
have toilet &&
_toilet()
{
local prev cur opts
_get_comp_words_by_ref cur prev
opts="-f"
COMPREPLY=()
case $prev in
-f)
local running=$(find /usr/share/figlet -name '*.[tf]lf' -printf '%P\n' | sed 's/\.[tf]lf$//')
COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
return 0
;;
*)
;;
esac
COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
return 0
} &&
complete -F _toilet toilet
_figlet
and _toilet
are almost identical except the pattern in find
/ sed
commands. _figlet
和_toilet
几乎相同,除了find
/ sed
命令中的模式。
How to extract a function called _figlet_toilet
which takes a pattern as argument? 如何提取以模式为参数的
_figlet_toilet
函数?
From: http://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html 来自: http : //www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html
After these matches have been generated, any shell function or command specified with the -F and -C options is invoked.
生成这些匹配项后,将调用由-F和-C选项指定的任何shell函数或命令。 When the command or function is invoked, the COMP_LINE, COMP_POINT, COMP_KEY, and COMP_TYPE variables are assigned values as described above (see Bash Variables).
调用命令或函数时,将如上所述为COMP_LINE,COMP_POINT,COMP_KEY和COMP_TYPE变量分配值(请参见Bash变量)。 If a shell function is being invoked, the COMP_WORDS and COMP_CWORD variables are also set.
如果正在调用Shell函数,则还将设置COMP_WORDS和COMP_CWORD变量。 When the function or command is invoked, the first argument is the name of the command whose arguments are being completed , the second argument is the word being completed, and the third argument is the word preceding the word being completed on the current command line.
调用该函数或命令时, 第一个参数是其参数正在完成的命令的名称 ,第二个参数是在当前命令行上要完成的单词,第三个参数是在该命令行上要完成的单词之前的单词。 No filtering of the generated completions against the word being completed is performed;
不针对完成的单词对生成的完成进行过滤; the function or command has complete freedom in generating the matches.
函数或命令在生成匹配项方面具有完全的自由度。
# bash completion for figlet/toilet
{
have figlet || have toilet
} &&
_figlet_toilet()
{
local prev cur opts pat
_get_comp_words_by_ref cur prev
opts="-f"
COMPREPLY=()
case $prev in
-f)
case "${1}" in
figlet)
pat='flf'
;;
toilet)
pat='[tf]lf'
;;
esac
local running=$(find /usr/share/figlet -name "*.${pat}" -printf '%P\n' | sed "s/\.${pat}\$//")
COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
return 0
;;
*)
;;
esac
# if '-f' is already given, then generate random string
for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
if [[ ${COMP_WORDS[i]} == -f ]]; then
# COMPREPLY=("'$(fortune -sn42)'")
return 0
fi
done
COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) )
return 0
} &&
complete -F _figlet_toilet figlet toilet
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.