Repository: rsteube/carapace
Pull request #269 (2021-03-25 11:41:11)

zsh re-enabled special characters
#215
#29 


Patch:
diff --git a/internal/zsh/action.go b/internal/zsh/action.go
index a1d4723..cedb216 100644
--- a/internal/zsh/action.go
+++ b/internal/zsh/action.go
@@ -8,23 +8,10 @@ import (
 )
 
 var sanitizer = strings.NewReplacer(
-	`$`, ``,
-	"`", ``,
 	"\n", ``,
-	`\`, ``,
-	`"`, ``,
-	`'`, ``,
-	"`", ``,
-	`|`, ``,
-	`>`, ``,
-	`<`, ``,
-	`&`, ``,
-	`(`, ``,
-	`)`, ``,
-	`;`, ``,
-	`#`, ``,
-	`[`, `\[`,
-	`]`, `\]`,
+	"\r", ``,
+	"\t", ``,
+	`'`, `'\''`,
 )
 
 func Sanitize(values ...string) []string {
@@ -35,10 +22,6 @@ func Sanitize(values ...string) []string {
 	return sanitized
 }
 
-func EscapeSpace(s string) string {
-	return strings.Replace(s, " ", `\ `, -1)
-}
-
 func ActionRawValues(callbackValue string, values ...common.RawValue) string {
 	filtered := make([]common.RawValue, 0)
 
@@ -50,10 +33,14 @@ func ActionRawValues(callbackValue string, values ...common.RawValue) string {
 
 	vals := make([]string, len(filtered))
 	for index, val := range filtered {
+		val.Value = sanitizer.Replace(val.Value)
+		val.Display = sanitizer.Replace(val.Display)
+		val.Description = sanitizer.Replace(val.Description)
+
 		if strings.TrimSpace(val.Description) == "" {
-			vals[index] = fmt.Sprintf("%v\t%v", EscapeSpace(sanitizer.Replace(val.Value)), sanitizer.Replace(val.Display))
+			vals[index] = fmt.Sprintf("%v\t%v", val.Value, val.Display)
 		} else {
-			vals[index] = fmt.Sprintf("%v\t%v (%v)", EscapeSpace(sanitizer.Replace(val.Value)), sanitizer.Replace(val.Display), sanitizer.Replace(val.Description))
+			vals[index] = fmt.Sprintf("%v\t%v (%v)", val.Value, val.Display, val.Description)
 		}
 	}
 	return strings.Join(vals, "\n")
diff --git a/internal/zsh/snippet.go b/internal/zsh/snippet.go
index 00326f9..7f8382f 100644
--- a/internal/zsh/snippet.go
+++ b/internal/zsh/snippet.go
@@ -2,34 +2,35 @@ package zsh
 
 import (
 	"fmt"
-	"strings"
 
 	"github.com/rsteube/carapace/internal/uid"
 	"github.com/spf13/cobra"
 )
 
-var replacer = strings.NewReplacer(
-	"\n", ``,
-	"`", `\"`,
-	`:`, `\:`,
-	`"`, `\"`,
-	`[`, `\[`,
-	`]`, `\]`,
-)
-
 func Snippet(cmd *cobra.Command) string {
 	return fmt.Sprintf(`#compdef %v
 function _%v_completion {
   local IFS=$'\n'
-  # shellcheck disable=SC2207,SC2086
-  local c=($(%v _carapace zsh _ ${^words//\\ / }''))
+  
+  # shellcheck disable=SC2207,SC2086,SC2154
+  if echo ${words}"''" | xargs echo 2>/dev/null > /dev/null; then
+    # shellcheck disable=SC2207,SC2086
+    local c=($(echo ${words}"''" | xargs %v _carapace zsh _ ))
+  elif echo ${words} | sed "s/\$/'/" | xargs echo 2>/dev/null > /dev/null; then
+    # shellcheck disable=SC2207,SC2086
+    local c=($(echo ${words} | sed "s/\$/'/" | xargs %v _carapace zsh _ ))
+  else
+    # shellcheck disable=SC2207,SC2086
+    local c=($(echo ${words} | sed 's/$/"/'  | xargs %v _carapace zsh _ ))
+  fi
+
   # shellcheck disable=SC2034,2206
   local vals=(${c%%%%$'\t'*})
   # shellcheck disable=SC2034,2206
   local descriptions=(${c##*$'\t'})
-  compadd -Q -S '' -d descriptions -a -- vals
+  compadd -S '' -d descriptions -a -- vals
 }
 compquote '' 2>/dev/null && _%v_completion
 compdef _%v_completion %v
-`, cmd.Name(), cmd.Name(), uid.Executable(), cmd.Name(), cmd.Name(), cmd.Name())
+`, cmd.Name(), cmd.Name(), uid.Executable(), uid.Executable(), uid.Executable(), cmd.Name(), cmd.Name(), cmd.Name())
 }


Test patch:
diff --git a/example/cmd/_test/zsh.sh b/example/cmd/_test/zsh.sh
index 7bedd0d..2de3b82 100644
--- a/example/cmd/_test/zsh.sh
+++ b/example/cmd/_test/zsh.sh
@@ -1,13 +1,24 @@
 #compdef example
 function _example_completion {
   local IFS=$'\n'
-  # shellcheck disable=SC2207,SC2086
-  local c=($(example _carapace zsh _ ${^words//\\ / }''))
+  
+  # shellcheck disable=SC2207,SC2086,SC2154
+  if echo ${words}"''" | xargs echo 2>/dev/null > /dev/null; then
+    # shellcheck disable=SC2207,SC2086
+    local c=($(echo ${words}"''" | xargs example _carapace zsh _ ))
+  elif echo ${words} | sed "s/\$/'/" | xargs echo 2>/dev/null > /dev/null; then
+    # shellcheck disable=SC2207,SC2086
+    local c=($(echo ${words} | sed "s/\$/'/" | xargs example _carapace zsh _ ))
+  else
+    # shellcheck disable=SC2207,SC2086
+    local c=($(echo ${words} | sed 's/$/"/'  | xargs example _carapace zsh _ ))
+  fi
+
   # shellcheck disable=SC2034,2206
   local vals=(${c%%$'\t'*})
   # shellcheck disable=SC2034,2206
   local descriptions=(${c##*$'\t'})
-  compadd -Q -S '' -d descriptions -a -- vals
+  compadd -S '' -d descriptions -a -- vals
 }
 compquote '' 2>/dev/null && _example_completion
 compdef _example_completion example