From 70067196037b693592732beaebbf1ae25f60bb22 Mon Sep 17 00:00:00 2001 From: Jack Wright Date: Sat, 30 Nov 2024 13:57:31 -0800 Subject: [PATCH] fixing formatting --- nushell_completions.go | 32 +++++++++++++++--------------- nushell_completions_test.go | 12 +++++++++++ site/content/completions/_index.md | 22 +++++++++----------- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/nushell_completions.go b/nushell_completions.go index e908fc6..d6cc274 100644 --- a/nushell_completions.go +++ b/nushell_completions.go @@ -15,15 +15,15 @@ package cobra import ( - "bytes" - "fmt" - "io" - "os" + "bytes" + "fmt" + "io" + "os" ) func (c *Command) GenNushellCompletion(w io.Writer, includeDesc bool) error { - buf := new(bytes.Buffer) - WriteStringAndCheck(buf, fmt.Sprintf(` + buf := new(bytes.Buffer) + WriteStringAndCheck(buf, fmt.Sprintf(` let cobra_completer = {|spans| let ShellCompDirectiveError = %[1]d let ShellCompDirectiveNoSpace = %[2]d @@ -40,7 +40,7 @@ let cobra_completer = {|spans| ] { # This will catch the stderr message related to the directive and any other errors, # such as the command not being a cobra based command - let result = do --ignore-errors { cobra_active_help=0 run-external $cmd "__complete" ...$spans | complete } + let result = do --ignore-errors { COBRA_ACTIVE_HELP=0 run-external $cmd "__complete" ...$spans | complete } if $result != null and $result.exit_code == 0 { let completions = $result.stdout | lines @@ -110,18 +110,18 @@ let cobra_completer = {|spans| } } `, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, - ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder)) + ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder)) - _, err := buf.WriteTo(w) - return err + _, err := buf.WriteTo(w) + return err } func (c *Command) GenNushellCompletionFile(filename string, includeDesc bool) error { - outFile, err := os.Create(filename) - if err != nil { - return err - } - defer outFile.Close() + outFile, err := os.Create(filename) + if err != nil { + return err + } + defer outFile.Close() - return c.GenNushellCompletion(outFile, includeDesc) + return c.GenNushellCompletion(outFile, includeDesc) } diff --git a/nushell_completions_test.go b/nushell_completions_test.go index 73fedd0..1901540 100644 --- a/nushell_completions_test.go +++ b/nushell_completions_test.go @@ -95,3 +95,15 @@ func TestFailGenNushellCompletionFile(t *testing.T) { } } } + +func TestNushellCompletionNoActiveHelp(t *testing.T) { + c := &Command{Use: "c", Run: emptyRun} + + buf := new(bytes.Buffer) + assertNoErr(t, c.GenNushellCompletion(buf, true)) + output := buf.String() + + // check that active help is being disabled + activeHelpVar := activeHelpGlobalEnvVar + check(t, output, fmt.Sprintf("%s=0", activeHelpVar)) +} diff --git a/site/content/completions/_index.md b/site/content/completions/_index.md index 2f598c7..f64d6e4 100644 --- a/site/content/completions/_index.md +++ b/site/content/completions/_index.md @@ -73,21 +73,17 @@ PowerShell: Nushell: # To configure the Nushell cobra external completer for the first time: - # 1. Copy the output of the command below: - > %[1]s completion nushell - # 2. Edit the nushell config file: - > config nu - # 3. Paste above the "let-env config" line. - # 4. Change the config block's external_completer line to be - external_completer: $cobra_completer - # 5. You will need to start a new shell or for this setup to take effect. - - # If you have already setup the cobra external completer: # 1. Edit the nushell config file: > config nu - # 2. Modify the cobra_apps varible to contain this application: - > let cobra_apps = [ "othercobraapp", "%[1]s" ] - # 3. You will need to start a new shell for this setup to take effect. + # 2. Copy the completer to at the end of the file. + # 3. Add a section like the following below the cobra_completer: + $env.config.completions.external = { + enable: true + max_results: 100 + completer: $cobra_completer + } + +NOTE: This completer will work for all cobra based commands. More information can be found in the [External Completions](https://www.nushell.sh/book/custom_completions.html#custom-descriptions) section of the Nushell book. Information on setting up more than external completer can be found [Multiple completer](https://www.nushell.sh/cookbook/external_completers.html#multiple-completer) of the Nushell cookbook. `,cmd.Root().Name()), DisableFlagsInUseLine: true,