Skip to content

Commit

Permalink
Put activeHelp logic in its own file
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
  • Loading branch information
marckhouzam committed Oct 12, 2021
1 parent 491bdad commit 9da4d5b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
52 changes: 52 additions & 0 deletions active_help.go
@@ -0,0 +1,52 @@
package cobra

import (
"fmt"
"os"
"strings"
)

const (
activeHelpMarker = "_activeHelp_ "
// The below values should not be changed: programs will be using them explicitly
// in their user documentation, and users will be using them explicitly.
activeHelpEnvVarSuffix = "_ACTIVE_HELP"
activeHelpGlobalEnvVar = "COBRA_ACTIVE_HELP"
activeHelpGlobalDisable = "0"
)

// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.
// Such strings will be processed by the completion script and will be shown as ActiveHelp
// to the user.
// The array parameter should be the array that will contain the completions.
// This function can be called multiple times before and/or after completions are added to
// the array. Each time this function is called with the same array, the new
// ActiveHelp line will be shown below the previous ones when completion is triggered.
func AppendActiveHelp(compArray []string, activeHelpStr string) []string {
return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr))
}

// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment
// variable. It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the
// root command in upper case, with all - replaced by _.
// This format should not be changed: users will be using it explicitly.
func activeHelpEnvVar(name string) string {
activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
return strings.ReplaceAll(activeHelpEnvVar, "-", "_")
}

// setActiveHelpConfig first checks the global environment variable
// of ActiveHelp to see if it is disabling active help, and if it is not,
// it then looks to the program-specific variable.
// It then sets the ActiveHelpConfig value to make it available when
// calling the completion function. We also set it on the root,
// just in case users try to access it from there.
func setActiveHelpConfig(cmd *Command) {
activeHelpCfg := os.Getenv(activeHelpGlobalEnvVar)
if activeHelpCfg != activeHelpGlobalDisable {
activeHelpCfg = os.Getenv(activeHelpEnvVar(cmd.Root().Name()))
}

cmd.ActiveHelpConfig = activeHelpCfg
cmd.Root().ActiveHelpConfig = cmd.ActiveHelpConfig
}
2 changes: 1 addition & 1 deletion active_help.md
Expand Up @@ -173,4 +173,4 @@ Completion ended with directive: ShellCompDirectiveDefault
If a user wants to disable Active Help for every single program based on Cobra, the global environment variable `COBRA_ACTIVE_HELP` can be used as follows:
```
export COBRA_ACTIVE_HELP=0
```
```
34 changes: 1 addition & 33 deletions completions.go
Expand Up @@ -81,11 +81,6 @@ const (
compCmdNoDescFlagName = "no-descriptions"
compCmdNoDescFlagDesc = "disable completion descriptions"
compCmdNoDescFlagDefault = false

activeHelpMarker = "_activeHelp_ "
activeHelpEnvVarSuffix = "_ACTIVE_HELP"
activeHelpGlobalEnvVar = "COBRA_ACTIVE_HELP"
activeHelpGlobalDisable = "0"
)

// CompletionOptions are the options to control shell completion
Expand All @@ -100,11 +95,6 @@ type CompletionOptions struct {
DisableDescriptions bool
}

func activeHelpEnvVar(name string) string {
activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
return strings.ReplaceAll(activeHelpEnvVar, "-", "_")
}

// NoFileCompletions can be used to disable file completion for commands that should
// not trigger file completions.
func NoFileCompletions(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
Expand All @@ -127,17 +117,6 @@ func (c *Command) RegisterFlagCompletionFunc(flagName string, f func(cmd *Comman
return nil
}

// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.
// Such strings will be processed by the completion script and will be shown as ActiveHelp
// to the user.
// The array parameter should be the array that will contain the completions.
// This function can be called multiple times before and/or after completions are added to
// the array. Each time this function is called with the same array, the new
// ActiveHelp line will be shown below the previous ones when completion is triggered.
func AppendActiveHelp(compArray []string, activeHelpStr string) []string {
return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr))
}

// Returns a string listing the different directive enabled in the specified parameter
func (d ShellCompDirective) string() string {
var directives []string
Expand Down Expand Up @@ -445,18 +424,7 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi
// Call the registered completion function to get the completions.
var comps []string

// First set the ActiveHelpConfig value to make it available when
// calling the completion function. We also set it on the root,
// just in case users try to access it from there.
// First check the global environment variable to see if it is
// disabling active help, and if it is not, use the program-specific var.
activeHelpVar := os.Getenv(activeHelpGlobalEnvVar)
if activeHelpVar != activeHelpGlobalDisable {
activeHelpVar = os.Getenv(activeHelpEnvVar(c.Root().Name()))
}

finalCmd.ActiveHelpConfig = activeHelpVar
finalCmd.Root().ActiveHelpConfig = finalCmd.ActiveHelpConfig
setActiveHelpConfig(finalCmd)

comps, directive = completionFn(finalCmd, finalArgs, toComplete)
completions = append(completions, comps...)
Expand Down

0 comments on commit 9da4d5b

Please sign in to comment.