This commit is contained in:
sxyazi 2023-08-20 01:17:58 +08:00
parent 1ada483046
commit 6cefccb939
No known key found for this signature in database
2 changed files with 16 additions and 17 deletions

View file

@ -1,10 +1,9 @@
use std::{ffi::OsStr, os::unix::prelude::OsStrExt, process::Stdio};
use std::{ffi::{OsStr, OsString}, os::unix::prelude::{OsStrExt, OsStringExt}, process::Stdio};
use anyhow::{bail, Result};
use tokio::{io::AsyncWriteExt, process::Command};
use tracing::info;
pub async fn clipboard_get() -> Result<String> {
pub async fn clipboard_get() -> Result<OsString> {
let all = [
("pbpaste", vec![]),
("wl-paste", vec![]),
@ -13,9 +12,11 @@ pub async fn clipboard_get() -> Result<String> {
];
for (cmd, args) in all {
let output = Command::new(cmd).args(args).kill_on_drop(true).output().await?;
let Ok(output) = Command::new(cmd).args(args).kill_on_drop(true).output().await else {
continue;
};
if output.status.success() {
return Ok(String::from_utf8_lossy(&output.stdout).to_string());
return Ok(OsString::from_vec(output.stdout));
}
}
@ -23,8 +24,6 @@ pub async fn clipboard_get() -> Result<String> {
}
pub async fn clipboard_set(s: impl AsRef<OsStr>) -> Result<()> {
info!("clipboard_set: {:?}", s.as_ref());
let all = [
("pbcopy", vec![]),
("wl-copy", vec![]),
@ -33,21 +32,21 @@ pub async fn clipboard_set(s: impl AsRef<OsStr>) -> Result<()> {
];
for (cmd, args) in all {
info!("clipboard_set: trying {:?} {:?}", cmd, args);
let child = Command::new(cmd)
let Ok(mut child) = Command::new(cmd)
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::null())
.kill_on_drop(true)
.spawn();
.spawn()
else {
continue;
};
info!("clipboard_set: spawned {:?}", child);
let mut child = child?;
if let Some(mut stdin) = child.stdin.take() {
stdin.write_all(s.as_ref().as_bytes()).await?;
let mut stdin = child.stdin.take().unwrap();
if stdin.write_all(s.as_ref().as_bytes()).await.is_err() {
continue;
}
if child.wait().await?.success() {
if child.wait().await.map(|s| s.success()).unwrap_or_default() {
return Ok(());
}
}

View file

@ -252,7 +252,7 @@ impl Input {
}
self.insert(!before);
for c in s.chars() {
for c in s.to_string_lossy().chars() {
self.type_(c);
}
self.escape();