feat(clipboard): add support for xclip and termux

This commit is contained in:
MedzikUser 2022-02-28 23:20:50 +01:00
parent a29ca66e0d
commit 4ad662e35e
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
3 changed files with 59 additions and 15 deletions

View File

@ -2,7 +2,7 @@
## Screenshots ## Screenshots
![upload](https://cdn.magicuser.cf/7eMaL5d.png) ![upload](https://i.imgur.com/MG35kvf.png)
![delete](https://cdn.magicuser.cf/TSxBrhO.png) ![delete](https://cdn.magicuser.cf/TSxBrhO.png)
@ -19,7 +19,10 @@ imgurs completions fish > ~/.config/fish/completions/imgurs.fish
``` ```
## Dependencies ## Dependencies
- **xsel** - support clipboard on Linux - support clipboard on Linux
- **xsel**
- **xclip** - alternative to **xsel**
- **termux-api** - on **Termux**
- **libnotify** - support notification on Linux - **libnotify** - support notification on Linux
## How to install Imgurs CLI? ## How to install Imgurs CLI?

View File

@ -1,18 +1,16 @@
use super::send_api_request;
use crate::api::{ use crate::api::{
configuration::{api_url, ImgurClient}, configuration::{api_url, ImgurClient},
ImageInfo, ImageInfo,
}; };
use super::send_api_request;
use anyhow::Error as anyhow_err;
use reqwest::Method; use reqwest::Method;
use std::{ use std::{
collections::HashMap, collections::HashMap,
io::{Error, ErrorKind}, io::{Error, ErrorKind},
}; };
pub async fn upload_image(c: ImgurClient, image: &str) -> Result<ImageInfo, anyhow_err> { pub async fn upload_image(c: ImgurClient, image: &str) -> Result<ImageInfo, anyhow::Error> {
let mut form = HashMap::new(); let mut form = HashMap::new();
form.insert("image", image.to_string()); form.insert("image", image.to_string());
@ -23,15 +21,15 @@ pub async fn upload_image(c: ImgurClient, image: &str) -> Result<ImageInfo, anyh
let status = res.status(); let status = res.status();
if status.is_client_error() || status.is_server_error() { if status.is_client_error() || status.is_server_error() {
let body = res.text().await.map_err(anyhow_err::new)?; let body = res.text().await.map_err(anyhow::Error::new)?;
let err = Error::new( let err = Error::new(
ErrorKind::Other, ErrorKind::Other,
format!("server returned non-successful status code = {status}, body = {body}"), format!("server returned non-successful status code = {status}, body = {body}"),
); );
Err(anyhow_err::from(err)) Err(anyhow::Error::from(err))
} else { } else {
let content: ImageInfo = res.json().await.map_err(anyhow_err::new)?; let content: ImageInfo = res.json().await.map_err(anyhow::Error::new)?;
Ok(content) Ok(content)
} }
} }

View File

@ -1,3 +1,21 @@
#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
))]
fn is_program_in_path(program: &str) -> bool {
use std::{env, fs};
if let Ok(path) = env::var("PATH") {
for p in path.split(":") {
let p_str = format!("{}/{}", p, program);
if fs::metadata(p_str).is_ok() {
return true;
}
}
}
false
}
#[cfg(all( #[cfg(all(
unix, unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten")) not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
@ -8,12 +26,37 @@ pub fn set_clipboard(content: String) {
process::{Command, Stdio}, process::{Command, Stdio},
}; };
let mut child = Command::new("xsel") use colored::Colorize;
.arg("--input")
.arg("--clipboard") let mut child;
.stdin(Stdio::piped())
.spawn() // xsel
.expect("execute command xsel"); if is_program_in_path("xsel") {
child = Command::new("xsel")
.arg("--input")
.arg("--clipboard")
.stdin(Stdio::piped())
.spawn()
.expect("execute command xsel")
// xclip
} else if is_program_in_path("xclip") {
child = Command::new("xclip")
.arg("-in")
.arg("-selection")
.arg("clipboard")
.stdin(Stdio::piped())
.spawn()
.expect("execute command xclip")
// termux
} else if is_program_in_path("termux-clipboard-set") {
child = Command::new("termux-clipboard-set")
.stdin(Stdio::piped())
.spawn()
.expect("execute command termux-clipboard-set")
} else {
println!("{} {}", "WARN".yellow(), "command for clipboard not found".magenta());
return
}
child child
.stdin .stdin