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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With