Skip to content

Commit

Permalink
gum: common flag completion
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jan 14, 2025
1 parent 91b6506 commit fa17247
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 350 deletions.
27 changes: 2 additions & 25 deletions completers/gum_completer/cmd/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gum"
"github.com/carapace-sh/carapace-bin/completers/gum_completer/cmd/common"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -94,33 +94,10 @@ func init() {
chooseCmd.Flags().String("unselected-prefix", "", "Prefix to show on unselected items (hidden if limit is 1)")
rootCmd.AddCommand(chooseCmd)

common.AddFlagCompletion(chooseCmd)
carapace.Gen(chooseCmd).FlagCompletion(carapace.ActionMap{
"cursor.align": gum.ActionAlignments(),
"cursor.background": gum.ActionColors(),
"cursor.border": gum.ActionBorders(),
"cursor.border-background": gum.ActionColors(),
"cursor.border-foreground": gum.ActionColors(),
"cursor.foreground": gum.ActionColors(),
"header.align": gum.ActionAlignments(),
"header.background": gum.ActionColors(),
"header.border": gum.ActionBorders(),
"header.border-background": gum.ActionColors(),
"header.border-foreground": gum.ActionColors(),
"header.foreground": gum.ActionColors(),
"item.align": gum.ActionAlignments(),
"item.background": gum.ActionColors(),
"item.border": gum.ActionBorders(),
"item.border-background": gum.ActionColors(),
"item.border-foreground": gum.ActionColors(),
"item.foreground": gum.ActionColors(),
"selected": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.ActionValues(c.Args...).UniqueList(",")
}),
"selected.align": gum.ActionAlignments(),
"selected.background": gum.ActionColors(),
"selected.border": gum.ActionBorders(),
"selected.border-background": gum.ActionColors(),
"selected.border-foreground": gum.ActionColors(),
"selected.foreground": gum.ActionColors(),
})
}
27 changes: 27 additions & 0 deletions completers/gum_completer/cmd/common/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package common

