Skip to content

Commit

Permalink
feature: allow copying from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhop committed May 7, 2019
1 parent 9010c40 commit 4c1d3b9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Release archive creation:
- [X] figure out how to report version/commit info that I'm bothering to embed in the build
- [X] fix reading environment var for EDITOR so it actually overrides config defaults
- [X] convert all manual filepath building to use `path.Join`
- [ ] add ability copy from stdin? would allow ability to pipe output from commands to `clip copy`, which may be a nice abstraction to having to remember OS-specific commands
- [X] add ability copy from stdin? would allow ability to pipe output from commands to `clip copy`, which may be a nice abstraction to having to remember OS-specific commands

## Credits/Thanks
Clip is written using the [Cobra](https://github.com/spf13/cobra) and [Viper](https://github.com/spf13/viper) libraries, with the clipboard management provided by [atotto/clipboard library](https://github.com/atotto/clipboard). They made my life a heck of a life easier, so thanks to them <3.
51 changes: 44 additions & 7 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"strings"
"path/filepath"
"bufio"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -36,14 +37,21 @@ import (
var copyCmd = &cobra.Command{
Use: "copy <Clip template>",
Aliases: []string{"load", "in"},
Short: "Copy a Clip template to your clipboard (default if just running `clip $arg`)",
Long: `Copy a Clip template to your clipboard`,
Args: cobra.ExactArgs(1),
Short: "Copy a Clip template/Stdin to your clipboard (default if just running `clip $arg`)",
Long: `Copy a Clip template or command output from Stdin to your clipboard`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateFilename := filepath.Join(viper.GetString("templatedir"), os.Args[len(os.Args) - 1] + ".yml")
err := writeClipTemplateToClipboard(templateFilename)
if err != nil {
fmt.Printf("Failed to copy Clip template '%s' to clipboard: %v\n", strings.TrimSuffix(filepath.Base(templateFilename), filepath.Ext(templateFilename)), err)
if len(args) == 0 {
err := writeStdinToClipboard()
if err != nil {
fmt.Printf("Failed to copy from stdin: %v\n", err)
}
} else if len(args) == 1 {
templateFilename := filepath.Join(viper.GetString("templatedir"), os.Args[len(os.Args) - 1] + ".yml")
err := writeClipTemplateToClipboard(templateFilename)
if err != nil {
fmt.Printf("Failed to copy Clip template '%s' to clipboard: %v\n", strings.TrimSuffix(filepath.Base(templateFilename), filepath.Ext(templateFilename)), err)
}
}
},
}
Expand All @@ -70,3 +78,32 @@ func writeClipTemplateToClipboard(filename string) error {

return nil
}

func writeStdinToClipboard() error {
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}

if info.Mode()&os.ModeCharDevice == os.ModeCharDevice || info.Size() <= 0 {
fmt.Println("Invalid input device for stdin")
} else {
scanner := bufio.NewScanner(os.Stdin)
var output []string

for scanner.Scan() {
output = append(output, scanner.Text())
}

if scanner.Err() != nil {
return fmt.Errorf("Failed to read data from stdin: %v\n", err)
}

err := clipboard.WriteAll(strings.Join(output, "\n"))
if err != nil {
return fmt.Errorf("Failed to write data from stdin to clipboard: %v\n", err)
}
}

return nil
}

0 comments on commit 4c1d3b9

Please sign in to comment.