add clipboard, print colors in image info

This commit is contained in:
Medzik 2022-02-25 18:24:24 +01:00
parent f516bde8cd
commit 329263edb0
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
11 changed files with 214 additions and 65 deletions

View File

@ -4,7 +4,6 @@ on:
push: push:
branches: branches:
- main - main
paths-ignore: paths-ignore:
- '*.md' - '*.md'
@ -16,9 +15,10 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - name: Checkout
uses: actions/checkout@v2
- name: Setup GIT - name: Setup Git
run: | run: |
git config --global user.name "MedzikUserBot" git config --global user.name "MedzikUserBot"
git config --global user.email "rm99iv9s@duck.com" git config --global user.email "rm99iv9s@duck.com"
@ -43,7 +43,7 @@ jobs:
git diff-index --quiet HEAD || git commit -m "rustfmt" git diff-index --quiet HEAD || git commit -m "rustfmt"
- name: Push changes - name: Push changes
uses: ad-m/github-push-action@a3fd843e49cd58d296bdd2431c4853569a1b900f uses: ad-m/github-push-action@b007e7b818e33b04afd056e4c4b57ba917145d7a
with: with:
github_token: ${{ secrets.GITHUB_TOKEN }} github_token: ${{ secrets.GITHUB_TOKEN }}
branch: 'main' branch: 'main'

84
Cargo.lock generated
View File

@ -44,7 +44,7 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d76e1fe0171b6d0857afca5671db12a44e71e80823db13ab39f776fb09ad079" checksum = "1d76e1fe0171b6d0857afca5671db12a44e71e80823db13ab39f776fb09ad079"
dependencies = [ dependencies = [
"clipboard-win", "clipboard-win 4.4.1",
"core-graphics", "core-graphics",
"image", "image",
"log", "log",
@ -328,6 +328,40 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "clipboard"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25a904646c0340239dcf7c51677b33928bf24fdf424b79a57909c0109075b2e7"
dependencies = [
"clipboard-win 2.2.0",
"objc",
"objc-foundation",
"objc_id",
"x11-clipboard",
]
[[package]]
name = "clipboard-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac0b9a9fdf4e6100bfcdc6587c9c26a6a1d1d8247e74684c18a113d635d3dc78"
dependencies = [
"clipboard",
"libc",
"which",
"x11-clipboard",
]
[[package]]
name = "clipboard-win"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a093d6fed558e5fe24c3dfc85a68bb68f1c824f440d3ba5aca189e2998786b"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "clipboard-win" name = "clipboard-win"
version = "4.4.1" version = "4.4.1"
@ -386,9 +420,9 @@ checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.2" version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6888e10551bb93e424d8df1d07f1a8b4fceb0001a3a4b048bfc47554946f47b3" checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146"
dependencies = [ dependencies = [
"core-foundation-sys", "core-foundation-sys",
"libc", "libc",
@ -427,9 +461,9 @@ dependencies = [
[[package]] [[package]]
name = "crc32fast" name = "crc32fast"
version = "1.3.1" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2209c310e29876f7f0b2721e7e26b84aff178aa3da5d091f9bfbf47669e60e3" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
] ]
@ -554,6 +588,15 @@ version = "2.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71" checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71"
[[package]]
name = "failure"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
dependencies = [
"backtrace",
]
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "1.7.0" version = "1.7.0"
@ -858,6 +901,8 @@ dependencies = [
"chrono", "chrono",
"clap", "clap",
"clap_complete", "clap_complete",
"clipboard-ext",
"colored",
"dirs 4.0.0", "dirs 4.0.0",
"log", "log",
"notify-rust", "notify-rust",
@ -2084,6 +2129,16 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "which"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724"
dependencies = [
"failure",
"libc",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"
@ -2180,6 +2235,15 @@ dependencies = [
"xml-rs", "xml-rs",
] ]
[[package]]
name = "x11-clipboard"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89bd49c06c9eb5d98e6ba6536cf64ac9f7ee3a009b2f53996d405b3944f6bcea"
dependencies = [
"xcb",
]
[[package]] [[package]]
name = "x11rb" name = "x11rb"
version = "0.8.1" version = "0.8.1"
@ -2192,6 +2256,16 @@ dependencies = [
"winapi-wsapoll", "winapi-wsapoll",
] ]
[[package]]
name = "xcb"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e917a3f24142e9ff8be2414e36c649d47d6cc2ba81f16201cdef96e533e02de"
dependencies = [
"libc",
"log",
]
[[package]] [[package]]
name = "xml-rs" name = "xml-rs"
version = "0.8.4" version = "0.8.4"

View File

@ -31,6 +31,12 @@ clap_complete = "3.1.0"
anyhow = "1.0.55" anyhow = "1.0.55"
better-panic = "0.3.0" better-panic = "0.3.0"
validator = "0.14.0" validator = "0.14.0"
colored = "2.0.0"
[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))'.dependencies]
clipboard-ext = "0.2.0"
[target.'cfg(not(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten")))))'.dependencies]
arboard = "2.0.1" arboard = "2.0.1"
[dependencies.clap] [dependencies.clap]

View File

@ -3,3 +3,6 @@ id = '3e3ce0d7ac14d56'
[notification] [notification]
enabled = true enabled = true
[clipboard]
enabled = true

View File

@ -1,13 +1,13 @@
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ImageInfo { pub struct ImageInfo {
pub data: ImageInfoData, pub data: ImageInfoData,
pub success: bool, pub success: bool,
pub status: i32, pub status: i32,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ImageInfoData { pub struct ImageInfoData {
pub id: String, pub id: String,
pub title: Option<String>, pub title: Option<String>,

34
src/cli/clipboard.rs Normal file
View File

@ -0,0 +1,34 @@
#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
))]
use clipboard_ext::prelude::*;
#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
))]
use clipboard_ext::x11_fork::ClipboardContext;
#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
))]
pub fn set_clipboard(content: String) {
let mut ctx = ClipboardContext::new().expect("init clipboard");
ctx.set_contents(content).expect("set clipboard");
}
#[cfg(not(all(
unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
)))]
use arboard::Clipboard;
#[cfg(not(all(
unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))
)))]
pub fn set_clipboard(content: String) {
let mut clipboard = Clipboard::new().unwrap();
clipboard.set_text(content).unwrap();
}

