From 79fb3dd0cf9abf1c62d1bf48e7df18421e6ab261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 10 May 2023 00:00:43 +0300 Subject: [PATCH] Avoid redundant completion output target evaluations The target is not to be changed while outputting completions, so resolve it only once. --- completions.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/completions.go b/completions.go index b76e54ad..f9401888 100644 --- a/completions.go +++ b/completions.go @@ -217,6 +217,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 @@ -242,14 +243,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 : - 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.