import (
"path/filepath"
"strings"

"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gum"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func AddFlagCompletion(cmd *cobra.Command) {
m := make(carapace.ActionMap)
cmd.Flags().VisitAll(func(f *pflag.Flag) {
switch strings.TrimPrefix(filepath.Ext(f.Name), ".") {
case "background", "foreground", "border-background", "border-foreground":
m[f.Name] = gum.ActionColors()
case "align":
m[f.Name] = gum.ActionAlignments()
case "border":
m[f.Name] = gum.ActionBorders()
}
})

carapace.Gen(cmd).FlagCompletion(m)
}
23 changes: 2 additions & 21 deletions completers/gum_completer/cmd/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gum"
"github.com/carapace-sh/carapace-bin/completers/gum_completer/cmd/common"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -68,24 +68,5 @@ func init() {
confirmCmd.Flags().String("unselected.width", "", "Text width")
rootCmd.AddCommand(confirmCmd)

carapace.Gen(confirmCmd).FlagCompletion(carapace.ActionMap{
"prompt.align": gum.ActionAlignments(),
"prompt.background": gum.ActionColors(),
"prompt.border": gum.ActionBorders(),
"prompt.border-background": gum.ActionColors(),
"prompt.border-foreground": gum.ActionColors(),
"prompt.foreground": gum.ActionColors(),
"selected.align": gum.ActionAlignments(),
"selected.background": gum.ActionColors(),
"selected.border": gum.ActionBorders(),
"selected.border-background": gum.ActionColors(),
"selected.border-foreground": gum.ActionColors(),
"selected.foreground": gum.ActionColors(),
"unselected.align": gum.ActionAlignments(),
"unselected.background": gum.ActionColors(),
"unselected.border": gum.ActionBorders(),
"unselected.border-background": gum.ActionColors(),
"unselected.border-foreground": gum.ActionColors(),
"unselected.foreground": gum.ActionColors(),
})
common.AddFlagCompletion(confirmCmd)
}
47 changes: 2 additions & 45 deletions completers/gum_completer/cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gum"
"github.com/carapace-sh/carapace-bin/completers/gum_completer/cmd/common"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -147,50 +147,7 @@ func init() {
fileCmd.Flags().String("timeout", "", "Timeout until command aborts without a selection")
rootCmd.AddCommand(fileCmd)

carapace.Gen(fileCmd).FlagCompletion(carapace.ActionMap{
"cursor.align": gum.ActionAlignments(),
"cursor.background": gum.ActionColors(),
"cursor.border": gum.ActionBorders(),
"cursor.border-background": gum.ActionColors(),
"cursor.border-foreground": gum.ActionColors(),
"cursor.foreground": gum.ActionColors(),
"directory.align": gum.ActionAlignments(),
"directory.background": gum.ActionColors(),
"directory.border": gum.ActionBorders(),
"directory.border-background": gum.ActionColors(),
"directory.border-foreground": gum.ActionColors(),
"directory.foreground": gum.ActionColors(),
"file-size.align": gum.ActionAlignments(),
"file-size.background": gum.ActionColors(),
"file-size.border": gum.ActionBorders(),
"file-size.border-background": gum.ActionColors(),
"file-size.border-foreground": gum.ActionColors(),
"file-size.foreground": gum.ActionColors(),
"file.align": gum.ActionAlignments(),
"file.background": gum.ActionColors(),
"file.border": gum.ActionBorders(),
"file.border-background": gum.ActionColors(),
"file.border-foreground": gum.ActionColors(),
"file.foreground": gum.ActionColors(),
"permissions.align": gum.ActionAlignments(),
"permissions.background": gum.ActionColors(),
"permissions.border": gum.ActionBorders(),
"permissions.border-background": gum.ActionColors(),
"permissions.border-foreground": gum.ActionColors(),
"permissions.foreground": gum.ActionColors(),
"selected.align": gum.ActionAlignments(),
"selected.background": gum.ActionColors(),
"selected.border": gum.ActionBorders(),
"selected.border-background": gum.ActionColors(),
"selected.border-foreground": gum.ActionColors(),
"selected.foreground": gum.ActionColors(),
"symlink.align": gum.ActionAlignments(),
"symlink.background": gum.ActionColors(),
"symlink.border": gum.ActionBorders(),
"symlink.border-background": gum.ActionColors(),
"symlink.border-foreground": gum.ActionColors(),
"symlink.foreground": gum.ActionColors(),
})
common.AddFlagCompletion(fileCmd)

carapace.Gen(fileCmd).PositionalCompletion(
carapace.ActionDirectories(),
Expand Down
59 changes: 2 additions & 57 deletions completers/gum_completer/cmd/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gum"
"github.com/carapace-sh/carapace-bin/completers/gum_completer/cmd/common"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -175,60 +175,5 @@ func init() {
filterCmd.Flags().String("width", "", "Input width")
rootCmd.AddCommand(filterCmd)

carapace.Gen(filterCmd).FlagCompletion(carapace.ActionMap{
"cursor-text.align": gum.ActionAlignments(),
"cursor-text.background": gum.ActionColors(),
"cursor-text.border": gum.ActionBorders(),
"cursor-text.border-background": gum.ActionColors(),
"cursor-text.border-foreground": gum.ActionColors(),
"cursor-text.foreground": gum.ActionColors(),
"header.align": gum.ActionAlignments(),
"header.background": gum.ActionColors(),
"header.border": gum.ActionBorders(),
"header.border-background": gum.ActionColors(),
"header.border-foreground": gum.ActionColors(),
"header.foreground": gum.ActionColors(),
"indicator.align": gum.ActionAlignments(),
"indicator.background": gum.ActionColors(),
"indicator.border": gum.ActionBorders(),
"indicator.border-background": gum.ActionColors(),
"indicator.border-foreground": gum.ActionColors(),
"indicator.foreground": gum.ActionColors(),
"match.align": gum.ActionAlignments(),
"match.background": gum.ActionColors(),
"match.border": gum.ActionBorders(),
"match.border-background": gum.ActionColors(),
"match.border-foreground": gum.ActionColors(),
"match.foreground": gum.ActionColors(),
"placeholder.align": gum.ActionAlignments(),
"placeholder.background": gum.ActionColors(),
"placeholder.border": gum.ActionBorders(),
"placeholder.border-background": gum.ActionColors(),
"placeholder.border-foreground": gum.ActionColors(),
"placeholder.foreground": gum.ActionColors(),
"prompt.align": gum.ActionAlignments(),
"prompt.background": gum.ActionColors(),
"prompt.border": gum.ActionBorders(),
"prompt.border-background": gum.ActionColors(),
"prompt.border-foreground": gum.ActionColors(),
"prompt.foreground": gum.ActionColors(),
"selected-indicator.align": gum.ActionAlignments(),
"selected-indicator.background": gum.ActionColors(),
"selected-indicator.border": gum.ActionBorders(),
"selected-indicator.border-background": gum.ActionColors(),
"selected-indicator.border-foreground": gum.ActionColors(),
"selected-indicator.foreground": gum.ActionColors(),
"text.align": gum.ActionAlignments(),
"text.background": gum.ActionColors(),
"text.border": gum.ActionBorders(),
"text.border-background": gum.ActionColors(),
"text.border-foreground": gum.ActionColors(),
"text.foreground": gum.ActionColors(),
"unselected-prefix.align": gum.ActionAlignments(),
"unselected-prefix.background": gum.ActionColors(),
"unselected-prefix.border": gum.ActionBorders(),
"unselected-prefix.border-background": gum.ActionColors(),
"unselected-prefix.border-foreground": gum.ActionColors(),
"unselected-prefix.foreground": gum.ActionColors(),
})
common.AddFlagCompletion(filterCmd)
}
28 changes: 3 additions & 25 deletions completers/gum_completer/cmd/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/gum_completer/cmd/common"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gum"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -88,31 +89,8 @@ func init() {
inputCmd.Flags().String("width", "", "Input width (0 for terminal width)")
rootCmd.AddCommand(inputCmd)

common.AddFlagCompletion(inputCmd)
carapace.Gen(inputCmd).FlagCompletion(carapace.ActionMap{
"cursor.align": gum.ActionAlignments(),
"cursor.background": gum.ActionColors(),
"cursor.border": gum.ActionBorders(),
"cursor.border-background": gum.ActionColors(),
"cursor.border-foreground": gum.ActionColors(),
"cursor.foreground": gum.ActionColors(),
"cursor.mode": gum.ActionCursorModes(),
"header.align": gum.ActionAlignments(),
"header.background": gum.ActionColors(),
"header.border": gum.ActionBorders(),
"header.border-background": gum.ActionColors(),
"header.border-foreground": gum.ActionColors(),
"header.foreground": gum.ActionColors(),
"placeholder.align": gum.ActionAlignments(),
"placeholder.background": gum.ActionColors(),
"placeholder.border": gum.ActionBorders(),
"placeholder.border-background": gum.ActionColors(),
"placeholder.border-foreground": gum.ActionColors(),
"placeholder.foreground": gum.ActionColors(),
"prompt.align": gum.ActionAlignments(),
"prompt.background": gum.ActionColors(),
"prompt.border": gum.ActionBorders(),
"prompt.border-background": gum.ActionColors(),
"prompt.border-foreground": gum.ActionColors(),
"prompt.foreground": gum.ActionColors(),
"cursor.mode": gum.ActionCursorModes(),
})
}
6 changes: 2 additions & 4 deletions completers/gum_completer/cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gum"
"github.com/carapace-sh/carapace-bin/completers/gum_completer/cmd/common"
"github.com/spf13/cobra"
)

Expand All @@ -20,7 +20,5 @@ func init() {
joinCmd.Flags().Bool("vertical", false, "Join (potentially multi-line) strings vertically")
rootCmd.AddCommand(joinCmd)

carapace.Gen(joinCmd).FlagCompletion(carapace.ActionMap{
"align": gum.ActionAlignments(),
})
common.AddFlagCompletion(joinCmd)
}
51 changes: 5 additions & 46 deletions completers/gum_completer/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"

"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gum"
"github.com/carapace-sh/carapace-bin/completers/gum_completer/cmd/common"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -133,40 +133,11 @@ func init() {
logCmd.Flags().String("value.width", "", "Text width")
rootCmd.AddCommand(logCmd)

common.AddFlagCompletion(logCmd)
carapace.Gen(logCmd).FlagCompletion(carapace.ActionMap{
"file": carapace.ActionFiles(),
"formatter": carapace.ActionValues("json", "logfmt", "text"),
"key.align": gum.ActionAlignments(),
"key.background": gum.ActionColors(),
"key.border": gum.ActionBorders(),
"key.border-background": gum.ActionColors(),
"key.border-foreground": gum.ActionColors(),
"key.foreground": gum.ActionColors(),
"level": carapace.ActionValues("none", "debug", "info", "warn", "error", "fatal").StyleF(style.ForLogLevel),
"level.align": gum.ActionAlignments(),
"level.background": gum.ActionColors(),
"level.border": gum.ActionBorders(),
"level.border-background": gum.ActionColors(),
"level.border-foreground": gum.ActionColors(),
"level.foreground": gum.ActionColors(),
"message.align": gum.ActionAlignments(),
"message.background": gum.ActionColors(),
"message.border": gum.ActionBorders(),
"message.border-background": gum.ActionColors(),
"message.border-foreground": gum.ActionColors(),
"message.foreground": gum.ActionColors(),
"prefix.align": gum.ActionAlignments(),
"prefix.background": gum.ActionColors(),
"prefix.border": gum.ActionBorders(),
"prefix.border-background": gum.ActionColors(),
"prefix.border-foreground": gum.ActionColors(),
"prefix.foreground": gum.ActionColors(),
"separator.align": gum.ActionAlignments(),
"separator.background": gum.ActionColors(),
"separator.border": gum.ActionBorders(),
"separator.border-background": gum.ActionColors(),
"separator.border-foreground": gum.ActionColors(),
"separator.foreground": gum.ActionColors(),
"file": carapace.ActionFiles(),
"formatter": carapace.ActionValues("json", "logfmt", "text"),
"level": carapace.ActionValues("none", "debug", "info", "warn", "error", "fatal").StyleF(style.ForLogLevel),
"time": carapace.ActionValuesDescribed(
"layout", time.Layout,
"ansic", time.ANSIC,
Expand All @@ -187,17 +158,5 @@ func init() {
"datetime", "2006-01-02 15:04:05",
"dateonly", "2006-01-02",
"timeonly", "15:04:05"),
"time.align": gum.ActionAlignments(),
"time.background": gum.ActionColors(),
"time.border": gum.ActionBorders(),
"time.border-background": gum.ActionColors(),
"time.border-foreground": gum.ActionColors(),
"time.foreground": gum.ActionColors(),
"value.align": gum.ActionAlignments(),
"value.background": gum.ActionColors(),
"value.border": gum.ActionBorders(),
"value.border-background": gum.ActionColors(),
"value.border-foreground": gum.ActionColors(),
"value.foreground": gum.ActionColors(),
})
}
Loading

0 comments on commit fa17247

Please sign in to comment.