View File

@ -4,5 +4,5 @@ use super::print_image_info;
pub async fn image_info(client: ImgurClient, id: &str) { pub async fn image_info(client: ImgurClient, id: &str) {
let i = get_image(client, id).await.expect("send api request"); let i = get_image(client, id).await.expect("send api request");
print_image_info(i, false); print_image_info(i);
} }

View File

@ -1,62 +1,77 @@
pub mod clipboard;
pub mod credits; pub mod credits;
pub mod delete_image; pub mod delete_image;
pub mod info_image; pub mod info_image;
pub mod parse; pub mod parse;
pub mod upload_image; pub mod upload_image;
use crate::config::toml::parse;
use imgurs::api::ImageInfo; use imgurs::api::ImageInfo;
use arboard::Clipboard;
use chrono::{prelude::DateTime, Utc}; use chrono::{prelude::DateTime, Utc};
use log::info; use colored::Colorize;
use notify_rust::Notification;
use std::time::{Duration, UNIX_EPOCH}; use std::time::{Duration, UNIX_EPOCH};
pub fn print_image_info(i: ImageInfo, notify: bool) { pub fn print_image_info(i: ImageInfo) {
let d = UNIX_EPOCH + Duration::from_secs(i.data.datetime.try_into().unwrap()); let d = UNIX_EPOCH + Duration::from_secs(i.data.datetime.try_into().unwrap());
let datetime = DateTime::<Utc>::from(d); let datetime = DateTime::<Utc>::from(d);
let timestamp_str = datetime.format("%Y-%m-%d %H:%M:%S").to_string(); let timestamp_str = datetime.format("%Y-%m-%d %H:%M:%S").to_string();
if i.data.title != None { if i.data.title != None {
info!( println!(
"Title {}", "{} {}",
i.data.title.unwrap_or_else(|| "unknown".to_string()) "title".green(),
i.data
.title
.unwrap_or_else(|| "unknown".to_string())
.magenta()
); );
} }
if i.data.description != None { if i.data.description != None {
info!( println!(
"Description {}", "{} {}",
i.data.description.unwrap_or_else(|| "unknown".to_string()) "description".green(),
i.data
.description
.unwrap_or_else(|| "unknown".to_string())
.magenta()
); );
} }
if i.data.deletehash != None { if i.data.deletehash != None {
info!( println!(
"Deletehash {}", "{} {}",
i.data.deletehash.unwrap_or_else(|| "unknown".to_string()) "deletehash".green(),
i.data
.deletehash
.unwrap_or_else(|| "unknown".to_string())
.magenta()
); );
} }
info!("ID {}", i.data.id); println!("{} {}", "id".green(), i.data.id.magenta());
info!("Upload Date {} (UTC)", timestamp_str); println!(
info!("Type {}", i.data.img_type); "{} {} {}",
info!("Width {}", i.data.width); "upload date".green(),
info!("Height {}", i.data.height); timestamp_str.magenta(),
info!("File Size {} KB", i.data.size / 1000); "(UTC)".blue()
info!("Views {}", i.data.views); );
info!("Bandwidth {}", i.data.bandwidth); println!("{} {}", "type".green(), i.data.img_type.magenta());
info!("Link {}", i.data.link); println!("{} {}", "width".green(), i.data.width.to_string().magenta());
println!(
let config = parse(); "{} {}",
"height".green(),
if notify && config.notification.enabled { i.data.height.to_string().magenta()
Notification::new() );
.summary("Imgurs") println!(
.body(&format!("Uploaded {}", i.data.link)) "{} {} {}",
.show() "size".green(),
.expect("send notification"); (i.data.size / 1000).to_string().magenta(),
"KB".blue()
let mut clipboard = Clipboard::new().unwrap(); );
clipboard.set_text(i.data.link.into()).unwrap(); println!("{} {}", "views".green(), i.data.views.to_string().magenta());
} println!(
"{} {}",
"bandwidth".green(),
i.data.bandwidth.to_string().magenta()
);
println!("{} {}", "link".green(), i.data.link.magenta());
} }

