mirror of https://github.com/Aloxaf/silicon
Improve support for Windows clipboard
This commit is contained in:
parent
d29efda181
commit
db13d66f58
|
@ -1157,6 +1157,7 @@ name = "silicon"
|
|||
version = "0.2.6"
|
||||
dependencies = [
|
||||
"clipboard 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clipboard-win 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"conv 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"euclid 0.20.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -28,6 +28,10 @@ path = "src/lib.rs"
|
|||
name = "silicon"
|
||||
path = "src/bin.rs"
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies.clipboard-win]
|
||||
version = "2.2.0"
|
||||
optional = true
|
||||
|
||||
[dependencies.failure]
|
||||
version = "0.1.5"
|
||||
optional = true
|
||||
|
@ -46,4 +50,4 @@ optional = true
|
|||
|
||||
[features]
|
||||
default = [ "bin" ]
|
||||
bin = [ "structopt", "env_logger", "failure" ]
|
||||
bin = [ "structopt", "env_logger", "failure", "clipboard-win" ]
|
||||
|
|
28
src/bin.rs
28
src/bin.rs
|
@ -3,15 +3,13 @@ extern crate log;
|
|||
#[macro_use]
|
||||
extern crate failure;
|
||||
|
||||
use crate::clipboard::dump_image_to_clipboard;
|
||||
use crate::config::Config;
|
||||
use crate::utils::*;
|
||||
use failure::Error;
|
||||
use image::DynamicImage;
|
||||
use structopt::StructOpt;
|
||||
use syntect::easy::HighlightLines;
|
||||
use syntect::util::LinesWithEndings;
|
||||
#[cfg(target_os = "linux")]
|
||||
use {image::ImageOutputFormat, std::process::Command};
|
||||
|
||||
pub mod blur;
|
||||
pub mod config;
|
||||
|
@ -20,29 +18,7 @@ pub mod font;
|
|||
pub mod formatter;
|
||||
pub mod utils;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> {
|
||||
let mut temp = tempfile::NamedTempFile::new()?;
|
||||
image.write_to(&mut temp, ImageOutputFormat::PNG)?;
|
||||
Command::new("xclip")
|
||||
.args(&[
|
||||
"-sel",
|
||||
"clip",
|
||||
"-t",
|
||||
"image/png",
|
||||
temp.path().to_str().unwrap(),
|
||||
])
|
||||
.status()
|
||||
.map_err(|e| format_err!("Failed to copy image to clipboard: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub fn dump_image_to_clipboard(_image: &DynamicImage) -> Result<(), Error> {
|
||||
Err(format_err!(
|
||||
"This feature hasn't been implemented for your system"
|
||||
))
|
||||
}
|
||||
mod clipboard;
|
||||
|
||||
fn run() -> Result<(), Error> {
|
||||
let config: Config = Config::from_args();
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
use failure::Error;
|
||||
use image::DynamicImage;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> {
|
||||
use clipboard_win::{Clipboard, formats};
|
||||
use image::ImageOutputFormat;
|
||||
|
||||
let mut buffer = vec![];
|
||||
image.write_to(&mut buffer, ImageOutputFormat::BMP)?;
|
||||
|
||||
Clipboard::new()
|
||||
.unwrap()
|
||||
.set(formats::CF_BITMAP, &buffer)
|
||||
.map_err(|e| format_err!("Failed to copy image to clipboard: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> {
|
||||
use std::process::Command;
|
||||
use image::ImageOutputFormat;
|
||||
|
||||
let mut temp = tempfile::NamedTempFile::new()?;
|
||||
image.write_to(&mut temp, ImageOutputFormat::PNG)?;
|
||||
|
||||
Command::new("xclip")
|
||||
.args(&[
|
||||
"-sel",
|
||||
"clip",
|
||||
"-t",
|
||||
"image/png",
|
||||
temp.path().to_str().unwrap(),
|
||||
])
|
||||
.status()
|
||||
.map_err(|e| format_err!("Failed to copy image to clipboard: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
|
||||
pub fn dump_image_to_clipboard(_image: &DynamicImage) -> Result<(), Error> {
|
||||
Err(format_err!(
|
||||
"This feature hasn't been implemented for your system"
|
||||
))
|
||||
}
|
Loading…
Reference in New Issue