Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ActuallyAllie/cli-clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
allie-wake-up committed Oct 12, 2022
2 parents 7483bda + 41346cb commit 9b4b7be
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 43 deletions.
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ keywords = ["clipboard"]
edition = "2018"
readme = "README.md"

[dependencies]
anyhow = "1.0.31"

[target.'cfg(windows)'.dependencies]
clipboard-win = {version = "4.0.2", features=["std"]}

Expand All @@ -21,6 +18,5 @@ objc_id = "0.1"
objc-foundation = "0.1"

[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))'.dependencies]
failure = "0.1"
wl-clipboard-rs = "0.4"
x11-clipboard = "0.5.1"
wl-clipboard-rs = "0.6"
x11-clipboard = "0.6"
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ cli-clipboard is a fork of [rust-clipboard](https://github.com/aweinstock314/rus

On Linux it will first attempt to setup a Wayland clipboard provider. If that fails it will then fallback to the X11 clipboard provider.

Note: On Linux, you'll need to have xorg-dev and libxcb-composite0-dev to compile. On Debian and Ubuntu you can install them with

sudo apt install xorg-dev libxcb-composite0-dev

## Examples

Using ClipboardContext to create a clipboard provider:
Expand Down Expand Up @@ -53,7 +57,7 @@ fn clear(&mut self) -> anhow::Result<()>;

### ClipboardContext

- `ClipboardContext` is a type alias for one of {`WindowsClipboardContext`, `OSXClipboardContext`, `LinuxClipboardContext`}, all of which implement `ClipboardProvider`. Which concrete type is chosen for `ClipboardContext` depends on the OS (via conditional compilation).
- `ClipboardContext` is a type alias for one of {`WindowsClipboardContext`, `OSXClipboardContext`, `LinuxClipboardContext`}, all of which implement `ClipboardProvider`. Which concrete type is chosen for `ClipboardContext` depends on the OS (via conditional compilation).
- `WaylandClipboardContext` and `X11ClipboardContext` are also available but generally the correct one will be chosen by `LinuxClipboardContext`.

### Convenience Functions
Expand Down
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use anyhow::Result;
use crate::Result;

/// Trait for clipboard access
pub trait ClipboardProvider: Sized {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extern crate x11_clipboard as x11_clipboard_crate;
#[macro_use]
extern crate objc;

use anyhow::Result;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

mod common;
pub use common::ClipboardProvider;
Expand Down
2 changes: 1 addition & 1 deletion src/linux_clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::common::*;
use crate::wayland_clipboard::WaylandClipboardContext;
use crate::x11_clipboard::{Clipboard, X11ClipboardContext};
use anyhow::Result;
use crate::Result;

enum LinuxContext {
Wayland(WaylandClipboardContext),
Expand Down
17 changes: 6 additions & 11 deletions src/macos_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

use crate::common::*;
use anyhow::{anyhow, Result};
use crate::Result;
use objc::runtime::{Class, Object};
use objc_foundation::{INSArray, INSObject, INSString};
use objc_foundation::{NSArray, NSDictionary, NSObject, NSString};
Expand All @@ -32,11 +32,10 @@ extern "C" {}

impl ClipboardProvider for MacOSClipboardContext {
fn new() -> Result<MacOSClipboardContext> {
let cls =
Class::get("NSPasteboard").ok_or_else(|| anyhow!("Class::get(\"NSPasteboard\")"))?;
let cls = Class::get("NSPasteboard").ok_or_else(|| "Class::get(\"NSPasteboard\")")?;
let pasteboard: *mut Object = unsafe { msg_send![cls, generalPasteboard] };
if pasteboard.is_null() {
return Err(anyhow!("NSPasteboard#generalPasteboard returned null"));
return Err("NSPasteboard#generalPasteboard returned null".into());
}
let pasteboard: Id<Object> = unsafe { Id::from_ptr(pasteboard) };
Ok(MacOSClipboardContext { pasteboard })
Expand All @@ -53,16 +52,12 @@ impl ClipboardProvider for MacOSClipboardContext {
let obj: *mut NSArray<NSString> =
msg_send![self.pasteboard, readObjectsForClasses:&*classes options:&*options];
if obj.is_null() {
return Err(anyhow!(
"pasteboard#readObjectsForClasses:options: returned null",
));
return Err("pasteboard#readObjectsForClasses:options: returned null".into());
}
Id::from_ptr(obj)
};
if string_array.count() == 0 {
Err(anyhow!(
"pasteboard#readObjectsForClasses:options: returned empty",
))
Err("pasteboard#readObjectsForClasses:options: returned empty".into())
} else {
Ok(string_array[0].as_str().to_owned())
}
Expand All @@ -75,7 +70,7 @@ impl ClipboardProvider for MacOSClipboardContext {
if success {
Ok(())
} else {
Err(anyhow!("NSPasteboard#writeObjects: returned false"))
Err("NSPasteboard#writeObjects: returned false".into())
}
}

Expand Down
29 changes: 10 additions & 19 deletions src/wayland_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ limitations under the License.
*/

use crate::common::*;
use anyhow::Result;
use failure::Fail;
use crate::Result;
use std::io::{self, Read};
use wl_clipboard_rs::{
copy::{self, clear, Options, ServeRequests},
Expand Down Expand Up @@ -62,10 +61,8 @@ impl ClipboardProvider for WaylandClipboardContext {
fn new() -> Result<WaylandClipboardContext> {
let supports_primary_selection = match utils::is_primary_selection_supported() {
Ok(v) => v,
Err(e) => match e {
utils::PrimarySelectionCheckError::NoSeats => false,
_ => return Err(into_boxed_error(e)),
},
Err(utils::PrimarySelectionCheckError::NoSeats) => false,
Err(e) => return Err(e.into()),
};

Ok(WaylandClipboardContext {
Expand Down Expand Up @@ -111,12 +108,10 @@ impl ClipboardProvider for WaylandClipboardContext {
paste::MimeType::Text,
) {
Ok((reader, _)) => reader,
Err(e) => match e {
paste::Error::NoSeats | paste::Error::ClipboardEmpty | paste::Error::NoMimeType => {
return Ok("".to_string());
}
_ => return Err(into_boxed_error(e)),
},
Err(
paste::Error::NoSeats | paste::Error::ClipboardEmpty | paste::Error::NoMimeType,
) => return Ok("".to_string()),
Err(e) => return Err(e.into()),
};

Ok(read_into_string(&mut reader).map_err(Box::new)?)
Expand Down Expand Up @@ -148,22 +143,18 @@ impl ClipboardProvider for WaylandClipboardContext {
copy::Source::Bytes(data.into_bytes().into()),
copy::MimeType::Text,
)
.map_err(into_boxed_error)
.map_err(Into::into)
}

fn clear(&mut self) -> Result<()> {
if self.supports_primary_selection {
clear(copy::ClipboardType::Both, copy::Seat::All).map_err(into_boxed_error)
clear(copy::ClipboardType::Both, copy::Seat::All).map_err(Into::into)
} else {
clear(copy::ClipboardType::Regular, copy::Seat::All).map_err(into_boxed_error)
clear(copy::ClipboardType::Regular, copy::Seat::All).map_err(Into::into)
}
}
}

fn into_boxed_error<F: 'static + Fail>(fail: F) -> anyhow::Error {
fail.compat().into()
}

fn read_into_string<R: Read>(reader: &mut R) -> io::Result<String> {
let mut contents = String::new();
reader.read_to_string(&mut contents)?;
Expand Down
2 changes: 1 addition & 1 deletion src/windows_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
use clipboard_win::{empty, get_clipboard_string, set_clipboard_string, Clipboard};

use crate::common::ClipboardProvider;
use anyhow::Result;
use crate::Result;

pub struct WindowsClipboardContext;

Expand Down
4 changes: 2 additions & 2 deletions src/x11_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ limitations under the License.
*/

use crate::common::*;
use anyhow::Result;
use crate::Result;
use std::marker::PhantomData;
use std::time::Duration;
use x11_clipboard_crate::xcb::xproto::Atom;
use x11_clipboard_crate::xcb::x::Atom;
use x11_clipboard_crate::Atoms;
use x11_clipboard_crate::Clipboard as X11Clipboard;

Expand Down

0 comments on commit 9b4b7be

Please sign in to comment.