Skip to content

Commit

Permalink
Merge pull request #269 from rsteube/zsh-reenable-special-characters
Browse files Browse the repository at this point in the history
zsh re-enabled special characters
  • Loading branch information
rsteube authored Mar 25, 2021
2 parents 1fd11b2 + 314577b commit 39cecbb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
17 changes: 14 additions & 3 deletions example/cmd/_test/zsh.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 9 additions & 22 deletions internal/zsh/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,10 @@ import (
)

var sanitizer = strings.NewReplacer(
`$`, ``,
"`", ``,
"\n", ``,
`\`, ``,
`"`, ``,
`'`, ``,
"`", ``,
`|`, ``,
`>`, ``,
`<`, ``,
`&`, ``,
`(`, ``,
`)`, ``,
`;`, ``,
`#`, ``,
`[`, `\[`,
`]`, `\]`,
"\r", ``,
"\t", ``,
`'`, `'\''`,
)

func Sanitize(values ...string) []string {
Expand All @@ -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)

Expand All @@ -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")
Expand Down
29 changes: 15 additions & 14 deletions internal/zsh/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit 39cecbb

Please sign in to comment.