Skip to content

Commit 81b1005

Browse files
authored
Merge pull request #291 from carapace-sh/choices
replaced bridges.yaml with choices
2 parents 7a2cd1c + 58ba19b commit 81b1005

16 files changed

Lines changed: 366 additions & 68 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
services:
2-
urfavecliV1:
2+
urfavecli_v1:
33
build:
4-
context: urfavecliV1
4+
context: urfavecli_v1
55
image: ghcr.io/carapace-sh/carapace-bridge:urfavecliV1
66
hostname: carapace-bridge:urfavecliV1_tea
77
volumes:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ RUN apk add --no-cache curl libc6-compat tea
55
RUN curl -so - https://dl.elv.sh/linux-amd64/elvish-v0.21.0.tar.gz | tar -xzvC /usr/local/bin
66

77
RUN mkdir -p ~/.config/elvish \
8-
&& echo -e "set paths = [ /carapace-bridge/cmd/carapace-bridge \$@paths ]\neval (carapace-bridge _carapace|slurp)\neval (carapace-bridge urfavecli@v1 tea|slurp)" > ~/.config/elvish/rc.elv
8+
&& echo -e "set paths = [ /carapace-bridge/cmd/carapace-bridge \$@paths ]\neval (carapace-bridge _carapace|slurp)\neval (carapace-bridge urfavecli_v1 tea|slurp)" > ~/.config/elvish/rc.elv
99
ENV PATH="/carapace-bridge/cmd/carapace-bridge:$PATH"
1010

