-
Notifications
You must be signed in to change notification settings - Fork 1
/
vlipboard.v
144 lines (138 loc) · 2.72 KB
/
vlipboard.v
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
module vlipboard
import clipboard
import os
struct Vlipboard {
mut:
method int
clip &clipboard.Clipboard
}
// new creates a new Vlipboard instance
pub fn new() ?&Vlipboard {
// Clipboard.copy() wont work linux unless it's primary
mut clip := if os.user_os() == 'linux' { clipboard.new_primary() } else { clipboard.new() }
if os.getenv('WAYLAND_DISPLAY') != '' {
if !(os.exists_in_system_path('wl-copy') && os.exists_in_system_path('wl-paste')) {
return error('Clipboard wont work on Wayland unless you install wl-clipboard')
}
return &Vlipboard{
method: 1
clip: clip
}
} else if os.exists_in_system_path('termux-clipboard-set') && os.exists_in_system_path('termux-clipboard-get') {
return &Vlipboard{
method: 2
clip: clip
}
} else if os.exists('/dev/snarf') {
// Plan 9 from Bell Labs
return &Vlipboard{
method: 3
clip: clip
}
} else {
// if default clipboard library isn't available fall back to xclip or xsel
if clip.is_available() != true {
if os.exists_in_system_path('xclip') {
return &Vlipboard{
method: 4
clip: clip
}
} else if os.exists_in_system_path('xsel') {
return &Vlipboard{
method: 5
clip: clip
}
} else {
return error('Clipboard is not supported')
}
}
return &Vlipboard{
clip: clip
method: 0
}
}
}
// copy text into the clipboard
pub fn (mut vb Vlipboard) copy(text string) bool {
match vb.method {
0 {
return vb.clip.copy(text)
}
1 {
return os.system('wl-copy $text') == 0
}
2 {
return os.system('termux-clipboard-set $text') == 0
}
3 {
mut file := os.open('/dev/snarf') or {
return false
}
file.write(text)
return true
}
4 {
return os.system('xclip -in -selection clipboard <<< "$text"') == 0
}
5 {
return os.system('xsel --input --clipboard <<< "$text"') == 0
}
else {
return false
}
}
}
// paste returns the content of the clipboard
pub fn (mut vb Vlipboard) paste() string {
match vb.method {
0 {
return vb.clip.paste()
}
1 {
result := os.exec('wl-paste --no-newline') or {
return ''
}
return result.output
}
2 {
result := os.exec('termux-clipboard-get') or {
return ''
}
return result.output
}
3 {
result := os.read_file('/dev/snarf') or {
return ''
}
return result
}
4 {
result := os.exec('xclip -out -selection clipboard') or {
return ''
}
return result.output
}
5 {
result := os.exec('xsel --output --clipboard') or {
return ''
}
return result.output
}
else {
return ''
}
}
}
// clear the content of the clipboard
pub fn (mut vb Vlipboard) clear() bool {
match vb.method {
0 {
vb.clip.clear_all()
return true
}
else {
vb.copy('')
return true
}
}
}