Skip to content

Commit

Permalink
Merge pull request #270 from rsteube/bash-reenabled-special-characters
Browse files Browse the repository at this point in the history
bash: re-enabled special characters
  • Loading branch information
rsteube authored Mar 25, 2021
2 parents 39cecbb + 31bad19 commit f5a6035
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ RUN ln -s /carapace/example/example /usr/local/bin/example
# bash
RUN echo -e "\n\
PS1=$'\e[0;36mcarapace-bash \e[0m'\n\
source /usr/share/bash-completion/bash_completion \n\
source <(example _carapace)" \
> ~/.bashrc

Expand Down
45 changes: 27 additions & 18 deletions internal/bash/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,33 @@ import (
)

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

var quoter = strings.NewReplacer(
// seems readline provides quotation only for the filename completion (which would add suffixes) so do that here
`&`, `\&`,
`<`, `\<`,
`>`, `\>`,
"`", "\\`",
`'`, `\'`,
`"`, `\"`,
`{`, `\{`,
`}`, `\}`,
`$`, `\$`,
`#`, `\#`,
`|`, `\|`,
`?`, `\?`,
`(`, `\(`,
`)`, `\)`,
`;`, `\;`,
` `, `\ `,
`[`, `\[`,
`]`, `\]`,
`*`, `\*`,
`\`, `\\`,
)

func Sanitize(values ...string) []string {
Expand All @@ -33,10 +46,6 @@ func Sanitize(values ...string) []string {
return sanitized
}

func EscapeSpace(s string) string {
return strings.Replace(s, " ", `\ `, -1)
}

func commonPrefix(a, b string) string {
i := 0
for i < len(a) && i < len(b) && a[i] == b[i] {
Expand Down Expand Up @@ -100,7 +109,7 @@ func ActionRawValues(callbackValue string, values ...common.RawValue) string {
vals := make([]string, len(filtered))
for index, val := range filtered {
if len(filtered) == 1 {
vals[index] = EscapeSpace(sanitizer.Replace(val.Value))
vals[index] = quoter.Replace(sanitizer.Replace(val.Value))
} else {
if val.Description != "" {
vals[index] = fmt.Sprintf("%v (%v)", val.Display, sanitizer.Replace(val.Description))
Expand Down

0 comments on commit f5a6035

Please sign in to comment.