|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/atotto/clipboard" |
| 8 | + "github.com/fatih/color" |
| 9 | + "github.com/knqyf263/pet/config" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +// clipCmd represents the clip command |
| 14 | +var clipCmd = &cobra.Command{ |
| 15 | + Use: "clip", |
| 16 | + Short: "Copy the selected commands", |
| 17 | + Long: `Copy the selected commands to clipboard`, |
| 18 | + RunE: clip, |
| 19 | +} |
| 20 | + |
| 21 | +func clip(cmd *cobra.Command, args []string) (err error) { |
| 22 | + flag := config.Flag |
| 23 | + |
| 24 | + var options []string |
| 25 | + if flag.Query != "" { |
| 26 | + options = append(options, fmt.Sprintf("--query %s", flag.Query)) |
| 27 | + } |
| 28 | + |
| 29 | + commands, err := filter(options, flag.FilterTag) |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + command := strings.Join(commands, flag.Delimiter) |
| 34 | + if flag.Command && command != "" { |
| 35 | + fmt.Printf("%s: %s\n", color.YellowString("Command"), command) |
| 36 | + } |
| 37 | + return clipboard.WriteAll(command) |
| 38 | +} |
| 39 | + |
| 40 | +func init() { |
| 41 | + RootCmd.AddCommand(clipCmd) |
| 42 | + clipCmd.Flags().StringVarP(&config.Flag.Query, "query", "q", "", |
| 43 | + `Initial value for query`) |
| 44 | + clipCmd.Flags().BoolVarP(&config.Flag.Command, "command", "", false, |
| 45 | + `Display snippets in one line`) |
| 46 | + clipCmd.Flags().StringVarP(&config.Flag.Delimiter, "delimiter", "d", "; ", |
| 47 | + `Use delim as the command delimiter character`) |
| 48 | + clipCmd.Flags().StringVarP(&config.Flag.FilterTag, "tag", "t", "", |
| 49 | + `Filter tag`) |
| 50 | +} |
0 commit comments