From 314577bd49cb79528053caae3e0a13f3e58a1e9f Mon Sep 17 00:00:00 2001 From: rsteube Date: Thu, 25 Mar 2021 11:54:51 +0100 Subject: [PATCH] zsh re-enabled special characters --- example/cmd/_test/zsh.sh | 17 ++++++++++++++--- internal/zsh/action.go | 31 +++++++++---------------------- internal/zsh/snippet.go | 29 +++++++++++++++-------------- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/example/cmd/_test/zsh.sh b/example/cmd/_test/zsh.sh index 7bedd0df4..2de3b82bc 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 diff --git a/internal/zsh/action.go b/internal/zsh/action.go index a1d472318..cedb216dc 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 00326f9e5..7f8382fba 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()) }