Skip to content

Commit

Permalink
feat: add clip command (#191)
Browse files Browse the repository at this point in the history
copy selected command to clipboard
  • Loading branch information
helmecke authored May 8, 2023
1 parent 9b3e0fa commit 0104f05
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cmd/clip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"fmt"
"strings"

"github.com/atotto/clipboard"
"github.com/fatih/color"
"github.com/knqyf263/pet/config"
"github.com/spf13/cobra"
)

// clipCmd represents the clip command
var clipCmd = &cobra.Command{
Use: "clip",
Short: "Copy the selected commands",
Long: `Copy the selected commands to clipboard`,
RunE: clip,
}

func clip(cmd *cobra.Command, args []string) (err error) {
flag := config.Flag

var options []string
if flag.Query != "" {
options = append(options, fmt.Sprintf("--query %s", flag.Query))
}

commands, err := filter(options, flag.FilterTag)
if err != nil {
return err
}
command := strings.Join(commands, flag.Delimiter)
if flag.Command && command != "" {
fmt.Printf("%s: %s\n", color.YellowString("Command"), command)
}
return clipboard.WriteAll(command)
}

func init() {
RootCmd.AddCommand(clipCmd)
clipCmd.Flags().StringVarP(&config.Flag.Query, "query", "q", "",
`Initial value for query`)
clipCmd.Flags().BoolVarP(&config.Flag.Command, "command", "", false,
`Display snippets in one line`)
clipCmd.Flags().StringVarP(&config.Flag.Delimiter, "delimiter", "d", "; ",
`Use delim as the command delimiter character`)
clipCmd.Flags().StringVarP(&config.Flag.FilterTag, "tag", "t", "",
`Filter tag`)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61
)

require github.com/atotto/clipboard v0.1.4

require (
github.com/alessio/shellescape v1.4.1 // indirect
github.com/golang/protobuf v1.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/BurntSushi/toml v0.3.0 h1:e1/Ivsx3Z0FVTV0NSOv/aVgbUWyQuzj7DDnFblkRvsY
github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/briandowns/spinner v0.0.0-20170614154858-48dbb65d7bd5 h1:osZyZB7J4kE1tKLeaUjV6+uZVBfS835T0I/RxmwWw1w=
github.com/briandowns/spinner v0.0.0-20170614154858-48dbb65d7bd5/go.mod h1:hw/JEQBIE+c/BLI4aKM8UU8v+ZqrD3h7HC27kKt8JQU=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
Expand Down

0 comments on commit 0104f05

Please sign in to comment.