Skip to content

Commit

Permalink
Avoid redundant completion output target evaluations
Browse files Browse the repository at this point in the history
The target is not to be changed while outputting completions, so resolve
it only once.
  • Loading branch information
scop committed May 9, 2023
1 parent 35430e6 commit 2f706a2
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func (c *Command) initCompleteCmd(args []string) {
}

noDescriptions := (cmd.CalledAs() == ShellCompNoDescRequestCmd)
out := finalCmd.OutOrStdout()
for _, comp := range completions {
if GetActiveHelpConfig(finalCmd) == activeHelpGlobalDisable {
// Remove all activeHelp entries in this case
Expand All @@ -223,14 +224,14 @@ func (c *Command) initCompleteCmd(args []string) {
// although there is no description).
comp = strings.TrimSpace(comp)

// Print each possible completion to stdout for the completion script to consume.
fmt.Fprintln(finalCmd.OutOrStdout(), comp)
// Print each possible completion to the output for the completion script to consume.
fmt.Fprintln(out, comp)
}

// As the last printout, print the completion directive for the completion script to parse.
// The directive integer must be that last character following a single colon (:).
// The completion script expects :<directive>
fmt.Fprintf(finalCmd.OutOrStdout(), ":%d\n", directive)
fmt.Fprintf(out, ":%d\n", directive)

// Print some helpful info to stderr for the user to understand.
// Output from stderr must be ignored by the completion script.
Expand Down

0 comments on commit 2f706a2

Please sign in to comment.