Skip to content

Commit 2962c44

Browse files
committed
zsh: compdef - support multiple registered commands
1 parent 80246c2 commit 2962c44

2 files changed

Lines changed: 29 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 // TODO map still necessary?
5552
}
5653
}
5754

pkg/bridges/zsh.sh

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

0 commit comments

Comments
 (0)