1111
ENTRYPOINT /usr/local/bin/elvish

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Supported inputs:
1515
- [bash](https://www.gnu.org/software/bash/)
1616
- [carapace](https://github.com/carapace-sh/carapace)
1717
- [carapace-bin](https://github.com/carapace-sh/carapace-bin/)
18-
- [clap](https://github.com/clap-rs/clap)
18+
- [clap](https://github.com/clap-rs/clap) ([experimental](https://github.com/clap-rs/clap/issues/3166))
1919
- [click](https://github.com/pallets/click)
2020
- [cobra](https://github.com/spf13/cobra)
2121
- [complete](https://github.com/posener/complete)

cmd/carapace-bridge/cmd/choice.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/carapace-sh/carapace"
7+
"github.com/carapace-sh/carapace-bridge/pkg/actions/bridge"
8+
"github.com/carapace-sh/carapace-bridge/pkg/actions/choice"
9+
"github.com/carapace-sh/carapace-bridge/pkg/choices"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var choiceCmd = &cobra.Command{
14+
Use: "choice [-d] [variant]...",
15+
Short: "list or edit choices",
16+
Args: cobra.ArbitraryArgs,
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
if len(args) == 0 {
19+
choices, err := choices.List(true)
20+
if err != nil {
21+
return err
22+
}
23+
for _, choice := range choices {
24+
fmt.Println(choice.Format())
25+
}
26+
return nil
27+
}
28+
29+
switch cmd.Flag("delete").Changed {
30+
case true:
31+
for _, arg := range args {
32+
if err := choices.Unset(arg); err != nil {
33+
return err
34+
}
35+
}
36+
default:
37+
for _, arg := range args {
38+
if err := choices.Set(choices.Parse(arg)); err != nil {
39+
return err
40+
}
41+
}
42+
}
43+
return nil
44+
},
45+
}
46+
47+
func init() {
48+
carapace.Gen(choiceCmd).Standalone()
49+
choiceCmd.Flags().SetInterspersed(false)
50+
51+
choiceCmd.Flags().BoolP("delete", "d", false, "delete given choice(s)")
52+
rootCmd.AddCommand(choiceCmd)
53+
54+
carapace.Gen(choiceCmd).PositionalAnyCompletion(
55+
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
56+
if choiceCmd.Flag("delete").Changed {
57+
return choice.ActionChoices()
58+
}
59+
60+
return carapace.ActionMultiPartsN("/", 2, func(c carapace.Context) carapace.Action {
61+
switch len(c.Parts) {
62+
case 0:
63+
return carapace.ActionExecutables().Suffix("/")
64+
default:
65+
return bridge.ActionBridges(c.Parts[0]).Filter("macro").Suffix("@bridge")
66+
}
67+
})
68+
}),
69+
)
70+
}

cmd/carapace-bridge/cmd/root.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ func init() {
4040
carapace.Gen(rootCmd)
4141
rootCmd.AddGroup(&cobra.Group{ID: "bridge", Title: "Bridge Commands"})
4242
addSubCommand("argcomplete", "bridges https://github.com/kislyuk/argcomplete", bridge.ActionArgcomplete)
43-
addSubCommand("argcomplete@v1", "bridges https://github.com/kislyuk/argcomplete", bridge.ActionArgcompleteV1)
43+
addSubCommand("argcomplete_v1", "bridges https://github.com/kislyuk/argcomplete", bridge.ActionArgcompleteV1)
4444
addSubCommand("aws", "bridges https://github.com/aws/aws-cli", bridge.ActionAws)
4545
addSubCommand("bash", "bridges completions registered in bash", bridge.ActionBash)
46+
addSubCommand("bridge", "bridges completions defined by choices and CARAPACE_BRIDGE", bridge.ActionBridge)
4647
addSubCommand("carapace-bin", "bridges completions registered in carapace-bin", bridge.ActionCarapaceBin)
4748
addSubCommand("carapace", "bridges https://github.com/carapace-sh/carapace", bridge.ActionCarapace)
4849
addSubCommand("clap", "bridges https://github.com/clap-rs/clap", bridge.ActionClap)
@@ -56,8 +57,8 @@ func init() {
5657
addSubCommand("kitten", "bridges https://github.com/kovidgoyal/kitty", bridge.ActionKitten)
5758
addSubCommand("macro", "bridges macros exposed with https://github.com/carapace-sh/carapace-spec", bridge.ActionMacro)
5859
addSubCommand("powershell", "bridges completions registered in powershell", bridge.ActionPowershell)
59-
addSubCommand("urfavecli", "bridges https://github.com/urfave/cli (v2)", bridge.ActionUrfavecli)
60-
addSubCommand("urfavecli@v1", "bridges https://github.com/urfave/cli (v3)", bridge.ActionUrfavecliV1)
60+
addSubCommand("urfavecli", "bridges https://github.com/urfave/cli", bridge.ActionUrfavecli)
61+
addSubCommand("urfavecli_v1", "bridges https://github.com/urfave/cli", bridge.ActionUrfavecliV1)
6162
addSubCommand("yargs", "bridges https://github.com/yargs/yargs", bridge.ActionYargs)
6263
addSubCommand("zsh", "bridges completions registered in zsh", bridge.ActionZsh)
6364
}

cmd/go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ go 1.23.1
55
replace github.com/carapace-sh/carapace-bridge => ../
66

77
require (
8-
github.com/carapace-sh/carapace v1.5.0
8+
github.com/carapace-sh/carapace v1.10.3
99
github.com/carapace-sh/carapace-bridge v0.0.0-00010101000000-000000000000
1010
github.com/carapace-sh/carapace-selfupdate v0.0.5
11-
github.com/spf13/cobra v1.8.1
11+
github.com/spf13/cobra v1.9.1
1212
)
1313

1414
require (
15-
github.com/carapace-sh/carapace-shlex v1.0.1 // indirect
15+
github.com/carapace-sh/carapace-shlex v1.1.1 // indirect
1616
github.com/inconshreveable/mousetrap v1.1.0 // indirect
17-
github.com/spf13/pflag v1.0.5 // indirect
17+
github.com/spf13/pflag v1.0.9 // indirect
1818
gopkg.in/yaml.v3 v3.0.1 // indirect
1919
)

cmd/go.sum

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
github.com/carapace-sh/carapace v1.5.0 h1:rjNMTo5sY3YybMKbzEuaM19HEG6JXhvCTOrioV0QUHY=
2-
github.com/carapace-sh/carapace v1.5.0/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs=
1+
github.com/carapace-sh/carapace v1.10.3 h1:4L4HpEEZb+I2mtvqKMYed64QR15Tf6+uGDn8BIflObY=
2+
github.com/carapace-sh/carapace v1.10.3/go.mod h1:gdyqmVIQJFlcHn4D7BkgyjRU5LlluDXBaiRSdV6GY88=
33
github.com/carapace-sh/carapace-selfupdate v0.0.5 h1:Th7PtHlS2Kuhs1Kn+qskVoGWaPKFCwbvDrU4bVQA43c=
44
github.com/carapace-sh/carapace-selfupdate v0.0.5/go.mod h1:gyvAPrYpUU6pCgH+Mox+LTotxCQmUTHl+u/g4j/H9aQ=
5-
github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c=
6-
github.com/carapace-sh/carapace-shlex v1.0.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M=
7-
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
5+
github.com/carapace-sh/carapace-shlex v1.1.1 h1:ccmNeetAYZOk4IcV36youFDsXusT9uCNW2Njkw+QS+Q=
6+
github.com/carapace-sh/carapace-shlex v1.1.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M=
7+
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
88
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
99
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
1010
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
11-
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
12-
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
13-
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
14-
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
11+
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
12+
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
13+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
14+
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
15+
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1516
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1617
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1718
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

compose.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
include:
22
- ./.docker/argcomplete.yaml
3+
# TODO argcomplete_v1
34
- ./.docker/click.yaml
45
- ./.docker/cobra.yaml
56
- ./.docker/complete.yaml
67
- ./.docker/kingpin.yaml
78
- ./.docker/urfavecli.yaml
8-
- ./.docker/urfavecliV1.yaml
9+
- ./.docker/urfavecli_v1.yaml
910
- ./.docker/yargs.yaml
1011

1112
- ./.docker/inshellisense.yaml

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ require (
66
github.com/carapace-sh/carapace v1.10.3
77
github.com/carapace-sh/carapace-shlex v1.1.1
88
github.com/spf13/cobra v1.9.1
9-
gopkg.in/yaml.v3 v3.0.1
109
)
1110

1211
require (
1312
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1413
github.com/spf13/pflag v1.0.9 // indirect
14+
gopkg.in/yaml.v3 v3.0.1 // indirect
1515
)

pkg/actions/bridge/bridge.go

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55

66
"github.com/carapace-sh/carapace"
77
"github.com/carapace-sh/carapace-bridge/pkg/bridges"
8+
"github.com/carapace-sh/carapace-bridge/pkg/choices"
89
"github.com/carapace-sh/carapace-bridge/pkg/env"
10+
"github.com/carapace-sh/carapace/pkg/style"
11+
"github.com/carapace-sh/carapace/third_party/golang.org/x/sys/execabs"
912
)
1013

1114
var bridgeActions = map[string]func(command ...string) carapace.Action{
1215
"argcomplete": ActionArgcomplete,
13-
"argcomplete@v1": ActionArgcompleteV1,
16+
"argcomplete_v1": ActionArgcompleteV1,
1417
"aws": ActionAws,
1518
"bash": ActionBash,
1619
"carapace": ActionCarapace,
@@ -26,7 +29,7 @@ var bridgeActions = map[string]func(command ...string) carapace.Action{
2629
"kitten": ActionKitten,
2730
"powershell": ActionPowershell,
2831
"urfavecli": ActionUrfavecli,
29-
"urfavecli@v1": ActionUrfavecliV1,
32+
"urfavecli_v1": ActionUrfavecliV1,
3033
"yargs": ActionYargs,
3134
"zsh": ActionZsh,
3235
}
@@ -37,15 +40,87 @@ func Get(name string) (func(command ...string) carapace.Action, bool) {
3740
return a, ok
3841
}
3942

40-
// Bridges bridges completions as defined in bridges.yaml and CARAPACE_BRIDGE environment variable
41-
func ActionBridges(command ...string) carapace.Action {
43+
// ActionBridges completes available bridges.
44+
//
45+
// complete
46+
// cobra
47+
func ActionBridges(name string) carapace.Action {
48+
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
49+
return carapace.Batch(
50+
carapace.ActionValuesDescribed(
51+
"argcomplete", "bridges https://github.com/kislyuk/argcomplete",
52+
"argcomplete_v1", "bridges https://github.com/kislyuk/argcomplete",
53+
"carapace", "bridges https://github.com/carapace-sh/carapace",
54+
"clap", "bridges https://github.com/clap-rs/clap",
55+
"click", "bridges https://github.com/pallets/click",
56+
"cobra", "bridges https://github.com/spf13/cobra",
57+
"complete", "bridges https://github.com/posener/complete",
58+
"kingpin", "bridges https://github.com/alecthomas/kingpin",
59+
"macro", "bridges macros exposed with https://github.com/carapace-sh/carapace-spec",
60+
"urfavecli", "bridges https://github.com/urfave/cli (v2)",
61+
"urfavecli_v1", "bridges https://github.com/urfave/cli (v3)",
62+
"yargs", "bridges https://github.com/yargs/yargs",
63+
).Style(style.Dim),
64+
carapace.ActionValuesDescribed(
65+
"aws", "bridges https://github.com/aws/aws-cli",
66+
"bash", "bridges completions registered in bash",
67+
"carapace-bin", "bridges completions registered in carapace-bin",
68+
"fish", "bridges completions registered in fish",
69+
"gcloud", "bridges https://docs.cloud.google.com/sdk/gcloud",
70+
"inshellisense", "bridges https://github.com/microsoft/inshellisense",
71+
"kitten", "bridges https://github.com/kovidgoyal/kitty",
72+
"powershell", "bridges completions registered in powershell",
73+
"zsh", "bridges completions registered in zsh",
74+
).StyleF(func(s string, sc style.Context) string {
75+
executable := map[string]string{
76+
"aws": "aws",
77+
"bash": "bash",
78+
"carapace-bin": "carapace",
79+
"fish": "fish",
80+
"gcloud": "gcloud",
81+
"inshellisense": "inshellisense",
82+
"kitten": "kitten",
83+
"powershell": "pwsh",
84+
"zsh": "zsh",
85+
}[s]
86+
87+
if _, err := execabs.LookPath(executable); err != nil {
88+
return style.Carapace.KeywordNegative
89+
}
90+
91+
switch s {
92+
case "bash":
93+
if slices.Contains(bridges.Bash(), name) {
94+
return style.Carapace.KeywordPositive
95+
}
96+
case "fish":
97+
if slices.Contains(bridges.Fish(), name) {
98+
return style.Carapace.KeywordPositive
99+
}
100+
case "inshellisense":
101+
if slices.Contains(bridges.Inshellisense(), name) {
102+
return style.Carapace.KeywordPositive
103+
}
104+
case "zsh":
105+
if slices.Contains(bridges.Zsh(), name) {
106+
return style.Carapace.KeywordPositive
107+
}
108+
}
109+
return style.Default
110+
}),
111+
).ToA().Tag("bridges")
112+
})
113+
}
114+
115+
// ActionBridge bridges completions defined by choices and CARAPACE_BRIDGE
116+
func ActionBridge(command ...string) carapace.Action {
42117
return actionCommand(command...)(func(command ...string) carapace.Action {
43118
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
44-
if bridge, ok := bridges.Config()[command[0]]; ok {
45-
if action, ok := bridgeActions[bridge]; ok {
119+
if choice, err := choices.Get(command[0]); err == nil && (choice.Group == "" || choice.Group == "bridge") {
120+
if action, ok := bridgeActions[choice.Variant]; ok {
46121
return action(command...)
47122
}
48-
return carapace.ActionMessage("unknown bridge: %v", bridge)
123+
return carapace.ActionMessage("unknown bridge/variant: %v", choice.Variant)
49124
}
50125

51126
for _, b := range env.Bridges() {

0 commit comments

Comments
 (0)