使用虚拟路径完成zsh

时间:2017-03-01 08:14:09

标签: zsh zsh-completion

我想为具有虚拟文件树的工具创建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,因为文件树只是虚拟的。

1 个答案:

答案 0 :(得分:1)

我建议使用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
}
相关问题