mirror of
https://github.com/skidoodle/ctx.git
synced 2026-04-28 03:07:41 +02:00
Add cross-platform clipboard support
Implement CopyFile for darwin, linux, and windows and call it from main to copy generated output to the clipboard. Enable CGO for builds, update goreleaser and release workflow to use macOS, and add a cross-platform test workflow that verifies the clipboard.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
//go:build linux
|
||||
|
||||
package clipboard
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CopyFile(path string) error {
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("file://%s", absPath)
|
||||
|
||||
if isCommandAvailable("wl-copy") {
|
||||
cmd := exec.Command("wl-copy", "--type", "text/uri-list")
|
||||
cmd.Stdin = strings.NewReader(uri)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
if isCommandAvailable("xclip") {
|
||||
cmd := exec.Command("xclip", "-selection", "clipboard", "-t", "text/uri-list")
|
||||
cmd.Stdin = strings.NewReader(uri)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
return fmt.Errorf("install 'wl-copy' or 'xclip'")
|
||||
}
|
||||
|
||||
func isCommandAvailable(name string) bool {
|
||||
_, err := exec.LookPath(name)
|
||||
return err == nil
|
||||
}
|
||||
Reference in New Issue
Block a user