-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglippy.go
More file actions
81 lines (69 loc) · 1.67 KB
/
Copy pathglippy.go
File metadata and controls
81 lines (69 loc) · 1.67 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
package glippy
import (
"context"
"sync"
"time"
)
const baseWatchInterval = time.Second * 1
// Clipboard method identifiers returned by SetWithMethod.
const (
// MethodNative indicates the OS clipboard was used (X11, Wayland, pbcopy,
// Windows clipboard API).
MethodNative = "native"
// MethodOSC52 indicates the OSC 52 escape-sequence fallback was used.
// OSC 52 is fire-and-forget: success means the sequence was written to the
// terminal, not that the terminal honored it. Many terminal emulators
// disable OSC 52 writes by default.
MethodOSC52 = "osc52"
)
var once sync.Once
func startOnce() {
once.Do(func() {
start()
})
}
// Set sets clipboard content.
func Set(text string) error {
_, err := SetWithMethod(text)
return err
}
// SetWithMethod sets clipboard content and reports which mechanism was used.
// See the Method* constants for possible values.
func SetWithMethod(text string) (method string, err error) {
startOnce()
return set(text)
}
// Get get clipboard content
func Get() (string, error) {
startOnce()
return get()
}
// WatchWithInterval watching clipboard content at a specified interval
func WatchWithInterval(ctx context.Context, interval time.Duration) <-chan string {
recv := make(chan string, 1)
go func() {
ticker := time.NewTicker(interval)
lastData := ""
for {
select {
case <-ctx.Done():
close(recv)
return
case <-ticker.C:
data, err := Get()
if err != nil {
continue
}
if data != lastData {
recv <- data
lastData = data
}
}
}
}()
return recv
}
// Watch watching clipboard content
func Watch(ctx context.Context) <-chan string {
return WatchWithInterval(ctx, baseWatchInterval)
}