From 4f9470f42410a85305e264cbb571d2e1316493a6 Mon Sep 17 00:00:00 2001 From: Ferrah Aiko Wolf Date: Fri, 7 Jul 2023 17:13:09 -0300 Subject: [PATCH] notify error --- Cargo.toml | 2 +- PKGBUILD | 2 +- src/cli/mod.rs | 32 ++++++++++----- src/console.rs | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +- src/utils.rs | 93 +----------------------------------------- 6 files changed, 135 insertions(+), 104 deletions(-) create mode 100644 src/console.rs diff --git a/Cargo.toml b/Cargo.toml index 2fde54d..5f3d5db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "avif-converter" description = "Simple tool to batch convert multiple images to AVIF" authors = ["Ferrah Aiko "] license = "MIT" -version = "1.7.2" +version = "1.8.0" edition = "2021" readme = "" diff --git a/PKGBUILD b/PKGBUILD index 22f3d63..b3037fb 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,5 +1,5 @@ pkgname=avif-converter-git -pkgver=1.7.2 +pkgver=1.8.0 pkgrel=1 source=("git+https://git.solstice-x0.arpa/FerrahWolfeh/avif-converter.git") sha256sums=('SKIP') diff --git a/src/cli/mod.rs b/src/cli/mod.rs index ce0bc3b..f4f1506 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -11,8 +11,8 @@ use threadpool::ThreadPool; use clap::Parser; +use crate::console::ConsoleMsg; use crate::name_fun::Name; -use crate::ConsoleMsg; use color_eyre::eyre::{bail, Result}; mod commands; @@ -97,14 +97,23 @@ impl Args { pub fn run_conv(self) -> Result<()> { let console = ConsoleMsg::new(self.quiet, self.notify); + let error_con = ConsoleMsg::new(self.quiet, self.notify); - if self.path.is_dir() { - self.dir_conv(console) - } else if self.path.is_file() { - self.single_file_conv(console) - } else { - bail!("Unsupported operation") + let u = { + if self.path.is_dir() { + self.dir_conv(console) + } else if self.path.is_file() { + self.single_file_conv(console) + } else { + bail!("Unsupported operation") + } + }; + + if let Err(error) = u { + error_con.notify_error(&error.to_string())?; } + + Ok(()) } fn dir_conv(self, console: ConsoleMsg) -> Result<()> { @@ -244,6 +253,7 @@ impl Args { fn single_file_conv(self, console: ConsoleMsg) -> Result<()> { let mut console = console; let mut image = ImageFile::new_from_path(&self.path)?; + let image_size = image.metadata.size; console.print_message(format!( "Encoding single file {} ({})", @@ -270,14 +280,18 @@ impl Args { image.save_avif(self.output_file, self.name_type, self.keep)?; } + let bmp = image.bitmap.clone(); + + drop(image); + console.notify_image( &format!( "Finished in {:.2?} \n {} → {}", start.elapsed(), - ByteSize::b(image.metadata.size).to_string_as(true), + ByteSize::b(image_size).to_string_as(true), ByteSize::b(fsz).to_string_as(true) ), - &image.bitmap, + bmp, )?; console.finish_spinner(&format!( diff --git a/src/console.rs b/src/console.rs new file mode 100644 index 0000000..f0b456e --- /dev/null +++ b/src/console.rs @@ -0,0 +1,107 @@ +use std::time::Duration; + +use color_eyre::Result; +use image::{imageops::FilterType, DynamicImage}; +use notify_rust::{Image, Notification}; +use spinoff::{spinners, Color, Spinner, Streams}; + +use crate::utils::PROGRESS_BAR; + +pub struct ConsoleMsg { + spinner: Option, + quiet: bool, + notify: bool, +} + +impl ConsoleMsg { + #[must_use] + pub fn new(quiet: bool, notify: bool) -> Self { + Self { + spinner: None, + quiet, + notify, + } + } + + pub fn set_spinner(&mut self, message: &'static str) { + if !self.quiet { + let spinner = + Spinner::new_with_stream(spinners::Dots, message, Color::Green, Streams::Stderr); + + self.spinner = Some(spinner); + } + } + + pub fn finish_spinner(mut self, message: &str) -> Self { + if let Some(spin) = self.spinner { + spin.success(message); + self.spinner = None + } + + self + } + + pub fn print_message(&self, message: String) { + if !self.quiet { + println!("{message}"); + } + } + + pub fn setup_bar(&self, len: u64) { + if !self.quiet { + PROGRESS_BAR.set_length(len); + + PROGRESS_BAR.enable_steady_tick(Duration::from_millis(100)); + } + } + + pub fn finish_bar(&self) { + if !self.quiet { + PROGRESS_BAR.finish_and_clear(); + } + } + + pub fn notify_text(&self, message: &str) -> Result<()> { + if self.notify { + Notification::new() + .appname("AVIF Converter") + .summary("Conversion completed") + .body(message) + .icon("folder") + .show()?; + } + + Ok(()) + } + + pub fn notify_image(&self, message: &str, image: DynamicImage) -> Result<()> { + let img = image.resize(512, 512, FilterType::Nearest); + + if self.notify { + Notification::new() + .appname("AVIF Converter") + .summary("Conversion Completed") + .body(message) + .image_data(Image::from_rgba( + img.width() as i32, + img.height() as i32, + img.to_rgba8().into_vec(), + )?) + .show()?; + } + + Ok(()) + } + + pub fn notify_error(&self, message: &str) -> Result<()> { + if self.notify { + Notification::new() + .appname("AVIF Converter") + .summary("Conversion Failed") + .body(message) + .show()?; + } + + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index 58ffa21..4be2ba4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,10 @@ use cli::Args; use color_eyre::eyre::Result; -use utils::{search_dir, ConsoleMsg}; +use utils::search_dir; mod cli; +mod console; mod encoders; mod image_file; mod name_fun; diff --git a/src/utils.rs b/src/utils.rs index 58b91a4..50a6c99 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,104 +1,13 @@ -use std::{fmt::Write, fs, path::Path, time::Duration}; +use std::{fmt::Write, fs, path::Path}; -use color_eyre::Result; -use image::DynamicImage; use indicatif::{ProgressBar, ProgressState, ProgressStyle}; -use notify_rust::{Image, Notification}; use once_cell::sync::Lazy; -use spinoff::{spinners, Color, Spinner, Streams}; use crate::image_file::ImageFile; pub static PROGRESS_BAR: Lazy = Lazy::new(|| ProgressBar::new(0).with_style(bar_style())); -pub struct ConsoleMsg { - spinner: Option, - quiet: bool, - notify: bool, -} - -impl ConsoleMsg { - #[must_use] - pub fn new(quiet: bool, notify: bool) -> Self { - Self { - spinner: None, - quiet, - notify, - } - } - - pub fn set_spinner(&mut self, message: &'static str) { - if !self.quiet { - let spinner = - Spinner::new_with_stream(spinners::Dots, message, Color::Green, Streams::Stderr); - - self.spinner = Some(spinner); - } - } - - pub fn finish_spinner(mut self, message: &str) -> Self { - if let Some(spin) = self.spinner { - spin.success(message); - self.spinner = None - } - - self - } - - pub fn print_message(&self, message: String) { - if !self.quiet { - println!("{message}"); - } - } - - pub fn setup_bar(&self, len: u64) { - if !self.quiet { - PROGRESS_BAR.set_length(len); - - PROGRESS_BAR.enable_steady_tick(Duration::from_millis(100)); - } - } - - pub fn finish_bar(&self) { - if !self.quiet { - PROGRESS_BAR.finish_and_clear(); - } - } - - pub fn notify_text(&self, message: &str) -> Result<()> { - if self.notify { - Notification::new() - .appname("AVIF Converter") - .summary("Conversion completed") - .body(message) - .icon("folder") - .show()?; - } - - Ok(()) - } - - pub fn notify_image(&self, message: &str, image: &DynamicImage) -> Result<()> { - let img = image.resize(512, 512, image::imageops::FilterType::Nearest); - - if self.notify { - Notification::new() - .appname("AVIF Converter") - .summary("Conversion Completed") - .body(message) - .image_data(Image::from_rgba( - img.width() as i32, - img.height() as i32, - img.to_rgba8().into_vec(), - )?) - .show()?; - } - - Ok(()) - } -} - pub fn search_dir(dir: &Path) -> Vec { let paths = fs::read_dir(dir).unwrap();