Skip to content

Commit

Permalink
Merge pull request #100 from rsteube/bash
Browse files Browse the repository at this point in the history
added bash
  • Loading branch information
rsteube committed Mar 30, 2023
2 parents 8c1b540 + ac5cd93 commit 756c7ce
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Invokes completion of various frameworks and provides bridge actions for [carapa

Supported frameworks:
- [argcomplete](https://github.com/kislyuk/argcomplete)
- [bash](https://www.gnu.org/software/bash/)
- [carapace](https://github.com/rsteube/carapace)
- [carapace-bin](https://github.com/rsteube/carapace-bin/)
- [click](https://github.com/pallets/click)
Expand Down
1 change: 1 addition & 0 deletions cmd/carapace-bridge/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {
carapace.Gen(rootCmd)
rootCmd.AddGroup(&cobra.Group{ID: "bridge", Title: "Bridge Commands"})
addSubCommand("argcomplete", "bridges https://github.com/kislyuk/argcomplete", bridge.ActionArgcomplete)
addSubCommand("bash", "bridges completions registered in bash", bridge.ActionBash)
addSubCommand("carapace-bin", "bridges completions registered in carapace-bin", bridge.ActionCarapaceBin)
addSubCommand("carapace", "bridges https://github.com/rsteube/carapace", bridge.ActionCarapace)
addSubCommand("click", "bridges https://github.com/pallets/click", bridge.ActionClick)
Expand Down
49 changes: 49 additions & 0 deletions pkg/actions/bridge/bash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package bridge

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

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

//go:embed bash.sh
var bashSnippet string

// ActionBash bridges completions registered in bash
// (uses custom `.bashrc` in “~/.config/carapace/bridge/bash`)
func ActionBash(command ...string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if len(command) == 0 {
return carapace.ActionMessage("missing argument [ActionFish]")
}

configDir, err := xdg.UserConfigDir()
if err != nil {
return carapace.ActionMessage(err.Error())
}

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

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

// for index, arg := range args {
// args[index] = replacer.Replace(arg)
// }

rcfile := fmt.Sprintf("%v/carapace/bridge/bash/.bashrc", configDir)
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 {
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
})
}
18 changes: 18 additions & 0 deletions pkg/actions/bridge/bash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash -i
# COMP_LINE="$1"
COMP_WORDS=($COMP_LINE)
if [ "${COMP_LINE: -1}" = " " ]; then
COMP_WORDS+=("")
fi
COMP_CWORD=$((${#COMP_WORDS[@]} - 1))
COMP_POINT=${#COMP_LINE}

$"$(complete -p "${COMP_WORDS[0]}" | sed -r 's/.* -F ([^ ]+).*/\1/')"

for i in "${COMPREPLY[@]}"; do
if [[ -d "${i}" && "${i}" != */ ]]; then
echo "${i}/"
else
echo "${i}"
fi
done

0 comments on commit 756c7ce

Please sign in to comment.