View File

@ -1,6 +1,6 @@
use imgurs::api::ImgurClient; use imgurs::api::ImgurClient;
use clap::{App, AppSettings, IntoApp, Parser, Subcommand}; use clap::{Command, IntoApp, Parser, Subcommand};
use clap_complete::{generate, Generator, Shell}; use clap_complete::{generate, Generator, Shell};
use log::error; use log::error;
use std::io::stdout; use std::io::stdout;
@ -26,29 +26,20 @@ enum Commands {
#[clap(about = "Print API Rate Limit")] #[clap(about = "Print API Rate Limit")]
Credits, Credits,
#[clap( #[clap(about = "Upload image to Imgur")]
setting(AppSettings::ArgRequiredElseHelp),
about = "Upload image to Imgur"
)]
Upload { path: String }, Upload { path: String },
#[clap( #[clap(about = "Delete image from Imgur")]
setting(AppSettings::ArgRequiredElseHelp),
about = "Delete image from Imgur"
)]
Delete { delete_hash: String }, Delete { delete_hash: String },
#[clap(setting(AppSettings::ArgRequiredElseHelp), about = "Print image info")] #[clap(about = "Print image info")]
Info { id: String }, Info { id: String },
#[clap( #[clap(about = "Print shell completions (bash, zsh, fish, powershell)")]
setting(AppSettings::ArgRequiredElseHelp),
about = "Print shell completions (bash, zsh, fish, powershell)"
)]
Completions { shell: String }, Completions { shell: String },
} }
fn print_completions<G: Generator>(gen: G, app: &mut App) { fn print_completions<G: Generator>(gen: G, app: &mut Command) {
generate(gen, app, app.get_name().to_string(), &mut stdout()) generate(gen, app, app.get_name().to_string(), &mut stdout())
} }
@ -73,7 +64,7 @@ pub async fn parse(client: ImgurClient) {
} }
Commands::Completions { shell } => { Commands::Completions { shell } => {
let mut app = Cli::into_app(); let mut app = Cli::command();
match shell.as_str() { match shell.as_str() {
"bash" => print_completions(Shell::Bash, &mut app), "bash" => print_completions(Shell::Bash, &mut app),

View File

@ -1,10 +1,22 @@
use super::clipboard::set_clipboard;
use imgurs::api::{upload_image::upload_image as upload_img, ImgurClient}; use imgurs::api::{upload_image::upload_image as upload_img, ImgurClient};
use notify_rust::Notification;
use crate::config::toml::parse as parse_config;
use super::print_image_info; use super::print_image_info;
use base64::encode as base64_encode; use base64::encode as base64_encode;
use std::{fs::read as fs_read, path::Path}; use std::{fs::read as fs_read, path::Path};
macro_rules! notify (
($notification: expr) => (
if parse_config().notification.enabled {
$notification.show().expect("send notification");
}
);
);
pub async fn upload_image(client: ImgurClient, path: &str) { pub async fn upload_image(client: ImgurClient, path: &str) {
let mut image: String = path.to_string(); let mut image: String = path.to_string();
@ -18,5 +30,13 @@ pub async fn upload_image(client: ImgurClient, path: &str) {
} }
let i = upload_img(client, &image).await.expect("send api request"); let i = upload_img(client, &image).await.expect("send api request");
print_image_info(i, true); print_image_info(i.clone());
let body = format!("Uploaded {}", i.data.link);
notify!(Notification::new().summary("Imgurs").body(&body));
if parse_config().clipboard.enabled {
set_clipboard(i.data.link)
}
} }

View File

@ -6,6 +6,7 @@ pub mod toml;
pub struct Config { pub struct Config {
pub imgur: ConfigImgur, pub imgur: ConfigImgur,
pub notification: ConfigNotification, pub notification: ConfigNotification,
pub clipboard: ConfigClipboard,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -17,3 +18,8 @@ pub struct ConfigImgur {
pub struct ConfigNotification { pub struct ConfigNotification {
pub enabled: bool, pub enabled: bool,
} }
#[derive(Debug, Deserialize)]
pub struct ConfigClipboard {
pub enabled: bool,
}