Initial push to gitea
This commit is contained in:
30
internal/output/clipboard.go
Normal file
30
internal/output/clipboard.go
Normal file
@@ -0,0 +1,30 @@
|
||||
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)")
|
||||
}
|
||||
Reference in New Issue
Block a user