Repository: rsteube/carapace
Pull request #494 (2022-04-13 10:22:30)

nushell: nu-complete


Patch:
diff --git a/internal/shell/nushell/action.go b/internal/shell/nushell/action.go
index beb3f8f..231bca4 100644
--- a/internal/shell/nushell/action.go
+++ b/internal/shell/nushell/action.go
@@ -23,17 +23,12 @@ func sanitize(values []common.RawValue) []common.RawValue {
 	return values
 }
 
-type suggestion struct {
-	Display     string `json:"display"`
-	Replacement string `json:"replacement"`
-}
-
 // ActionRawValues formats values for nushell
 func ActionRawValues(currentWord string, nospace bool, values common.RawValues) string {
 	filtered := values.FilterPrefix(currentWord)
 	sort.Sort(common.ByDisplay(filtered))
 
-	vals := make([]suggestion, len(filtered))
+	vals := make([]string, len(filtered))
 	for index, val := range sanitize(filtered) {
 		if strings.ContainsAny(val.Value, ` {}()[]<>$&"|;#\`+"`") {
 			val.Value = fmt.Sprintf("'%v'", val.Value)
@@ -43,11 +38,7 @@ func ActionRawValues(currentWord string, nospace bool, values common.RawValues)
 			val.Value = val.Value + " "
 		}
 
-		if val.Description == "" {
-			vals[index] = suggestion{Display: val.Display, Replacement: val.Value}
-		} else {
-			vals[index] = suggestion{Display: fmt.Sprintf(`%v (%v)`, val.Display, val.TrimmedDescription()), Replacement: val.Value}
-		}
+		vals[index] = val.Value
 	}
 	m, _ := json.Marshal(vals)
 	return string(m)
diff --git a/internal/shell/nushell/snippet.go b/internal/shell/nushell/snippet.go
index 37daeec..b46457b 100644
--- a/internal/shell/nushell/snippet.go
+++ b/internal/shell/nushell/snippet.go
@@ -10,5 +10,20 @@ import (
 
 // Snippet creates the nushell completion script
 func Snippet(cmd *cobra.Command) string {
-	return fmt.Sprintf("config set completion.%v [%v _carapace nushell]", cmd.Name(), uid.Executable())
+	return fmt.Sprintf(`module carapace_%v {
+  def "nu-complete %v" [line: string, pos: int] {
+    let words = ($line | str substring [0 $pos] | split row " ")
+    if ($line | str substring [0 $pos] | str ends-with " ") {
+      %v _carapace nushell ($words | append "") | from json
+    } else {
+      %v _carapace nushell $words | from json
+    }
+  }
+  
+  export extern "%v" [
+    ...args: string@"nu-complete %v"
+  ]
+}
+use carapace_%v *
+`, cmd.Name(), cmd.Name(), uid.Executable(), uid.Executable(), cmd.Name(), cmd.Name(), cmd.Name())
 }


Test patch:
diff --git a/example/cmd/_test/nushell.nu b/example/cmd/_test/nushell.nu
new file mode 100644
index 0000000..5bf48b5
--- /dev/null
+++ b/example/cmd/_test/nushell.nu
@@ -0,0 +1,16 @@
+module carapace_example {
+  def "nu-complete example" [line: string, pos: int] {
+    let words = ($line | str substring [0 $pos] | split row " ")
+    if ($line | str substring [0 $pos] | str ends-with " ") {
+      example _carapace nushell ($words | append "") | from json
+    } else {
+      example _carapace nushell $words | from json
+    }
+  }
+  
+  export extern "example" [
+    ...args: string@"nu-complete example"
+  ]
+}
+use carapace_example *
+
diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go
index b5c17bf..6318ab6 100644
--- a/example/cmd/root_test.go
+++ b/example/cmd/root_test.go
@@ -34,6 +34,10 @@ func TestFish(t *testing.T) {
 	testScript(t, "fish", "./_test/fish.fish")
 }
 
+func TestNushell(t *testing.T) {
+	testScript(t, "nushell", "./_test/nushell.nu")
+}
+
 func TestOil(t *testing.T) {
 	testScript(t, "oil", "./_test/oil.sh")
 }