Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using output of command to generate autocompletion commands for zsh

Hey, I'm trying to get zsh to run a git command, and use the output to generate autocomplete possibilities.

The command I'm trying to run is

git log -n 2 --pretty=format:"'%h %an'"

And here's the code I'm using:

local lines words

lines=(${(f)$(git log -n 2 --pretty=format:"'%h %an'")})
words=${(f)$(_call_program foobar git log -n 2 --pretty=format:"%h")}

echo "Length of lines is " ${#lines[@]} " value is " ${lines}
echo "Length of words is " ${#words[@]} " value is " ${words}

compadd -d lines -a -- words

This doesn't work at all...it thinks that words is a single element and lines aren't getting printed properly at all.

However, when I try to setup an array of strings by hand, it all works.

local lines words

lines=('one two' 'three')
words=('one two' 'three')

echo "Length of lines is " ${#lines[@]} " value is " ${lines}
echo "Length of words is " ${#words[@]} " value is " ${words}

compadd -d lines -a -- words
like image 768
Majd Taby Avatar asked Mar 23 '11 07:03

Majd Taby


1 Answers

To force words being an array, you should use either

words=( ${(f)...} )

or

set -A words ${(f)...}

. If you use just words=${(f)...}, you will always get one value. By the way, why have you added parenthesis around ${(f)...} when you were writing lines definition, but have not done it for words?

Also, there is another thing to concern: ${(f)$(...)} should be replaced with ${(f)"$(...)"}. It is some black magic here: I don't know why first one does emit a single scalar value, while second one does emit an array of scalar values, just was pointed to this fact by someone here on stackoverflow.

like image 110
ZyX Avatar answered Sep 24 '22 15:09

ZyX