feat: add xclip and xsel support

This commit is contained in:
sxyazi 2023-08-20 00:37:53 +08:00
parent 8499e900c7
commit 1ada483046
No known key found for this signature in database
2 changed files with 32 additions and 6 deletions

View file

@ -2,10 +2,18 @@ use std::{ffi::OsStr, os::unix::prelude::OsStrExt, process::Stdio};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use tokio::{io::AsyncWriteExt, process::Command}; use tokio::{io::AsyncWriteExt, process::Command};
use tracing::info;
pub async fn clipboard_get() -> Result<String> { pub async fn clipboard_get() -> Result<String> {
for cmd in &["pbpaste", "wl-paste"] { let all = [
let output = Command::new(cmd).kill_on_drop(true).output().await?; ("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() { if output.status.success() {
return Ok(String::from_utf8_lossy(&output.stdout).to_string()); return Ok(String::from_utf8_lossy(&output.stdout).to_string());
} }
@ -15,9 +23,27 @@ pub async fn clipboard_get() -> Result<String> {
} }
pub async fn clipboard_set(s: impl AsRef<OsStr>) -> Result<()> { pub async fn clipboard_set(s: impl AsRef<OsStr>) -> Result<()> {
for cmd in &["pbcopy", "wl-copy"] { info!("clipboard_set: {:?}", s.as_ref());
let mut child =
Command::new(cmd).stdin(Stdio::piped()).stdout(Stdio::null()).kill_on_drop(true).spawn()?; 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() { if let Some(mut stdin) = child.stdin.take() {
stdin.write_all(s.as_ref().as_bytes()).await?; stdin.write_all(s.as_ref().as_bytes()).await?;
} }

View file

@ -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"}