From 1ada48304616ae8c3fb3f9007bcfcae46e495a31 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 20 Aug 2023 00:37:53 +0800 Subject: [PATCH] feat: add `xclip` and `xsel` support --- core/src/external/clipboard.rs | 36 +++++++++++++++++++++++++++++----- cspell.json | 2 +- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/core/src/external/clipboard.rs b/core/src/external/clipboard.rs index a7704137..ed852e3b 100644 --- a/core/src/external/clipboard.rs +++ b/core/src/external/clipboard.rs @@ -2,10 +2,18 @@ use std::{ffi::OsStr, os::unix::prelude::OsStrExt, process::Stdio}; use anyhow::{bail, Result}; use tokio::{io::AsyncWriteExt, process::Command}; +use tracing::info; pub async fn clipboard_get() -> Result { - for cmd in &["pbpaste", "wl-paste"] { - let output = Command::new(cmd).kill_on_drop(true).output().await?; + let all = [ + ("pbpaste", vec![]), + ("wl-paste", vec![]), + ("xclip", vec!["-o", "-selection", "clipboard"]), + ("xsel", vec!["-ob"]), + ]; + + for (cmd, args) in all { + let output = Command::new(cmd).args(args).kill_on_drop(true).output().await?; if output.status.success() { return Ok(String::from_utf8_lossy(&output.stdout).to_string()); } @@ -15,9 +23,27 @@ 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()?; + info!("clipboard_set: {:?}", s.as_ref()); + + let all = [ + ("pbcopy", vec![]), + ("wl-copy", vec![]), + ("xclip", vec!["-selection", "clipboard"]), + ("xsel", vec!["-ib"]), + ]; + + for (cmd, args) in all { + info!("clipboard_set: trying {:?} {:?}", cmd, args); + let child = Command::new(cmd) + .args(args) + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .kill_on_drop(true) + .spawn(); + + 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?; } 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"}