Skip to content

Commit

Permalink
Implement --to-clipboard for macOS
Browse files Browse the repository at this point in the history
* The pasteboard crate has been added to Cargo.toml.
* In src/bin, use of this crate is conditional on target_os = "macOS"
* The implementation of dump_image_to_clipboard is exactly like the linux one, except instead of shelling out to xclip, we call the unsafe function:

Pasteboard::Image.copy(temp.path().to_str().unwrap());

Hat tip to @segeljakt for pasteboard :)
  • Loading branch information
dreness committed Aug 11, 2019
1 parent d29efda commit f6c13ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -19,6 +19,7 @@ tempfile = "3.1.0"
conv = "0.3.3"
euclid = "0.20"
log = "0.4.8"
pasteboard = "0.1.1"

[lib]
name = "silicon"
Expand Down
14 changes: 13 additions & 1 deletion src/bin.rs
Expand Up @@ -12,6 +12,8 @@ use syntect::easy::HighlightLines;
use syntect::util::LinesWithEndings;
#[cfg(target_os = "linux")]
use {image::ImageOutputFormat, std::process::Command};
#[cfg(target_os = "macos")]
use {image::ImageOutputFormat, pasteboard::Pasteboard};

pub mod blur;
pub mod config;
Expand All @@ -37,7 +39,17 @@ pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> {
Ok(())
}

#[cfg(not(target_os = "linux"))]
#[cfg(target_os = "macos")]
pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> {
let mut temp = tempfile::NamedTempFile::new()?;
image.write_to(&mut temp, ImageOutputFormat::PNG)?;
unsafe {
Pasteboard::Image.copy(temp.path().to_str().unwrap());
}
Ok(())
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
pub fn dump_image_to_clipboard(_image: &DynamicImage) -> Result<(), Error> {
Err(format_err!(
"This feature hasn't been implemented for your system"
Expand Down

0 comments on commit f6c13ed

Please sign in to comment.