From 6cefccb9396b2450bfa7a8dbad428af9597a1e37 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 20 Aug 2023 01:17:58 +0800 Subject: [PATCH] .. --- core/src/external/clipboard.rs | 31 +++++++++++++++---------------- core/src/input/input.rs | 2 +- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/core/src/external/clipboard.rs b/core/src/external/clipboard.rs index ed852e3b..b26075d5 100644 --- a/core/src/external/clipboard.rs +++ b/core/src/external/clipboard.rs @@ -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 { +pub async fn clipboard_get() -> Result { let all = [ ("pbpaste", vec![]), ("wl-paste", vec![]), @@ -13,9 +12,11 @@ pub async fn clipboard_get() -> Result { ]; 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 { } pub async fn clipboard_set(s: impl AsRef) -> 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) -> 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(()); } } diff --git a/core/src/input/input.rs b/core/src/input/input.rs index 2018d278..2f009947 100644 --- a/core/src/input/input.rs +++ b/core/src/input/input.rs @@ -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();