Skip to content

Commit 5817857

Browse files
authored
Merge pull request #260 from carapace-sh/zsh-compdef-multiple
zsh: compdef - support multiple registered commands
2 parents 80246c2 + f17ba04 commit 5817857

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

pkg/bridges/zsh.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bridges
22

33
import (
44
"bytes"
5+
_ "embed"
56
"fmt"
67
"os"
78
"runtime"
@@ -13,6 +14,9 @@ import (
1314
"github.com/carapace-sh/carapace/pkg/xdg"
1415
)
1516

17+
//go:embed zsh.sh
18+
var zshScript string
19+
1620
func Zsh() []string {
1721
if runtime.GOOS == "windows" {
1822
return []string{}
@@ -23,7 +27,7 @@ func Zsh() []string {
2327
}
2428

2529
return cache("zsh", func() ([]string, error) {
26-
script := "printf '%s\n' $fpath"
30+
script := zshScript
2731
if path, err := zshrc(); err == nil {
2832
script = fmt.Sprintf("autoload -U compinit && compinit;source %#v;%v;compinit", path, script)
2933
}
@@ -39,19 +43,12 @@ func Zsh() []string {
3943
}
4044
return nil, err
4145
}
42-
lines := strings.Split(stdout.String(), "\n")
4346

47+
lines := strings.Split(stdout.String(), "\n")
4448
unique := make(map[string]bool)
4549
for _, line := range lines {
46-
entries, err := os.ReadDir(line)
47-
if err != nil {
48-
carapace.LOG.Println(err.Error())
49-
continue
50-
}
51-
for _, entry := range entries {
52-
if !entry.IsDir() && strings.HasPrefix(entry.Name(), "_") {
53-
unique[strings.TrimPrefix(entry.Name(), "_")] = true
54-
}
50+
if line != "" {
51+
unique[line] = true
5552
}
5653
}
5754

pkg/bridges/zsh.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
printf '%s\n' $fpath \
2+
| xargs -I{} find {} -name '_*' \
3+
| xargs head -q -n1 \
4+
| grep -v -F \
5+
-e ' -k ' \
6+
-e ' -K ' \
7+
-e '#autoload' \
8+
| sed -e 's/ -[^ ]\+//g' \
9+
-e 's/^#compdef //' \
10+
| tr " " "\n" \
11+
| grep -v \
12+
-e '=' \
13+
-e '\[' \
14+
-e '\*' \
15+
-e '#' \
16+
-e '^_' \
17+
-e '^-$' \
18+
-e '^.$' \
19+
| sort \
20+
| uniq

0 commit comments

Comments
 (0)