Skip to content

Commit

Permalink
Merge pull request #107 from rsteube/zsh-fix-lazy
Browse files Browse the repository at this point in the history
zsh: fix lazy completion script
  • Loading branch information
rsteube authored Nov 15, 2020
2 parents a2dd425 + 4d36ef0 commit 22a1521
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 6 additions & 3 deletions zsh/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 22a1521

Please sign in to comment.