-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclipboard.go
More file actions
102 lines (89 loc) · 2.51 KB
/
Copy pathclipboard.go
File metadata and controls
102 lines (89 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Package nativeclipboard provides cross-platform clipboard access using purego
// instead of cgo. It supports text and image data on macOS, Linux, and Windows.
//
// The package initializes automatically and returns errors from individual operations.
//
// Read and write clipboard data:
//
// // Write text to clipboard
// changed, err := nativeclipboard.Text.Write([]byte("hello world"))
// if err != nil {
// log.Fatal(err)
// }
//
// // Read text from clipboard
// data, err := nativeclipboard.Text.Read()
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(string(data))
//
// The package supports text (UTF-8) and images (PNG).
package nativeclipboard
import (
"context"
"errors"
"sync"
)
var (
// ErrUnavailable indicates the clipboard is not available
ErrUnavailable = errors.New("clipboard unavailable")
// ErrUnsupported indicates the requested format is not supported
ErrUnsupported = errors.New("unsupported format")
)
// Format represents a clipboard data format.
type Format int
// Supported clipboard formats
const (
// Text provides access to text clipboard operations.
Text Format = iota
// Image provides access to image (PNG) clipboard operations.
Image
)
var (
// Due to platform limitations, concurrent reads can cause issues.
// Use a global lock to guarantee one operation at a time.
lock = sync.Mutex{}
initError error
)
func init() {
initError = initialize()
}
// Read reads clipboard data in this format.
// Returns an error if the clipboard is unavailable or initialization failed.
func (f Format) Read() ([]byte, error) {
if initError != nil {
return nil, initError
}
lock.Lock()
defer lock.Unlock()
buf, err := read(f)
if err != nil {
return nil, err
}
return buf, nil
}
// Write writes data to the clipboard in this format.
// Returns a channel that receives a signal when the clipboard content
// has been overwritten by another application, and an error if the operation fails.
func (f Format) Write(buf []byte) (<-chan struct{}, error) {
if initError != nil {
return nil, initError
}
lock.Lock()
defer lock.Unlock()
changed, err := write(f, buf)
if err != nil {
return nil, err
}
return changed, nil
}
// Watch returns a channel that receives clipboard data whenever it changes.
// The channel will be closed when the provided context is canceled.
// Returns an error if the clipboard is unavailable or initialization failed.
func (f Format) Watch(ctx context.Context) (<-chan []byte, error) {
if initError != nil {
return nil, initError
}
return watch(ctx, f), nil
}