Skip to content

Commit

Permalink
bridge: use shlex.Join
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jan 13, 2024
1 parent 148a54f commit f846e02
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ go 1.16

require (
github.com/rsteube/carapace v0.49.0
github.com/rsteube/carapace-shlex v0.1.2
github.com/spf13/cobra v1.8.0
)

replace github.com/rsteube/carapace => ../carapace/
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/rsteube/carapace v0.49.0 h1:HhUyBCiZHdWusl73D652XuWS+3YsYBoS3QXNfpiyv9c=
github.com/rsteube/carapace v0.49.0/go.mod h1:4ZC5bulItu9t9sZ5yPcHgPREd8rPf274Q732n+wfl/o=
github.com/rsteube/carapace-shlex v0.1.1 h1:fRQEBBKyYKm4TXUabm4tzH904iFWSmXJl3UZhMfQNYU=
github.com/rsteube/carapace-shlex v0.1.1/go.mod h1:zPw1dOFwvLPKStUy9g2BYKanI6bsQMATzDMYQQybo3o=
github.com/rsteube/carapace-shlex v0.1.2 h1:ZKjhIfXoCkEnzensMglTaLbkNOaLkmM8SCRshpJKx6s=
github.com/rsteube/carapace-shlex v0.1.2/go.mod h1:zPw1dOFwvLPKStUy9g2BYKanI6bsQMATzDMYQQybo3o=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
Expand Down
3 changes: 2 additions & 1 deletion pkg/actions/bridge/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/rsteube/carapace"
shlex "github.com/rsteube/carapace-shlex"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/pkg/xdg"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ func ActionBash(command ...string) carapace.Action {
return carapace.ActionMessage(err.Error())
}

c.Setenv("COMP_LINE", strings.Join(args, " "))
c.Setenv("COMP_LINE", shlex.Join(args)) // TODO TODO other COMP_* variables
return carapace.ActionExecCommand("bash", "--rcfile", configPath, "-i", "-c", bashSnippet, strings.Join(args, " "))(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines[:len(lines)-1]...).StyleF(style.ForPath)
Expand Down
3 changes: 2 additions & 1 deletion pkg/actions/bridge/click.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/rsteube/carapace"
shlex "github.com/rsteube/carapace-shlex"
)

// ActionClick bridges https://github.com/pallets/click
Expand Down Expand Up @@ -42,7 +43,7 @@ func ActionClick(command ...string) carapace.Action {
args := append(command[1:], c.Args...)
current := c.Value

compLine := command[0] + " " + strings.Join(append(args, current), " ") // TODO escape/quote special characters
compLine := command[0] + " " + shlex.Join(append(args, current))
c.Setenv(fmt.Sprintf("_%v_COMPLETE", strings.ToUpper(command[0])), "zsh_complete")
c.Setenv("COMP_WORDS", compLine)
c.Setenv("COMP_CWORD", strconv.Itoa(len(args)+1))
Expand Down
21 changes: 12 additions & 9 deletions pkg/actions/bridge/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/rsteube/carapace"
shlex "github.com/rsteube/carapace-shlex"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/pkg/xdg"
)
Expand All @@ -22,23 +23,25 @@ func ActionFish(command ...string) carapace.Action {
return carapace.ActionMessage(err.Error())
}

replacer := strings.NewReplacer(
` `, `\ `,
`"`, `\""`,
)
// TODO verify
// replacer := strings.NewReplacer(
// ` `, `\ `,
// `"`, `\""`,
// )

args := append(command, c.Args...)
args = append(args, c.Value)
for index, arg := range args {
args[index] = replacer.Replace(arg)
}
// TODO verify
// args = append(args, c.Value)
// for index, arg := range args {
// args[index] = replacer.Replace(arg)
// }

configPath := fmt.Sprintf("%v/carapace/bridge/fish/config.fish", configDir)
if err := ensureExists(configPath); err != nil {
return carapace.ActionMessage(err.Error())
}

snippet := fmt.Sprintf(`source %#v;complete --do-complete="%v"`, configPath, strings.Join(args, " ")) // TODO needs custom escaping
snippet := fmt.Sprintf(`source %#v;complete --do-complete="%v"`, configPath, shlex.Join(args)) // TODO needs custom escaping
return carapace.ActionExecCommand("fish", "--no-config", "--command", snippet)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

Expand Down
4 changes: 2 additions & 2 deletions pkg/actions/bridge/inshellisense.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/rsteube/carapace"
shlex "github.com/rsteube/carapace-shlex"
)

// ActionInshellisense bridges https://github.com/microsoft/inshellisense
Expand All @@ -16,8 +17,7 @@ func ActionInshellisense(command ...string) carapace.Action {

args := append(command, c.Args...)
args = append(args, c.Value)
input := strings.Join(args, " ") // TODO simple join for now as the lexer in inshellisense can't handle quotes and spaces anyway
return carapace.ActionExecCommand("inshellisense", "complete", input)(func(output []byte) carapace.Action {
return carapace.ActionExecCommand("inshellisense", "complete", shlex.Join(args))(func(output []byte) carapace.Action {
var r struct {
Suggestions []struct {
Name string
Expand Down
3 changes: 2 additions & 1 deletion pkg/actions/bridge/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/rsteube/carapace"
shlex "github.com/rsteube/carapace-shlex"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/pkg/xdg"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func ActionPowershell(command ...string) carapace.Action {
// args[index] = strings.Replace(arg, " ", "` ", -1)
// }

line := strings.Join(args, " ")
line := shlex.Join(args)
snippet := []string{
fmt.Sprintf(`Get-Content "%v/carapace/bridge/powershell/Microsoft.PowerShell_profile.ps1" | Out-String | Invoke-Expression`, configDir),
fmt.Sprintf(`[System.Management.Automation.CommandCompletion]::CompleteInput("%v", %v, $null).CompletionMatches | ConvertTo-Json `, line, len(line)),
Expand Down

0 comments on commit f846e02

Please sign in to comment.