From 7073b3307ce08132292705416120bb22fe6df08c Mon Sep 17 00:00:00 2001 From: Vincent Landgraf Date: Mon, 30 Sep 2024 17:45:03 +0200 Subject: [PATCH] implement clipboard using xclip to support openbsd --- pkg/clipboard/clipboard_openbsd.go | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 pkg/clipboard/clipboard_openbsd.go diff --git a/pkg/clipboard/clipboard_openbsd.go b/pkg/clipboard/clipboard_openbsd.go new file mode 100644 index 0000000..a8c8af8 --- /dev/null +++ b/pkg/clipboard/clipboard_openbsd.go @@ -0,0 +1,50 @@ +package clipboard + +import ( + "fmt" + "os" + "os/exec" +) + +// writes usinng the xclip command, might also +// work on freebsd and netbsd +func writeAll(text string) error { + path, err := exec.LookPath("xclip") + if err != nil { + return fmt.Errorf("failed to find xclip: %w", err) + } + + r, w, err := os.Pipe() + if err != nil { + return fmt.Errorf("failed to create xclip pipe: %w", err) + } + var perr error + go func() { + _, err := w.WriteString(text) + if err != nil { + perr = fmt.Errorf("failed to write to xclip: %w", err) + } + w.Close() // ignore err + }() + + c := exec.Cmd{ + Path: path, + Args: []string{ + "-i", + "-selection", + "clipboard", + }, + Stdin: r, + Stdout: nil, + Stderr: nil, + } + err = c.Run() + if err != nil { + return fmt.Errorf("failed to run xclip: %w", err) + } + if perr != nil { + return fmt.Errorf("failed to write to xclip: %w", err) + } + + return nil +}