feat: add xclip and xsel support (#74)

This commit is contained in:
三咲雅 · Misaki Masa 2023-08-20 01:50:03 +08:00 committed by GitHub
parent 8499e900c7
commit bdbd552457
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 13 deletions

View File

@ -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<String> {
for cmd in &["pbpaste", "wl-paste"] {
let output = Command::new(cmd).kill_on_drop(true).output().await?;
pub async fn clipboard_get() -> Result<OsString> {
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<String> {
}
pub async fn clipboard_set(s: impl AsRef<OsStr>) -> 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(());
}
}

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();

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