@@ -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
1114var 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