Skip to content

Commit ac7e8da

Browse files
author
Naseschwarz
committed
Move Wayland/X string copying out of copy_string
Copying logic seems too nested to comprehend with the introcution of two paths towards OSC52 otherwise.
1 parent 2541bf4 commit ac7e8da

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

src/clipboard.rs

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use anyhow::{anyhow, Result};
2-
use base64::prelude::{Engine, BASE64_STANDARD};
32
use std::io::Write;
43
use std::path::PathBuf;
54
use std::process::{Command, Stdio};
@@ -68,7 +67,9 @@ fn is_wsl() -> bool {
6867
// This enables copying even if there is no Wayland or X socket available,
6968
// e.g. via SSH, as long as it supported by the terminal.
7069
// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
70+
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
7171
fn copy_string_osc52(text: &str, out: &mut impl Write) -> Result<()> {
72+
use base64::prelude::{Engine, BASE64_STANDARD};
7273
const OSC52_DESTINATION_CLIPBOARD: char = 'c';
7374
write!(
7475
out,
@@ -80,33 +81,47 @@ fn copy_string_osc52(text: &str, out: &mut impl Write) -> Result<()> {
8081
}
8182

8283
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
83-
pub fn copy_string(text: &str) -> Result<()> {
84-
if std::env::var("WAYLAND_DISPLAY").is_ok() {
85-
if exec_copy_with_args("wl-copy", &[], text, false).is_err() {
86-
copy_string_osc52(text, &mut std::io::stdout())?;
87-
}
84+
fn copy_string_wayland(text: &str) -> Result<()> {
85+
if exec_copy_with_args("wl-copy", &[], text, false).is_ok() {
86+
return Ok(());
8887
}
8988

90-
if is_wsl() {
91-
return exec_copy_with_args("clip.exe", &[], text, false);
92-
}
89+
copy_string_osc52(text, &mut std::io::stdout())
90+
}
9391

92+
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
93+
fn copy_string_x(text: &str) -> Result<()> {
9494
if exec_copy_with_args(
9595
"xclip",
9696
&["-selection", "clipboard"],
9797
text,
9898
false,
9999
)
100-
.is_err()
100+
.is_ok()
101101
{
102-
if exec_copy_with_args("xsel", &["--clipboard"], text, true)
103-
.is_err()
104-
{
105-
copy_string_osc52(text, &mut std::io::stdout())?;
106-
}
102+
return Ok(());
107103
}
108104

109-
Ok(())
105+
if exec_copy_with_args("xsel", &["--clipboard"], text, true)
106+
.is_ok()
107+
{
108+
return Ok(());
109+
}
110+
111+
copy_string_osc52(text, &mut std::io::stdout())
112+
}
113+
114+
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
115+
pub fn copy_string(text: &str) -> Result<()> {
116+
if std::env::var("WAYLAND_DISPLAY").is_ok() {
117+
return copy_string_wayland(text);
118+
}
119+
120+
if is_wsl() {
121+
return exec_copy_with_args("clip.exe", &[], text, false);
122+
}
123+
124+
copy_string_x(text)
110125
}
111126

112127
#[cfg(any(target_os = "macos", windows))]

0 commit comments

Comments
 (0)