Skip to content

Commit

Permalink
Merge pull request #80 from rsteube/explicit-shell
Browse files Browse the repository at this point in the history
support explicit shell
  • Loading branch information
rsteube committed Mar 28, 2023
2 parents d07868d + be73c05 commit c61b575
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions cmd/carapace-bridge/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package cmd

import (
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bridge/pkg/actions/bridge"
"github.com/rsteube/carapace-bridge/pkg/actions/os"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "carapace-bridge",
Short: "bridge completions",
Run: func(cmd *cobra.Command, args []string) {},
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
}

func Execute(version string) error {
rootCmd.Version = version
return rootCmd.Execute()
}
func init() {
carapace.Gen(rootCmd).Standalone()
carapace.Gen(rootCmd)
addSubCommand("argcomplete", "bridges https://github.com/kislyuk/argcomplete", bridge.ActionArgcomplete)
addSubCommand("carapace-bin", "bridges completions registered in carapace-bin", bridge.ActionCarapaceBin)
addSubCommand("carapace", "bridges https://github.com/rsteube/carapace", bridge.ActionCarapace)
Expand All @@ -36,7 +41,13 @@ func addSubCommand(use, short string, f func(s ...string) carapace.Action) {
Short: short,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
rootCmd.SetArgs(append([]string{"_carapace", "export", "", use}, args...))
splitted := strings.Split(args[0], "/")
args[0] = splitted[0]
shell := "export"
if len(splitted) > 1 {
shell = splitted[1]
}
rootCmd.SetArgs(append([]string{"_carapace", shell, "", use}, args...))
rootCmd.Execute()
},
DisableFlagParsing: true,
Expand All @@ -47,12 +58,34 @@ func addSubCommand(use, short string, f func(s ...string) carapace.Action) {
rootCmd.AddCommand(cmd)

carapace.Gen(cmd).PositionalCompletion(
os.ActionPathExecutables(),
carapace.ActionMultiParts("/", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return os.ActionPathExecutables()
case 1:
return carapace.ActionStyledValues(
"bash", "#d35673",
"bash-ble", "#c2039a",
"elvish", "#ffd6c9",
"export", style.Default,
"fish", "#7ea8fc",
"ion", "#0e5d6d",
"nushell", "#29d866",
"oil", "#373a36",
"powershell", "#e8a16f",
"tcsh", "#412f09",
"xonsh", "#a8ffa9",
"zsh", "#efda53",
)
default:
return carapace.ActionValues()
}
}),
)

carapace.Gen(cmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
command := c.Args[0]
command := strings.Split(c.Args[0], "/")[0]
c.Args = c.Args[1:]
return f(command).Invoke(c).ToA()
}),
Expand Down

0 comments on commit c61b575

Please sign in to comment.