Skip to content

Commit f87a74e

Browse files
committed
Fix clipboard copying on Wayland
1 parent bd82d85 commit f87a74e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/utils.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use base64::{engine::general_purpose, Engine as _};
22
use copypasta_ext::prelude::*;
3+
use copypasta_ext::wayland_bin::WaylandBinClipboardContext;
34
use copypasta_ext::x11_bin::ClipboardContext as BinClipboardContext;
45
use copypasta_ext::x11_fork::ClipboardContext as ForkClipboardContext;
56
use crossterm::style::Print;
@@ -90,15 +91,8 @@ pub fn verified_password(message: &str, minimum_length: usize) -> String {
9091
}
9192
}
9293

93-
fn in_ssh_shell() -> bool {
94-
return !env::var("SSH_CONNECTION")
95-
.unwrap_or_default()
96-
.trim()
97-
.is_empty();
98-
}
99-
10094
pub fn copy_string_to_clipboard(content: String) -> Result<CopyType, ()> {
101-
if in_ssh_shell()
95+
if env_var_set("SSH_CONNECTION")
10296
&& crossterm::execute!(
10397
io::stdout(),
10498
Print(format!(
@@ -111,6 +105,12 @@ pub fn copy_string_to_clipboard(content: String) -> Result<CopyType, ()> {
111105
// We do not use copypasta_ext::osc52 module because we have enabled terminal raw mode, so we print with crossterm utilities
112106
// Check https://github.com/timvisee/rust-clipboard-ext/blob/371df19d2f961882a21c957f396d1e24548d1f28/src/osc52.rs#L92
113107
Ok(CopyType::OSC52)
108+
} else if env_var_set("WAYLAND_DISPLAY")
109+
&& WaylandBinClipboardContext::new()
110+
.and_then(|mut ctx| ctx.set_contents(content.clone()))
111+
.is_ok()
112+
{
113+
Ok(CopyType::Native)
114114
} else if BinClipboardContext::new()
115115
.and_then(|mut ctx| ctx.set_contents(content.clone()))
116116
.is_ok()
@@ -123,3 +123,9 @@ pub fn copy_string_to_clipboard(content: String) -> Result<CopyType, ()> {
123123
Err(())
124124
}
125125
}
126+
127+
fn env_var_set(env_var: &str) -> bool {
128+
env::var(env_var)
129+
.map(|v| !v.trim().is_empty())
130+
.unwrap_or(false)
131+
}

0 commit comments

Comments
 (0)