This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
/
clipboard_utils.go
55 lines (45 loc) · 1.83 KB
/
clipboard_utils.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
package utils
import (
"github.com/atotto/clipboard"
)
// ClipboardManager is an interface for actions with the clipboard
type ClipboardManager interface {
GetFromClipboard() (string, error)
GetMostRecentlyAddedURL() string
SetMostRecentlyAddedURL(url string)
CopyToClipboard(text string) error
}
// ClipboardManagerImpl is an implementation of ClipboardManager that uses atotto's clipboard library
type ClipboardManagerImpl struct {
AddedURL string
}
// GetFromClipboard gets the clipboard text using atotto's clipboard library
func (c *ClipboardManagerImpl) GetFromClipboard() (string, error) {
return clipboard.ReadAll()
}
// GetMostRecentlyAddedURL gets the URL that was most recently added
func (c *ClipboardManagerImpl) GetMostRecentlyAddedURL() string {
return c.AddedURL
}
// SetMostRecentlyAddedURL sets the URL that was most recently added
func (c *ClipboardManagerImpl) SetMostRecentlyAddedURL(url string) {
c.AddedURL = url
}
// CopyToClipboard writes supplied text to the clipboard using atotto's clipboard library
func (c *ClipboardManagerImpl) CopyToClipboard(text string) error {
return clipboard.WriteAll(text)
}
// IsURLInClipboard checks to see if the clipboard text is actually a URL
func IsURLInClipboard(clipboardManager ClipboardManager) bool {
result, err := clipboardManager.GetFromClipboard()
return err == nil && IsURL(result)
}
// CheckURLMostRecentlyAdded checks to see if the text in clipboard is the same as the URL that was just added
func CheckURLMostRecentlyAdded(clipboardManager ClipboardManager) bool {
result, err := clipboardManager.GetFromClipboard()
return err == nil && result == clipboardManager.GetMostRecentlyAddedURL()
}
// GetFromClipboard gets clipboard text from the clipboard manager
func GetFromClipboard(clipboardManager ClipboardManager) (string, error) {
return clipboardManager.GetFromClipboard()
}