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:
2026-01-31 01:53:12 +01:00
parent fd5c8d72f5
commit af9e0a5372
7 changed files with 248 additions and 5 deletions
+43
View File
@@ -0,0 +1,43 @@
//go:build darwin
package clipboard
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include <stdlib.h>
#import <Cocoa/Cocoa.h>
int copyFileToPasteboard(char* path) {
@autoreleasepool {
NSString *strPath = [NSString stringWithUTF8String:path];
if (!strPath) return 0;
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb clearContents];
[pb declareTypes:@[NSFilenamesPboardType] owner:nil];
return [pb setPropertyList:@[strPath] forType:NSFilenamesPboardType] ? 1 : 0;
}
}
*/
import "C"
import (
"errors"
"path/filepath"
"unsafe"
)
func CopyFile(path string) error {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
cPath := C.CString(absPath)
defer C.free(unsafe.Pointer(cPath))
if success := C.copyFileToPasteboard(cPath); success == 0 {
return errors.New("failed to write to macOS pasteboard")
}
return nil
}