feat: windows clipboard support (#75)

This commit is contained in:
三咲雅 · Misaki Masa 2023-08-20 08:24:41 +08:00 committed by GitHub
parent 64c6cdf072
commit 8c0ca4c649
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 4 deletions

28
Cargo.lock generated
View File

@ -316,6 +316,17 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b"
[[package]]
name = "clipboard-win"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7191c27c2357d9b7ef96baac1773290d4ca63b24205b82a3fd8a0637afcf0362"
dependencies = [
"error-code",
"str-buf",
"winapi",
]
[[package]] [[package]]
name = "color_quant" name = "color_quant"
version = "1.1.0" version = "1.1.0"
@ -365,6 +376,7 @@ dependencies = [
"adaptor", "adaptor",
"anyhow", "anyhow",
"async-channel", "async-channel",
"clipboard-win",
"config", "config",
"crossterm 0.27.0", "crossterm 0.27.0",
"futures", "futures",
@ -569,6 +581,16 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "error-code"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64f18991e7bf11e7ffee451b5318b5c1a73c52d0d0ada6e5a3017c8c1ced6a21"
dependencies = [
"libc",
"str-buf",
]
[[package]] [[package]]
name = "event-listener" name = "event-listener"
version = "2.5.3" version = "2.5.3"
@ -1695,6 +1717,12 @@ dependencies = [
"lock_api", "lock_api",
] ]
[[package]]
name = "str-buf"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e08d8363704e6c71fc928674353e6b7c23dcea9d82d7012c8faf2a3a025f8d0"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"

View File

@ -25,3 +25,6 @@ tracing = "^0"
trash = "^3" trash = "^3"
unicode-width = "^0" unicode-width = "^0"
yazi-prebuild = "^0" yazi-prebuild = "^0"
[target.'cfg(target_os = "windows")'.dependencies]
clipboard-win = "^4"

View File

@ -1,9 +1,14 @@
use std::{ffi::{OsStr, OsString}, os::unix::prelude::{OsStrExt, OsStringExt}, process::Stdio}; use std::ffi::OsString;
use anyhow::{bail, Result}; use anyhow::Result;
use tokio::{io::AsyncWriteExt, process::Command};
#[cfg(not(target_os = "windows"))]
pub async fn clipboard_get() -> Result<OsString> { pub async fn clipboard_get() -> Result<OsString> {
use std::os::unix::prelude::OsStringExt;
use anyhow::bail;
use tokio::process::Command;
let all = [ let all = [
("pbpaste", vec![]), ("pbpaste", vec![]),
("wl-paste", vec![]), ("wl-paste", vec![]),
@ -23,7 +28,22 @@ pub async fn clipboard_get() -> Result<OsString> {
bail!("failed to get clipboard") bail!("failed to get clipboard")
} }
pub async fn clipboard_set(s: impl AsRef<OsStr>) -> Result<()> { #[cfg(target_os = "windows")]
pub async fn clipboard_get() -> Result<OsString> {
use anyhow::anyhow;
use clipboard_win::{formats, get_clipboard};
let result = tokio::task::spawn_blocking(|| get_clipboard::<String, _>(formats::Unicode));
Ok(result.await?.map_err(|_| anyhow!("failed to get clipboard"))?.into())
}
#[cfg(not(target_os = "windows"))]
pub async fn clipboard_set(s: impl AsRef<std::ffi::OsStr>) -> Result<()> {
use std::{os::unix::prelude::OsStrExt, process::Stdio};
use anyhow::bail;
use tokio::{io::AsyncWriteExt, process::Command};
let all = [ let all = [
("pbcopy", vec![]), ("pbcopy", vec![]),
("wl-copy", vec![]), ("wl-copy", vec![]),
@ -55,3 +75,15 @@ pub async fn clipboard_set(s: impl AsRef<OsStr>) -> Result<()> {
bail!("failed to set clipboard") bail!("failed to set clipboard")
} }
#[cfg(target_os = "windows")]
pub async fn clipboard_set(s: impl AsRef<std::ffi::OsStr>) -> Result<()> {
use anyhow::anyhow;
use clipboard_win::{formats, set_clipboard};
let s = s.as_ref().to_owned();
let result =
tokio::task::spawn_blocking(move || set_clipboard(formats::Unicode, s.to_string_lossy()));
Ok(result.await?.map_err(|_| anyhow!("failed to set clipboard"))?)
}