From 4cced3e2cac752adbfb527b27a826f79c75987c0 Mon Sep 17 00:00:00 2001 From: rsteube Date: Mon, 1 Apr 2024 10:39:20 +0200 Subject: [PATCH] xclip: use flag functions for long shorthand - shared action --- completers/xclip_completer/cmd/root.go | 45 +++++++++---------- .../actions/tools/xclip}/target.go | 6 +-- 2 files changed, 23 insertions(+), 28 deletions(-) rename {completers/xclip_completer/cmd/action => pkg/actions/tools/xclip}/target.go (76%) diff --git a/completers/xclip_completer/cmd/root.go b/completers/xclip_completer/cmd/root.go index acd52af872..c1ebf24122 100644 --- a/completers/xclip_completer/cmd/root.go +++ b/completers/xclip_completer/cmd/root.go @@ -2,7 +2,7 @@ package cmd import ( "github.com/carapace-sh/carapace" - "github.com/carapace-sh/carapace-bin/completers/xclip_completer/cmd/action" + "github.com/carapace-sh/carapace-bin/pkg/actions/tools/xclip" "github.com/spf13/cobra" ) @@ -20,39 +20,34 @@ func Execute() error { func init() { carapace.Gen(rootCmd).Standalone() - rootCmd.Flags().StringS("display", "d", "", "X display to use") - rootCmd.Flags().StringS("display-long", "display", "", "X display to use") - rootCmd.Flags().BoolS("filter", "f", false, "Print the text piped to standard in back to standard out") - rootCmd.Flags().BoolS("filter-long", "filter", false, "Print the text piped to standard in back to standard out") - rootCmd.Flags().BoolP("help", "h", false, "Show quick summary of options") - rootCmd.Flags().BoolS("input", "i", true, "Read text into X selection") - rootCmd.Flags().BoolS("input-long", "in", true, "Read text into X selection") - rootCmd.Flags().IntS("loops", "l", 0, "Number of X selection requests to wait before exiting") - rootCmd.Flags().IntS("loops-long", "loops", 0, "Number of X selection requests to wait before exiting") + rootCmd.Flags().StringN("display", "d", "", "X display to use") + rootCmd.Flags().BoolN("filter", "f", false, "Print the text piped to standard in back to standard out") + rootCmd.Flags().BoolN("help", "h", false, "Show quick summary of options") + rootCmd.Flags().BoolN("in", "i", true, "Read text into X selection") + rootCmd.Flags().IntN("loops", "l", 0, "Number of X selection requests to wait before exiting") rootCmd.Flags().BoolS("noutf8", "noutf8", false, "Operate in legacy mode") - rootCmd.Flags().BoolS("output", "o", true, "Print the X selection to standard out") - rootCmd.Flags().BoolS("output-long", "out", true, "Print the X selection to standard out") + rootCmd.Flags().BoolN("out", "o", true, "Print the X selection to standard out") rootCmd.Flags().BoolS("quiet", "quiet", false, "Run in the foreground, output information messages") - rootCmd.Flags().BoolS("rmlast", "r", false, "Remove any trailing newlines from the selection") - rootCmd.Flags().BoolS("rmlast-long", "rmlastnl", false, "Remove any trailing newlines from the selection") + rootCmd.Flags().BoolN("rmlastln", "r", false, "Remove any trailing newlines from the selection") rootCmd.Flags().StringS("selection", "selection", "primary", "Specify which X selection to use") rootCmd.Flags().BoolS("silent", "silent", true, "Fork into background to wait, output errors only") - rootCmd.Flags().StringS("target", "t", "TEXT", "Specify a specific data format with given target atom") - rootCmd.Flags().StringS("target-long", "target", "TEXT", "Specify a specific data format with given target atom") + rootCmd.Flags().StringN("target", "t", "TEXT", "Specify a specific data format with given target atom") rootCmd.Flags().BoolS("verbose", "verbose", false, "Verbose output") - rootCmd.Flags().BoolP("version", "version", false, "Show version information") + rootCmd.Flags().BoolS("version", "version", false, "Show version information") - rootCmd.MarkFlagsMutuallyExclusive("display", "display-long") - rootCmd.MarkFlagsMutuallyExclusive("input", "input-long", "output", "output-long") - rootCmd.MarkFlagsMutuallyExclusive("filter", "filter-long", "output", "output-long") - rootCmd.MarkFlagsMutuallyExclusive("loops", "loops-long") + rootCmd.MarkFlagsMutuallyExclusive("in", "out") + rootCmd.MarkFlagsMutuallyExclusive("filter", "out") rootCmd.MarkFlagsMutuallyExclusive("quiet", "silent", "verbose") - rootCmd.MarkFlagsMutuallyExclusive("rmlast", "rmlast-long") - rootCmd.MarkFlagsMutuallyExclusive("target", "target-long") carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{ "selection": carapace.ActionValues("primary", "secondary", "clipboard"), - "target": xclip.ActionTarget(), + "target": carapace.Batch( + xclip.ActionTargets(), + carapace.ActionValues("TARGETS"), + ).ToA(), }) - carapace.Gen(rootCmd).PositionalCompletion(carapace.ActionFiles()) + + carapace.Gen(rootCmd).PositionalCompletion( + carapace.ActionFiles(), + ) } diff --git a/completers/xclip_completer/cmd/action/target.go b/pkg/actions/tools/xclip/target.go similarity index 76% rename from completers/xclip_completer/cmd/action/target.go rename to pkg/actions/tools/xclip/target.go index 295997e790..caa97d7752 100644 --- a/completers/xclip_completer/cmd/action/target.go +++ b/pkg/actions/tools/xclip/target.go @@ -6,11 +6,11 @@ import ( "github.com/carapace-sh/carapace" ) -// ActionTarget completes selection targets -func ActionTarget() carapace.Action { +// ActionTargets completes selection targets +func ActionTargets() carapace.Action { return carapace.ActionExecCommand("xclip", "-t", "TARGETS", "-o")(func(output []byte) carapace.Action { lines := strings.Split(string(output), "\n") - values := []string{"TARGETS"} + values := make([]string, 0) for _, line := range lines { if line != "" { values = append(values, line)