From 4ad662e35e796afcdb7340c8f1c814597bd8838f Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Mon, 28 Feb 2022 23:20:50 +0100 Subject: [PATCH] feat(clipboard): add support for xclip and termux --- README.md | 7 ++++-- src/api/upload_image.rs | 12 ++++----- src/cli/clipboard.rs | 55 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f29f9fa..7a1e264 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## Screenshots -![upload](https://cdn.magicuser.cf/7eMaL5d.png) +![upload](https://i.imgur.com/MG35kvf.png) ![delete](https://cdn.magicuser.cf/TSxBrhO.png) @@ -19,7 +19,10 @@ imgurs completions fish > ~/.config/fish/completions/imgurs.fish ``` ## Dependencies -- **xsel** - support clipboard on Linux +- support clipboard on Linux + - **xsel** + - **xclip** - alternative to **xsel** + - **termux-api** - on **Termux** - **libnotify** - support notification on Linux ## How to install Imgurs CLI? diff --git a/src/api/upload_image.rs b/src/api/upload_image.rs index 1083731..da0e58a 100644 --- a/src/api/upload_image.rs +++ b/src/api/upload_image.rs @@ -1,18 +1,16 @@ +use super::send_api_request; use crate::api::{ configuration::{api_url, ImgurClient}, ImageInfo, }; -use super::send_api_request; - -use anyhow::Error as anyhow_err; use reqwest::Method; use std::{ collections::HashMap, io::{Error, ErrorKind}, }; -pub async fn upload_image(c: ImgurClient, image: &str) -> Result { +pub async fn upload_image(c: ImgurClient, image: &str) -> Result { let mut form = HashMap::new(); form.insert("image", image.to_string()); @@ -23,15 +21,15 @@ pub async fn upload_image(c: ImgurClient, image: &str) -> Result 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( unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten")) @@ -8,12 +26,37 @@ pub fn set_clipboard(content: String) { process::{Command, Stdio}, }; - let mut child = Command::new("xsel") - .arg("--input") - .arg("--clipboard") - .stdin(Stdio::piped()) - .spawn() - .expect("execute command xsel"); + use colored::Colorize; + + let mut child; + + // 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 .stdin