From bdbd552457d1029ae39c5c5ec2e4f06d5a53498f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Sun, 20 Aug 2023 01:50:03 +0800 Subject: [PATCH] feat: add `xclip` and `xsel` support (#74) --- core/src/external/clipboard.rs | 49 ++++++++++++++++++++++++++-------- core/src/input/input.rs | 2 +- cspell.json | 2 +- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/core/src/external/clipboard.rs b/core/src/external/clipboard.rs index a7704137..ca8d5f20 100644 --- a/core/src/external/clipboard.rs +++ b/core/src/external/clipboard.rs @@ -1,13 +1,22 @@ -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}; -pub async fn clipboard_get() -> Result { - for cmd in &["pbpaste", "wl-paste"] { - let output = Command::new(cmd).kill_on_drop(true).output().await?; +pub async fn clipboard_get() -> Result { + let all = [ + ("pbpaste", vec![]), + ("wl-paste", vec![]), + ("xclip", vec!["-o", "-selection", "clipboard"]), + ("xsel", vec!["-ob"]), + ]; + + for (cmd, args) in all { + 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)); } } @@ -15,13 +24,31 @@ pub async fn clipboard_get() -> Result { } pub async fn clipboard_set(s: impl AsRef) -> Result<()> { - for cmd in &["pbcopy", "wl-copy"] { - let mut child = - Command::new(cmd).stdin(Stdio::piped()).stdout(Stdio::null()).kill_on_drop(true).spawn()?; - if let Some(mut stdin) = child.stdin.take() { - stdin.write_all(s.as_ref().as_bytes()).await?; + let all = [ + ("pbcopy", vec![]), + ("wl-copy", vec![]), + ("xclip", vec!["-selection", "clipboard"]), + ("xsel", vec!["-ib"]), + ]; + + for (cmd, args) in all { + let Ok(mut child) = Command::new(cmd) + .args(args) + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .kill_on_drop(true) + .spawn() + else { + continue; + }; + + 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() { + drop(stdin); + + 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(); diff --git a/cspell.json b/cspell.json index 01207a52..9058cffe 100644 --- a/cspell.json +++ b/cspell.json @@ -1 +1 @@ -{"language":"en","version":"0.2","flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos"]} +{"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel"],"language":"en","flagWords":[],"version":"0.2"}