Skip to content

Commit

Permalink
Merge pull request #60 from kamiyaa/master
Browse files Browse the repository at this point in the history
Add wayland clipboard support
  • Loading branch information
conradkleinespel authored Jan 25, 2021
2 parents 666421b + 3050e4b commit cc85601
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 26 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ curl https://sh.rustup.rs -sSf | sh -s -- -y
cargo install --root /usr rooster
```

On **Wayland**:

install [wl-clipboard](https://github.com/bugaevc/wl-clipboard)

Make sure you have the following environment variable set: `XDG_SESSION_TYPE=wayland`


For other distributions, the various Docker files can help you find which dependencies you need.

Once you have installed Rooster (see instructions below), you can view documentation with:
Expand All @@ -93,6 +100,7 @@ Here's a list of existing Rooster contributors:
- [@dwrensha](https://github.com/dwrensha)
- [@Eternity-Yarr](https://github.com/Eternity-Yarr)
- [@jaezun](https://github.com/jaezun)
- [@kamiyaa](https://github.com/kamiyaa)
- [@maxjacobson](https://github.com/maxjacobson)
- [@qmx](https://github.com/qmx)
- [@yamnikov-oleg](https://github.com/yamnikov-oleg)
Expand Down
90 changes: 64 additions & 26 deletions src/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,40 @@ pub fn copy_to_clipboard(s: &SafeString) -> Result<(), ()> {
use quale::which;
use shell_escape;
use std::process::Command;
use std::env;

let password = SafeString::new(shell_escape::escape(s.deref().into()).into());

match which("xsel") {
Some(xsel) => {
let shell = format!(
"printf '%s' {} | {} -ib 2> /dev/null",
password.deref(),
xsel.to_string_lossy()
);
if Command::new("sh")
.args(&["-c", shell.as_str()])
.status()
.map_err(|_| ())?
.success()
{
Ok(())
} else {
Err(())
fn wayland_clipboards(password: &SafeString) -> Result<(), ()> {
match which("wl-copy") {
Some(wl_copy) => {
let shell = format!(
"printf '%s' {} | {} 2> /dev/null",
password.deref(),
wl_copy.to_string_lossy()
);
if Command::new("sh")
.args(&["-c", shell.as_str()])
.status()
.map_err(|_| ())?
.success()
{
Ok(())
} else {
Err(())
}
}
None => Err(()),
}
None => match which("xclip") {
Some(xclip) => {
}

fn x11_clipboards(password: &SafeString) -> Result<(), ()> {
match which("xsel") {
Some(xsel) => {
let shell = format!(
"printf '%s' {} | {} -selection clipboard 2> /dev/null",
"printf '%s' {} | {} -ib 2> /dev/null",
password.deref(),
xclip.to_string_lossy()
xsel.to_string_lossy()
);
if Command::new("sh")
.args(&["-c", shell.as_str()])
Expand All @@ -75,19 +82,49 @@ pub fn copy_to_clipboard(s: &SafeString) -> Result<(), ()> {
Err(())
}
}
None => Err(()),
},
None => match which("xclip") {
Some(xclip) => {
let shell = format!(
"printf '%s' {} | {} -selection clipboard 2> /dev/null",
password.deref(),
xclip.to_string_lossy()
);
if Command::new("sh")
.args(&["-c", shell.as_str()])
.status()
.map_err(|_| ())?
.success()
{
Ok(())
} else {
Err(())
}
}
None => Err(()),
}
}
}

match env::var_os("XDG_SESSION_TYPE") {
Some(s) if s == "wayland" => {
let s = wayland_clipboards(&password);
match s {
Ok(_) => Ok(()),
Err(_) => x11_clipboards(&password)
}
}
_ => x11_clipboards(&password),
}
}

#[cfg(target_os = "macos")]
pub fn paste_keys() -> String {
"Cmd+V".to_string()
pub fn paste_keys() -> &'static str {
"Cmd+V"
}

#[cfg(not(target_os = "macos"))]
pub fn paste_keys() -> String {
"Ctrl+V".to_string()
pub fn paste_keys() -> &'static str {
"Ctrl+V"
}

pub fn confirm_password_retrieved<
Expand Down Expand Up @@ -136,3 +173,4 @@ pub fn confirm_password_retrieved<
}
}
}

0 comments on commit cc85601

Please sign in to comment.