mirror of https://github.com/spf13/cobra.git
Format with spaces instead of tabs
This commit is contained in:
parent
e7abbf39a3
commit
7eac0e11ae
|
@ -15,114 +15,112 @@
|
|||
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(`
|
||||
# An external completer that works with any cobra based
|
||||
# command line application (e.g. kubectl, minikube)
|
||||
buf := new(bytes.Buffer)
|
||||
WriteStringAndCheck(buf, fmt.Sprintf(`
|
||||
let cobra_completer = {|spans|
|
||||
let ShellCompDirectiveError = %[1]d
|
||||
let ShellCompDirectiveNoSpace = %[2]d
|
||||
let ShellCompDirectiveNoFileComp = %[3]d
|
||||
let ShellCompDirectiveFilterFileExt = %[4]d
|
||||
let ShellCompDirectiveFilterDirs = %[5]d
|
||||
let ShellCompDirectiveError = %[1]d
|
||||
let ShellCompDirectiveNoSpace = %[2]d
|
||||
let ShellCompDirectiveNoFileComp = %[3]d
|
||||
let ShellCompDirectiveFilterFileExt = %[4]d
|
||||
let ShellCompDirectiveFilterDirs = %[5]d
|
||||
|
||||
let cmd = $spans | first
|
||||
let rest = $spans | skip
|
||||
let cmd = $spans | first
|
||||
let rest = $spans | skip
|
||||
|
||||
def exec_complete [
|
||||
spans: list<string>
|
||||
] {
|
||||
# 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 }
|
||||
def exec_complete [
|
||||
spans: list<string>
|
||||
] {
|
||||
# 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 }
|
||||
|
||||
if $result != null and $result.exit_code == 0 {
|
||||
let completions = $result.stdout | lines
|
||||
if $result != null and $result.exit_code == 0 {
|
||||
let completions = $result.stdout | lines
|
||||
|
||||
# the directive is the last line
|
||||
let directive = do -i { $completions | last | str replace ':' '' | into int }
|
||||
# the directive is the last line
|
||||
let directive = do -i { $completions | last | str replace ':' '' | into int }
|
||||
|
||||
let completions = $completions | drop | each { |it|
|
||||
# the first word is the command, the rest is the description
|
||||
let words = $it | split row -r '\s{1}'
|
||||
let completions = $completions | drop | each { |it|
|
||||
# the first word is the command, the rest is the description
|
||||
let words = $it | split row -r '\s{1}'
|
||||
|
||||
# If the last span contains a hypen and equals, attach it to the name
|
||||
let last_span = $spans | last
|
||||
let words = if ($last_span =~ '^-') and ($last_span =~ '=$') {
|
||||
$words | each {|it| $"($last_span)($it)" }
|
||||
} else {
|
||||
$words
|
||||
}
|
||||
# If the last span contains a hypen and equals, attach it to the name
|
||||
let last_span = $spans | last
|
||||
let words = if ($last_span =~ '^-') and ($last_span =~ '=$') {
|
||||
$words | each {|it| $"($last_span)($it)" }
|
||||
} else {
|
||||
$words
|
||||
}
|
||||
|
||||
{value: ($words | first | str trim), description: ($words | skip | str join ' ')}
|
||||
}
|
||||
{value: ($words | first | str trim), description: ($words | skip | str join ' ')}
|
||||
}
|
||||
|
||||
{completions: $completions, directive: $directive}
|
||||
} else {
|
||||
{completions: [], directive: -1}
|
||||
}
|
||||
}
|
||||
{completions: $completions, directive: $directive}
|
||||
} else {
|
||||
{completions: [], directive: -1}
|
||||
}
|
||||
}
|
||||
|
||||
if (not ($rest | is-empty)) {
|
||||
let result = exec_complete $rest
|
||||
let completions = $result.completions
|
||||
let directive = $result.directive
|
||||
if (not ($rest | is-empty)) {
|
||||
let result = exec_complete $rest
|
||||
let completions = $result.completions
|
||||
let directive = $result.directive
|
||||
|
||||
# Add space at the end of each completion
|
||||
let completions = if $directive != $ShellCompDirectiveNoSpace {
|
||||
$completions | each {|it| {value: $"($it.value) ", description: $it.description}}
|
||||
} else {
|
||||
$completions
|
||||
}
|
||||
# Add space at the end of each completion
|
||||
let completions = if $directive != $ShellCompDirectiveNoSpace {
|
||||
$completions | each {|it| {value: $"($it.value) ", description: $it.description}}
|
||||
} else {
|
||||
$completions
|
||||
}
|
||||
|
||||
# Cobra returns a list of completions that are supported with this directive
|
||||
# There is no way to currently support this in a nushell external completer
|
||||
let completions = if $directive == $ShellCompDirectiveFilterFileExt {
|
||||
[]
|
||||
} else {
|
||||
$completions
|
||||
}
|
||||
# Cobra returns a list of completions that are supported with this directive
|
||||
# There is no way to currently support this in a nushell external completer
|
||||
let completions = if $directive == $ShellCompDirectiveFilterFileExt {
|
||||
[]
|
||||
} else {
|
||||
$completions
|
||||
}
|
||||
|
||||
if $directive == $ShellCompDirectiveNoFileComp {
|
||||
# Allow empty results as this will stop file completion
|
||||
$completions
|
||||
} else if ($completions | is-empty) or $directive == $ShellCompDirectiveError {
|
||||
# Not returning null causes file completions to break
|
||||
# Return null if there are no completions or ShellCompDirectiveError
|
||||
null
|
||||
} else {
|
||||
$completions
|
||||
}
|
||||
|
||||
if ($completions | is-empty) {
|
||||
null
|
||||
} else {
|
||||
$completions
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
if $directive == $ShellCompDirectiveNoFileComp {
|
||||
# Allow empty results as this will stop file completion
|
||||
$completions
|
||||
} else if ($completions | is-empty) or $directive == $ShellCompDirectiveError {
|
||||
# Not returning null causes file completions to break
|
||||
# Return null if there are no completions or ShellCompDirectiveError
|
||||
null
|
||||
} else {
|
||||
$completions
|
||||
}
|
||||
|
||||
if ($completions | is-empty) {
|
||||
null
|
||||
} else {
|
||||
$completions
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
`, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
|
||||
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs))
|
||||
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs))
|
||||
|
||||
_, 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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue