mirror of
https://github.com/skidoodle/ctx.git
synced 2026-04-28 11:17:42 +02:00
af9e0a5372
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.
44 lines
923 B
Go
44 lines
923 B
Go
//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
|
|
}
|