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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! use_clipboard hook for clipboard operations
//!
//! Provides clipboard read/write functionality using system commands.
//!
//! # Example
//!
//! ```rust,ignore
//! use rnk::prelude::*;
//!
//! fn app() -> Element {
//! let clipboard = use_clipboard();
//!
//! use_input(move |input, key| {
//! if key.ctrl && input == "c" {
//! clipboard.write("Copied text!");
//! } else if key.ctrl && input == "v" {
//! if let Some(text) = clipboard.read() {
//! println!("Pasted: {}", text);
//! }
//! }
//! });
//!
//! // ...
//! }
//! ```
use std::process::Command;
/// Handle for clipboard operations
#[derive(Clone, Copy)]
pub struct ClipboardHandle;
impl ClipboardHandle {
/// Read text from clipboard
pub fn read(&self) -> Option<String> {
read_clipboard()
}
/// Write text to clipboard
pub fn write(&self, text: &str) -> bool {
write_clipboard(text)
}
/// Check if clipboard is available
pub fn is_available(&self) -> bool {
is_clipboard_available()
}
/// Clear the clipboard
pub fn clear(&self) -> bool {
write_clipboard("")
}
}
/// Create a clipboard handle
pub fn use_clipboard() -> ClipboardHandle {
ClipboardHandle
}
/// Read text from system clipboard
pub fn read_clipboard() -> Option<String> {
#[cfg(target_os = "macos")]
{
Command::new("pbpaste").output().ok().and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
})
}
#[cfg(target_os = "linux")]
{
// Try xclip first, then xsel
Command::new("xclip")
.args(["-selection", "clipboard", "-o"])
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
})
.or_else(|| {
Command::new("xsel")
.args(["--clipboard", "--output"])
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
})
})
}
#[cfg(target_os = "windows")]
{
Command::new("powershell")
.args(["-command", "Get-Clipboard"])
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout)
.ok()
.map(|s| s.trim().to_string())
} else {
None
}
})
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
None
}
}
/// Write text to system clipboard
pub fn write_clipboard(text: &str) -> bool {
#[cfg(target_os = "macos")]
{
use std::io::Write;
Command::new("pbcopy")
.stdin(std::process::Stdio::piped())
.spawn()
.and_then(|mut child| {
if let Some(stdin) = child.stdin.as_mut() {
stdin.write_all(text.as_bytes())?;
}
child.wait()
})
.map(|status| status.success())
.unwrap_or(false)
}
#[cfg(target_os = "linux")]
{
use std::io::Write;
Command::new("xclip")
.args(["-selection", "clipboard"])
.stdin(std::process::Stdio::piped())
.spawn()
.and_then(|mut child| {
if let Some(stdin) = child.stdin.as_mut() {
stdin.write_all(text.as_bytes())?;
}
child.wait()
})
.map(|status| status.success())
.unwrap_or(false)
}
#[cfg(target_os = "windows")]
{
let escaped = text.replace("\"", "`\"");
Command::new("powershell")
.args(["-command", &format!("Set-Clipboard -Value \"{}\"", escaped)])
.status()
.map(|status| status.success())
.unwrap_or(false)
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
let _ = text;
false
}
}
/// Check if clipboard is available on this system
pub fn is_clipboard_available() -> bool {
#[cfg(target_os = "macos")]
{
Command::new("which")
.arg("pbcopy")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
#[cfg(target_os = "linux")]
{
Command::new("which")
.arg("xclip")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
|| Command::new("which")
.arg("xsel")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
#[cfg(target_os = "windows")]
{
true // PowerShell is always available on Windows
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
false
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_clipboard_handle() {
let clipboard = use_clipboard();
let _ = clipboard.is_available();
}
#[test]
fn test_is_clipboard_available() {
// Just check it doesn't panic
let _ = is_clipboard_available();
}
}