Skip to content

Commit

Permalink
fixing formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ayax79 committed Nov 30, 2024
1 parent 8b4aa59 commit 7006719
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
32 changes: 16 additions & 16 deletions nushell_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
12 changes: 12 additions & 0 deletions nushell_completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
22 changes: 9 additions & 13 deletions site/content/completions/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7006719

Please sign in to comment.