tru*_*tru 5 zsh zsh-completion
我想为具有虚拟文件树的工具创建 zsh 完成。例如,我的文件树如下所示:
/
|- foo/
| |- bar
| |- baz/
| |- qux
|- foobar
Run Code Online (Sandbox Code Playgroud)
我的工具mycmd有一个用于列出当前目录的子命令:
$ mycmd ls
foo/
foobar
$ mycmd ls foo/
bar
baz/
Run Code Online (Sandbox Code Playgroud)
我实际的 zsh 完成看起来是这样的:
_mycmd_ls() {
if [ ! -z "$words[-1]" ]; then
dir=$(dirname /$words[-1])
lastpart=$(basename $words[-1])
items=$(mycmd ls $dir | grep "^$lastpart")
else
items=$(mycmd ls)
fi
_values -s ' ' 'items' ${(uozf)items}
}
_mycmd() {
local -a commands
commands=(
'ls:list items in directory'
)
_arguments -C -s -S -n \
'(- 1 *)'{-v,--version}"[Show program\'s version number and exit]: :->full" \
'(- 1 *)'{-h,--help}'[Show help message and exit]: :->full' \
'1:cmd:->cmds' \
'*:: :->args' \
case "$state" in
(cmds)
_describe -t commands 'commands' commands
;;
(args)
_mycmd_ls
;;
(*)
;;
esac
}
_mycmd
Run Code Online (Sandbox Code Playgroud)
恕我直言,这是_values错误的效用函数。实际行为是:
$ mycmd ls<TAB>
foo/ foobar
$ mycmd ls foo/<TAB> ## <- it inserts automatically a space before <TAB> and so $words[-1] = ""
foo/ foobar
Run Code Online (Sandbox Code Playgroud)
我无法使用实用程序功能_files或_path_files因为文件树只是虚拟的。
我建议使用而compadd不是_values来控制附加的后缀字符。然后查看可用的选项,设置一个空后缀字符,以防虚拟目录是结果的一部分:
_mycmd_ls() {
if [ ! -z "$words[-1]" ]; then
dir=$(dirname /$words[-1])
lastpart=$(basename $words[-1])
items=$(mycmd ls $dir | grep "^$lastpart")
else
items=$(mycmd ls)
fi
local suffix=' ';
# do not append space to word completed if it is a directory (ends with /)
for val in $items; do
if [ "${val: -1:1}" = '/' ]; then
suffix=''
break
fi
done
compadd -S "$suffix" -a items
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
139 次 |
| 最近记录: |