Skip to content

Commit

Permalink
replace copypasta with well-maintained arboard
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Nov 20, 2023
1 parent 1d01324 commit d7537c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ tuirs-termion = ["tuirs", "dep:termion", "tui/termion"]
tuirs-no-backend = ["tuirs"]
# Other optional features
search = ["dep:regex"]
clipboard = ["dep:copypasta"]
clipboard = ["dep:arboard"]

[dependencies]
arbitrary = { version = "1", features = ["derive"], optional = true }
copypasta = { version = "0.10.0", optional = true }
arboard = { version = "3.2.1", default-features = false, features = ["wayland-data-control"], optional = true }
crossterm = { package = "crossterm", version = "0.27", optional = true }
crossterm-025 = { package = "crossterm", version = "0.25", optional = true }
ratatui = { version = ">=0.23.0, <1", default-features = false, optional = true }
Expand Down
20 changes: 9 additions & 11 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "clipboard")]
use copypasta::{ClipboardContext, ClipboardProvider as _};
use arboard::Clipboard as Arboard;
use std::borrow::Cow;
#[cfg(feature = "clipboard")]
use std::cell::RefCell;
Expand All @@ -23,13 +23,13 @@ pub enum Clipboard {
Piece(String),
Chunk(Vec<String>),
#[cfg(feature = "clipboard")]
Os(RefCell<ClipboardContext>), // Use `RefCell` not to make `Clipboard::contents` mut method
Os(RefCell<Arboard>), // Use `RefCell` not to make `Clipboard::contents` mut method
}

impl Default for Clipboard {
fn default() -> Self {
#[cfg(feature = "clipboard")]
if let Ok(ctx) = ClipboardContext::new() {
if let Ok(ctx) = Arboard::new() {
return Self::Os(RefCell::new(ctx));
}
Self::Piece(String::new())
Expand All @@ -50,7 +50,7 @@ impl Clipboard {
#[cfg(feature = "clipboard")]
if let Self::Os(ctx) = self {
if let Ok(mut ctx) = ctx.try_borrow_mut() {
let _ = ctx.set_contents(s);
let _ = ctx.set_text(s);
return;
}
}
Expand All @@ -66,7 +66,7 @@ impl Clipboard {
#[cfg(feature = "clipboard")]
if let Self::Os(ctx) = self {
if let Ok(mut ctx) = ctx.try_borrow_mut() {
let _ = ctx.set_contents(c.join("\n"));
let _ = ctx.set_text(c.join("\n"));
return;
}
}
Expand All @@ -82,7 +82,7 @@ impl Clipboard {
#[cfg(feature = "clipboard")]
Self::Os(ctx) => {
if let Ok(mut ctx) = ctx.try_borrow_mut() {
if let Ok(contents) = ctx.get_contents() {
if let Ok(contents) = ctx.get_text() {
let mut lines = contents
.split('\n')
.map(|s| s.strip_suffix('\r').unwrap_or(s).to_string())
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Clone for Clipboard {
Self::Chunk(c) => Self::Chunk(c.clone()),
#[cfg(feature = "clipboard")]
Self::Os(_) => {
if let Ok(ctx) = ClipboardContext::new() {
if let Ok(ctx) = Arboard::new() {
Self::Os(RefCell::new(ctx))
} else {
Self::Piece(String::new())
Expand Down Expand Up @@ -157,10 +157,8 @@ mod tests {
#[test]
fn default_value() {
let _guard = guard();
ClipboardContext::new()
.unwrap()
.set_contents("Hello, world".into())
.unwrap();
let mut arboard = Arboard::new().unwrap();
arboard.set_text("Hello, world").unwrap();
let c = Clipboard::default();
assert_eq!(String::from(c.contents()), "Hello, world");
}
Expand Down

0 comments on commit d7537c3

Please sign in to comment.