5

I want to create a zsh completion for a tool with a virtual file tree. e.g. my file tree looks like the following:

/ |- foo/ | |- bar | |- baz/ | |- qux |- foobar 

My tool mycmd has a subcommand for listing the current directory:

$ mycmd ls foo/ foobar $ mycmd ls foo/ bar baz/ 

My actual zsh completion looks like this:

_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 

IMHO is _values the wrong utility function. The actual behaviour is:

$ mycmd ls<TAB> foo/ foobar $ mycmd ls foo/<TAB> ## <- it inserts automatically a space before <TAB> and so $words[-1] = "" foo/ foobar 

I can't use the utility function _files or _path_files because the file tree is only virtual.

1
  • I'd be really curious to see if there is a way to do so. I'm facing the same kind of problems. Commented Dec 19, 2018 at 13:49

1 Answer 1

2

I would suggest to make use of compadd rather _values to get in control of the suffix character appended. Then looking at the available choices, set an empty suffix character in case a virtual directory is part of the result:

_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 } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was struggling to make sense of all the completion utility functions and styles, and this bit of code did just what I needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.