diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go index d7382eaa3..a951ac73d 100644 --- a/example/cmd/root_test.go +++ b/example/cmd/root_test.go @@ -1356,7 +1356,8 @@ function _example__multiparts { "1: :{_example_callback '_example__multiparts#1'}" \ "*: :{_example_callback '_example__multiparts#0'}" } -if compquote '' 2>/dev/null; then _example; else compdef _example example; fi +compquote '' 2>/dev/null && _example +compdef _example example ` rootCmd.InitDefaultHelpCmd() assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("zsh", false)) diff --git a/zsh/snippet.go b/zsh/snippet.go index 4d66789f7..10cb64867 100644 --- a/zsh/snippet.go +++ b/zsh/snippet.go @@ -22,11 +22,14 @@ var replacer = strings.NewReplacer( func snippetLazy(cmd *cobra.Command) string { return fmt.Sprintf(`#compdef %v function _%v { + compdef -d %v + unfunction _%v source <(%v _carapace zsh) } -if compquote '' 2>/dev/null; then _%v; else compdef _%v %v; fi -`, cmd.Name(), cmd.Name(), uid.Executable(), cmd.Name(), cmd.Name(), cmd.Name()) +compquote '' 2>/dev/null && _%v +compdef _%v %v +`, cmd.Name(), cmd.Name(), cmd.Name(), cmd.Name(), uid.Executable(), cmd.Name(), cmd.Name(), cmd.Name()) } func Snippet(cmd *cobra.Command, actions map[string]string, lazy bool) string { @@ -42,7 +45,7 @@ func Snippet(cmd *cobra.Command, actions map[string]string, lazy bool) string { `, cmd.Name(), uid.Executable()) result += snippetFunctions(cmd, actions) - result += fmt.Sprintf("if compquote '' 2>/dev/null; then _%v; else compdef _%v %v; fi\n", cmd.Name(), cmd.Name(), cmd.Name()) // check if withing completion function and enable direct sourcing + result += fmt.Sprintf("compquote '' 2>/dev/null && _%v\ncompdef _%v %v\n", cmd.Name(), cmd.Name(), cmd.Name()) return result }