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 anyhow::{bail, Result};
use tokio::{io::AsyncWriteExt, process::Command}; use tokio::{io::AsyncWriteExt, process::Command};
pub async fn clipboard_get() -> Result<String> { pub async fn clipboard_get() -> Result<OsString> {
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 Ok(output) = Command::new(cmd).args(args).kill_on_drop(true).output().await else {
continue;
};
if output.status.success() { 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<()> { pub async fn clipboard_set(s: impl AsRef<OsStr>) -> Result<()> {
for cmd in &["pbcopy", "wl-copy"] { let all = [
let mut child = ("pbcopy", vec![]),
Command::new(cmd).stdin(Stdio::piped()).stdout(Stdio::null()).kill_on_drop(true).spawn()?; ("wl-copy", vec![]),
if let Some(mut stdin) = child.stdin.take() { ("xclip", vec!["-selection", "clipboard"]),
stdin.write_all(s.as_ref().as_bytes()).await?; ("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(()); return Ok(());
} }
} }

View file

@ -252,7 +252,7 @@ impl Input {
} }
self.insert(!before); self.insert(!before);
for c in s.chars() { for c in s.to_string_lossy().chars() {
self.type_(c); self.type_(c);
} }
self.escape(); 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"}