diff --git a/cmd/copy.go b/cmd/copy.go index a7c202d..6aa5048 100644 --- a/cmd/copy.go +++ b/cmd/copy.go @@ -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() } diff --git a/cmd/copy_test.go b/cmd/copy_test.go index 413fb03..d77faf6 100644 --- a/cmd/copy_test.go +++ b/cmd/copy_test.go @@ -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"))