Skip to content

Commit

Permalink
Merge pull request #102 from rsteube/remove-xdg
Browse files Browse the repository at this point in the history
source config directly and don't mess with XDG
  • Loading branch information
rsteube committed Apr 1, 2023
2 parents f77f68f + 2e930c1 commit 782358e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
9 changes: 6 additions & 3 deletions pkg/actions/bridge/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ func ActionBash(command ...string) carapace.Action {
// args[index] = replacer.Replace(arg)
// }

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

c.Setenv("COMP_LINE", strings.Join(args, " "))
c.Setenv("XDG_CONFIG_HOME", fmt.Sprintf("%v/carapace/bridge", configDir))
return carapace.ActionExecCommand("bash", "--rcfile", rcfile, "-i", "-c", bashSnippet, strings.Join(args, " "))(func(output []byte) carapace.Action {
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)
}).Invoke(c).ToA().NoSpace([]rune("/=@:.,")...) // TODO check compopt for nospace
Expand Down
13 changes: 8 additions & 5 deletions pkg/actions/bridge/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ func ActionFish(command ...string) carapace.Action {
for index, arg := range args {
args[index] = replacer.Replace(arg)
}
snippet := fmt.Sprintf(`complete --do-complete="%v"`, strings.Join(args, " ")) // TODO needs custom escaping

c.Setenv("XDG_CONFIG_HOME", fmt.Sprintf("%v/carapace/bridge", configDir))
carapace.LOG.Println(snippet)
return carapace.ActionExecCommand("fish", "--command", snippet)(func(output []byte) carapace.Action {
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
return carapace.ActionExecCommand("fish", "--no-config", "--command", snippet)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

vals := make([]string, 0)
Expand All @@ -55,6 +58,6 @@ func ActionFish(command ...string) carapace.Action {
}
return style.ForPath(s, sc)
})
}).Invoke(c).ToA().NoSpace([]rune("/=@:.,")...)
}).NoSpace([]rune("/=@:.,")...)
})
}
27 changes: 23 additions & 4 deletions pkg/actions/bridge/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ package bridge
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/pkg/xdg"
)

func ensureExists(path string) (err error) {
if _, err = os.Stat(path); err == nil || !os.IsNotExist(err) {
return
}
if err = os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
return
}
_, err = os.Create(path)
return
}

// ActionPowershell bridges completions registered in powershell
// (uses custom `Microsoft.PowerShell_profile.ps1` in “~/.config/carapace/bridge/powershell`)
func ActionPowershell(command ...string) carapace.Action {
Expand All @@ -22,19 +35,25 @@ func ActionPowershell(command ...string) carapace.Action {
if err != nil {
return carapace.ActionMessage(err.Error())
}
c.Setenv("XDG_CONFIG_HOME", fmt.Sprintf("%v/carapace/bridge", configDir))
configPath := fmt.Sprintf("%v/carapace/bridge/powershell/Microsoft.PowerShell_profile.ps1", configDir)
if err := ensureExists(configPath); err != nil {
return carapace.ActionMessage(err.Error())
}

args := append(command, c.Args...)
args = append(args, c.CallbackValue)

// for index, arg := range args {
// TODO handle different escape character and escapcing in general
// TODO handle different escape character and escaping in general
// args[index] = strings.Replace(arg, " ", "` ", -1)
// }

line := strings.Join(args, " ")
snippet := fmt.Sprintf(`[System.Management.Automation.CommandCompletion]::CompleteInput("%v", %v, $null).CompletionMatches | ConvertTo-Json `, line, len(line))
return carapace.ActionExecCommand("pwsh", "-Command", snippet)(func(output []byte) carapace.Action {
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)),
}
return carapace.ActionExecCommand("pwsh", "-Command", strings.Join(snippet, ";"))(func(output []byte) carapace.Action {
if len(output) == 0 {
return carapace.ActionValues()
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/actions/bridge/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bridge

import (
_ "embed"
"fmt"
"strings"

"github.com/rsteube/carapace"
Expand All @@ -19,17 +18,20 @@ func ActionZsh(command ...string) carapace.Action {
return carapace.ActionMessage("missing argument [ActionZsh]")
}

args := []string{"--no-rcs", "-c", zsh.Script, "--"}
args = append(args, command...)
args = append(args, c.Args...)
args = append(args, c.CallbackValue)

configDir, err := xdg.UserConfigDir()
if err != nil {
return carapace.ActionMessage(err.Error())
}
if err := ensureExists(configDir + "/carapace/bridge/zsh/.zshrc"); err != nil {
return carapace.ActionMessage(err.Error())
}

args := []string{"-c", zsh.Script, "--"}
args = append(args, command...)
args = append(args, c.Args...)
args = append(args, c.CallbackValue)

c.Setenv("XDG_CONFIG_HOME", fmt.Sprintf("%v/carapace/bridge", configDir))
c.Setenv("CARAPACE_BRIDGE_CONFIG_HOME", configDir)
return carapace.ActionExecCommand("zsh", args...)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\r\n")
vals := make([]string, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ PROMPT=
# load completion system
autoload compinit
compinit -d ${XDG_CONFIG_HOME:-~}/.zcompdump_capture
source "${XDG_CONFIG_HOME:-~/.config}/zsh/.zshrc"
compinit -d "${CARAPACE_BRIDGE_CONFIG_HOME:-~/.config}/carapace/bridge/zsh/.zcompdump_capture"
source "${CARAPACE_BRIDGE_CONFIG_HOME:-~/.config}/carapace/bridge/zsh/.zshrc"
# never run a command
bindkey ''^M'' undefined
Expand Down

0 comments on commit 782358e

Please sign in to comment.