Skip to content

Commit 85d7018

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 85d7018

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/clipboard.rs

+34-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,12 @@ 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(any(
71+
all(target_family = "unix", not(target_os = "macos")),
72+
test
73+
))]
7174
fn copy_string_osc52(text: &str, out: &mut impl Write) -> Result<()> {
75+
use base64::prelude::{Engine, BASE64_STANDARD};
7276
const OSC52_DESTINATION_CLIPBOARD: char = 'c';
7377
write!(
7478
out,
@@ -80,33 +84,47 @@ fn copy_string_osc52(text: &str, out: &mut impl Write) -> Result<()> {
8084
}
8185

8286
#[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-
}
87+
fn copy_string_wayland(text: &str) -> Result<()> {
88+
if exec_copy_with_args("wl-copy", &[], text, false).is_ok() {
89+
return Ok(());
8890
}
8991

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

95+
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
96+
fn copy_string_x(text: &str) -> Result<()> {
9497
if exec_copy_with_args(
9598
"xclip",
9699
&["-selection", "clipboard"],
97100
text,
98101
false,
99102
)
100-
.is_err()
103+
.is_ok()
101104
{
102-
if exec_copy_with_args("xsel", &["--clipboard"], text, true)
103-
.is_err()
104-
{
105-
copy_string_osc52(text, &mut std::io::stdout())?;
106-
}
105+
return Ok(());
107106
}
108107

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

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

0 commit comments

Comments
 (0)