Skip to content

Commit 02f6a2d

Browse files
committed
zsh: convert to non-lazy snippet
1 parent 7e13494 commit 02f6a2d

File tree

1 file changed

+37
-6
lines changed
  • cmd/carapace/cmd/lazyinit

1 file changed

+37
-6
lines changed

cmd/carapace/cmd/lazyinit/zsh.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,43 @@ import (
66
)
77

88
func Zsh(completers []string) string {
9-
snippet := `%v%v
9+
for i, completer := range completers {
10+
completers[i] = fmt.Sprintf("%q", completer)
11+
}
12+
return fmt.Sprintf(`%v%v
1013
11-
function _carapace_lazy {
12-
source <(carapace $words[1] zsh)
14+
function _carapace_completer {
15+
local command="$words[1]"
16+
local words=${words[@]:0:$CURRENT}
17+
local IFS=$'\n'
18+
19+
# shellcheck disable=SC2086,SC2154,SC2155
20+
if echo ${words}"''" | xargs echo 2>/dev/null > /dev/null; then
21+
local lines="$(echo ${words}"''" | CARAPACE_COMPLINE="${words}" CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs carapace "$command" zsh )"
22+
elif echo ${words} | sed "s/\$/'/" | xargs echo 2>/dev/null > /dev/null; then
23+
local lines="$(echo ${words} | sed "s/\$/'/" | CARAPACE_COMPLINE="${words}" CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs carapace "$command" zsh)"
24+
else
25+
local lines="$(echo ${words} | sed 's/$/"/' | CARAPACE_COMPLINE="${words}" CARAPACE_ZSH_HASH_DIRS="$(hash -d)" xargs carapace "$command" zsh)"
26+
fi
27+
28+
local zstyle message data
29+
IFS=$'\001' read -r -d '' zstyle message data <<<"${lines}"
30+
# shellcheck disable=SC2154
31+
zstyle ":completion:${curcontext}:*" list-colors "${zstyle}"
32+
zstyle ":completion:${curcontext}:*" group-name ''
33+
[ -z "$message" ] || _message -r "${message}"
34+
35+
local block tag displays values displaysArr valuesArr
36+
while IFS=$'\002' read -r -d $'\002' block; do
37+
IFS=$'\003' read -r -d '' tag displays values <<<"${block}"
38+
# shellcheck disable=SC2034
39+
IFS=$'\n' read -r -d $'\004' -A displaysArr <<<"${displays}"$'\004'
40+
IFS=$'\n' read -r -d $'\004' -A valuesArr <<<"${values}"$'\004'
41+
42+
[[ ${#valuesArr[@]} -gt 1 ]] && _describe -t "${tag}" "${tag}" displaysArr valuesArr -Q -S ''
43+
done <<<"${data}"
1344
}
14-
compdef _carapace_lazy %v
15-
`
16-
return fmt.Sprintf(snippet, pathSnippet("zsh"), envSnippet("zsh"), strings.Join(completers, " "))
45+
46+
compdef _carapace_completer %v
47+
`, pathSnippet("zsh"), envSnippet("zsh"), strings.Join(completers, " "))
1748
}

0 commit comments

Comments
 (0)