fixing formatting

This commit is contained in:
Jack Wright 2024-11-30 13:57:31 -08:00
parent 8b4aa590bb
commit 7006719603
3 changed files with 37 additions and 29 deletions

View file

@ -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)
}

View file

@ -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))
}

View file

@ -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,