-
Notifications
You must be signed in to change notification settings - Fork 0
/
glippy.go
62 lines (52 loc) · 997 Bytes
/
glippy.go
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
package glippy
import (
"context"
"sync"
"time"
)
const baseWatchInterval = time.Second * 1
var once sync.Once
func startOnce() {
once.Do(func() {
start()
})
}
// Set set clipboard content
func Set(text string) 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)
}