Skip to content

Commit

Permalink
Merge pull request #1269 from rsteube/nushell-update-snippet
Browse files Browse the repository at this point in the history
nushell: update snippet
  • Loading branch information
rsteube authored Aug 22, 2022
2 parents c2a2a03 + 69db182 commit 86b0af4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 71 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ carapace --list | awk '{print $1}' | xargs -I{} touch ~/.config/fish/completions
carapace _carapace | source

# nushell
carapace _carapace nushell | save carapace.nu
source carapace.nu
carapace _carapace nushell

# oil (~/.config/oil/oshrc)
source <(carapace _carapace)
Expand Down
76 changes: 13 additions & 63 deletions cmd/carapace/cmd/lazyInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"os"
"regexp"
"strings"

"github.com/rsteube/carapace/third_party/golang.org/x/sys/execabs"
)

func getSpecs() (specs []string, dir string) {
Expand Down Expand Up @@ -128,71 +126,23 @@ end
return fmt.Sprintf(snippet, strings.Join(complete, "\n"))
}

func filterNushellBuiltins(completers []string) []string {
builtins := make(map[string]bool)
if output, err := execabs.Command("nu", "-c", "help commands | get name | str collect '\n'").Output(); err == nil {
lines := strings.Split(string(output), "\n")
for _, line := range lines {
builtins[strings.Split(line, " ")[0]] = true
}
func nushell_lazy(completers []string) string {
specs, dir := getSpecs()

filtered := make([]string, 0)
for _, c := range completers {
if !builtins[c] {
filtered = append(filtered, c)
}
}
return filtered
specSnippets := make([]string, 0)
for _, spec := range specs {
specSnippets = append(specSnippets, fmt.Sprintf(` %v: { carapace --spec '%v/%v.yaml' nushell $spans | from json }
`, spec, dir, spec))
}
return completers // skip filtering

return fmt.Sprintf(`let external_completer = {|spans|
{
$spans.0: {carapace $spans.0 nushell $spans | from json } # default
%v } | get $spans.0 | each {|it| do $it}
}
func nushell_lazy(completers []string) string {
completers = filterNushellBuiltins(completers) // skip nushell builtins by default
exports := make([]string, len(completers))
for index, completer := range completers {
exports[index] = fmt.Sprintf(` export extern "%v" [
...args: string@"nu-complete carapace"
]`, completer)
}
snippet := fmt.Sprintf(`module carapace {
def "nu-complete carapace" [line: string, pos: int] {
let words = ($line | str substring [0 $pos] | split row " ")
if ($line | str substring [0 $pos] | str ends-with " ") {
carapace $words.0 nushell ($words | append "") | from json
} else {
carapace $words.0 nushell $words | from json
}
}
%v
`, strings.Join(exports, "\n"))

// if specs, dir := getSpecs(); len(specs) > 0 {
// snippet += fmt.Sprintf(`
// def "nu-complete carapace-spec" [line: string, pos: int] {
// let words = ($line | str substring [0 $pos] | split row " ")
// if ($line | str substring [0 $pos] | str ends-with " ") {
// carapace --spec '%v'/$words.0'.yaml' nushell ($words | append "") | from json
// } else {
// carapace --spec '%v'/$words.0'.yaml' nushell $words | from json
// }
// }
//`, dir, dir)
//
// for _, spec := range specs {
// snippet += fmt.Sprintf(` export extern "%v" [
// ...args: string@"nu-complete carapace-spec"
// ]`, spec)
// }
// }

snippet += `
}
use carapace *
`
return snippet
let-env config = {
external_completer: $external_completer
}`, strings.Join(specSnippets, "\n"))
}

func oil_lazy(completers []string) string {
Expand Down
2 changes: 1 addition & 1 deletion cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var rootCmd = &cobra.Command{
bash: source <(carapace _carapace bash)
elvish: eval (carapace _carapace elvish | slurp)
fish: carapace _carapace fish | source
nushell: carapace _carapace nushell | save carapace.nu ; nu -c 'source carapace.nu'
nushell: carapace _carapace nushell
oil: source <(carapace _carapace oil)
powershell: carapace _carapace powershell | Out-String | Invoke-Expression
tcsh: eval `+"`"+`carapace _carapace tcsh`+"`"+`
Expand Down
3 changes: 1 addition & 2 deletions docs/src/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ carapace --list | awk '{print $1}' | xargs -I{} touch ~/.config/fish/completions
carapace _carapace | source

# nushell
carapace _carapace | save carapace.nu
source carapace.nu
carapace _carapace

# oil (~/.config/oil/oshrc)
source <(carapace _carapace)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/pelletier/go-toml v1.9.5
github.com/rsteube/carapace v0.21.1
github.com/rsteube/carapace v0.22.0
github.com/rsteube/carapace-spec v0.0.22
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rsteube/carapace v0.21.1 h1:ulbhwIrZOr4g6LziYtjO2S10LZOO2dbFLkUumtF3AAg=
github.com/rsteube/carapace v0.21.1/go.mod h1:JjsAq3W3rvWEIg0FkOxT0HUNQjnboP/T0S8Khof9ZWU=
github.com/rsteube/carapace v0.22.0 h1:lXYBadIedOIoy/lWfKl9TCxZBVKQ/ClTOKUD6DC2DuY=
github.com/rsteube/carapace v0.22.0/go.mod h1:JjsAq3W3rvWEIg0FkOxT0HUNQjnboP/T0S8Khof9ZWU=
github.com/rsteube/carapace-spec v0.0.22 h1:uslsWc2jWXqg1ihnc02ztkyS80XiTiLJI8wvDo7iiOc=
github.com/rsteube/carapace-spec v0.0.22/go.mod h1:vyKkMfbIYHhPiUeyCemLWh96XNPwWI9B6Go0+W5nDUw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down

0 comments on commit 86b0af4

Please sign in to comment.