Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,48 @@ func init() {
func copy(content []byte) error {
switch runtime.GOOS {
case "darwin":
return pbcopy(content)
return pipeTo(content, "pbcopy")
case "linux":
return linuxCopy(content)
case "windows":
return pipeTo(content, "powershell", "-command", "Set-Clipboard")
default:
// only mac for now. help wanted for other platforms.
return fmt.Errorf("copy not supported on %s", runtime.GOOS)
}
}

func pbcopy(content []byte) error {
cmd := exec.Command("pbcopy")

in, err := cmd.StdinPipe()
if err != nil {
return err
func linuxCopy(content []byte) error {
if _, err := exec.LookPath("wl-copy"); err == nil {
return pipeTo(content, "wl-copy")
}
if _, err := exec.LookPath("xclip"); err == nil {
return pipeTo(content, "xclip", "-selection", "clipboard")
}
if _, err := exec.LookPath("xsel"); err == nil {
return pipeTo(content, "xsel", "-ib")
}
return fmt.Errorf("copy not supported on linux: install wl-clipboard, xclip, or xsel")
}

func pipeTo(content []byte, name string, args ...string) error {
cmd := exec.Command(name, args...)

err = cmd.Start()
in, err := cmd.StdinPipe()
if err != nil {
return err
}

_, err = in.Write(content)
if err != nil {
if err := cmd.Start(); err != nil {
return err
}

err = in.Close()
if err != nil {
if _, err := in.Write(content); err != nil {
return err
}

err = cmd.Wait()
if err != nil {
if err := in.Close(); err != nil {
return err
}

return nil
return cmd.Wait()
}
4 changes: 2 additions & 2 deletions cmd/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

func TestCopy_UnsupportedOS(t *testing.T) {
if runtime.GOOS == "darwin" {
t.Skip("test only exercises the non-darwin branch")
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" || runtime.GOOS == "windows" {
t.Skip("test only exercises the default/unhandled OS branch")
}

err := copy([]byte("hello"))
Expand Down
Loading