Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions zee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ zee-grammar = { version = "0.4.0", path = "../zee-grammar" }

# Optional dependencies
crossclip = { optional = true, version = "0.7.1" }
wl-clipboard-rs = { optional = true, version = "0.9.2" }

[build-dependencies]
anyhow = "1.0.58"
Expand All @@ -61,8 +62,5 @@ zee-grammar = { version = "0.4.0", path = "../zee-grammar" }

[features]
default = []

# Enables integration with the system's clipboard.
system-clipboard = [
"crossclip",
]
wayland-clipboard = ["wl-clipboard-rs"]
system-clipboard = ["crossclip"]
45 changes: 44 additions & 1 deletion zee/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,57 @@ pub trait Clipboard {

pub fn create() -> Result<Arc<dyn Clipboard>> {
cfg_if::cfg_if! {
if #[cfg(feature = "system-clipboard")] {
if #[cfg(feature = "wayland-clipboard")] {
wayland::create()
} else if #[cfg(feature = "system-clipboard")] {
system::create()
} else {
local::create()
}
}
}

#[cfg(feature = "wayland-clipboard")]
mod wayland {
use super::Clipboard;
use crate::error::Result;
use std::io::Read;
use std::sync::Arc;
use wl_clipboard_rs::{
copy::{self, MimeType},
paste,
};

pub(crate) fn create() -> Result<Arc<dyn super::Clipboard>> {
Ok(Arc::new(WaylandClipboard))
}

struct WaylandClipboard;

impl Clipboard for WaylandClipboard {
fn get_contents(&self) -> Result<String> {
let (mut pipe_reader, _) = paste::get_contents(
paste::ClipboardType::Regular,
paste::Seat::Unspecified,
paste::MimeType::Text,
)?;

let mut buf = String::new();
pipe_reader.read_to_string(&mut buf)?;
Ok(buf)
}

fn set_contents(&self, contents: String) -> crate::error::Result<()> {
let opts = copy::Options::new();
opts.copy(
copy::Source::Bytes(contents.into_bytes().into()),
MimeType::Autodetect,
)?;
Ok(())
}
}
}

#[cfg(feature = "system-clipboard")]
mod system {
use crossclip::Clipboard;
Expand Down