Files
Summerize/internal/output/clipboard.go
2026-05-10 13:37:17 -06:00

31 lines
794 B
Go

package output
import (
"fmt"
"os/exec"
"strings"
)
// CopyToClipboard tries platform-appropriate clipboard tools and writes data
// to the first one available: wl-copy (Wayland), xclip (X11), pbcopy (macOS).
// Returns the tool name used or an error if none are available.
func CopyToClipboard(data string) (string, error) {
candidates := [][]string{
{"wl-copy"},
{"xclip", "-selection", "clipboard"},
{"pbcopy"},
}
for _, c := range candidates {
if _, err := exec.LookPath(c[0]); err != nil {
continue
}
cmd := exec.Command(c[0], c[1:]...)
cmd.Stdin = strings.NewReader(data)
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("%s: %w", c[0], err)
}
return c[0], nil
}
return "", fmt.Errorf("no clipboard tool found (tried wl-copy, xclip, pbcopy)")
}