Skip to content

Commit d4949a6

Browse files
authored
support copy to clipboard on wayland (#1233)
1 parent 074bb7c commit d4949a6

File tree

3 files changed

+34
-41
lines changed

3 files changed

+34
-41
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
* use `GIT_DIR` and `GIT_WORK_DIR` from environment if set ([#1191](https://github.com/extrawurst/gitui/pull/1191))
2525
* new [FAQ](./FAQ.md)s page
2626
* mention macports in install section [[@fs111](https://github.com/fs111)]([#1237](https://github.com/extrawurst/gitui/pull/1237))
27+
* support copy to clipboard on wayland ([#397](https://github.com/extrawurst/gitui/issues/397))
2728

2829
### Fixed
2930
* opening tags list without remotes ([#1111](https://github.com/extrawurst/gitui/issues/1111))

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ tui = { version = "0.18", default-features = false, features = ['crossterm', 'se
5050
unicode-segmentation = "1.9"
5151
unicode-truncate = "0.2"
5252
unicode-width = "0.1"
53-
54-
[target.'cfg(all(target_family="unix",not(target_os="macos")))'.dependencies]
5553
which = "4.2"
5654

5755
# pprof is not available on windows

src/clipboard.rs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
use anyhow::{anyhow, Result};
2-
#[cfg(target_family = "unix")]
3-
#[cfg(not(target_os = "macos"))]
4-
use std::ffi::OsStr;
52
use std::io::Write;
3+
use std::path::PathBuf;
64
use std::process::{Command, Stdio};
5+
use which::which;
76

8-
fn execute_copy_command(command: Command, text: &str) -> Result<()> {
9-
let mut command = command;
7+
fn exec_copy_with_args(
8+
command: &str,
9+
args: &[&str],
10+
text: &str,
11+
) -> Result<()> {
12+
let binary = which(command)
13+
.ok()
14+
.unwrap_or_else(|| PathBuf::from(command));
1015

11-
let mut process = command
16+
let mut process = Command::new(binary)
17+
.args(args)
1218
.stdin(Stdio::piped())
1319
.stdout(Stdio::null())
1420
.spawn()
@@ -28,47 +34,35 @@ fn execute_copy_command(command: Command, text: &str) -> Result<()> {
2834
Ok(())
2935
}
3036

31-
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
32-
fn gen_command(
33-
path: impl AsRef<OsStr>,
34-
xclip_syntax: bool,
35-
) -> Command {
36-
let mut c = Command::new(path);
37-
if xclip_syntax {
38-
c.arg("-selection");
39-
c.arg("clipboard");
40-
} else {
41-
c.arg("--clipboard");
42-
}
43-
c
37+
fn exec_copy(command: &str, text: &str) -> Result<()> {
38+
exec_copy_with_args(command, &[], text)
4439
}
4540

4641
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
47-
pub fn copy_string(string: &str) -> Result<()> {
48-
use std::path::PathBuf;
49-
use which::which;
50-
let (path, xclip_syntax) = which("xclip").ok().map_or_else(
51-
|| {
52-
(
53-
which("xsel")
54-
.ok()
55-
.unwrap_or_else(|| PathBuf::from("xsel")),
56-
false,
57-
)
58-
},
59-
|path| (path, true),
60-
);
42+
pub fn copy_string(text: &str) -> Result<()> {
43+
if std::env::var("WAYLAND_DISPLAY").is_ok() {
44+
return exec_copy("wl-copy", text);
45+
}
6146

62-
let cmd = gen_command(path, xclip_syntax);
63-
execute_copy_command(cmd, string)
47+
if exec_copy_with_args(
48+
"xclip",
49+
&["-selection", "clipboard"],
50+
text,
51+
)
52+
.is_err()
53+
{
54+
return exec_copy_with_args("xsel", &["--clipboard"], text);
55+
}
56+
57+
Ok(())
6458
}
6559

6660
#[cfg(target_os = "macos")]
67-
pub fn copy_string(string: &str) -> Result<()> {
68-
execute_copy_command(Command::new("pbcopy"), string)
61+
pub fn copy_string(text: &str) -> Result<()> {
62+
exec_copy("pbcopy", text)
6963
}
7064

7165
#[cfg(windows)]
72-
pub fn copy_string(string: &str) -> Result<()> {
73-
execute_copy_command(Command::new("clip"), string)
66+
pub fn copy_string(text: &str) -> Result<()> {
67+
exec_copy("clip", text)
7468
}

0 commit comments

Comments
 (0)