我想为具有虚拟文件树的工具创建zsh完成.
例如我的文件树如下所示:
/
|- foo/
| |- bar
| |- baz/
| |- qux
|- foobar
我的工具mycmd有一个子命令,用于列出当前目录:
$mycmd ls
foo/
foobar
$mycmd ls foo/
bar
baz/
我的实际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
恕我直言_values错误的效用函数.实际行为是:
$mycmd ls<TAB>
foo/ foobar
$mycmd ls foo/<TAB> ## <- it inserts automatically a space before <TAB> and so $words[-1] = ""
foo/ foobar
我无法使用实用程序函数_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
}