Skip to content

Commit

Permalink
notify error
Browse files Browse the repository at this point in the history
  • Loading branch information
FerrahWolfeh committed Jul 7, 2023
1 parent 9104392 commit 4f9470f
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 104 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "avif-converter"
description = "Simple tool to batch convert multiple images to AVIF"
authors = ["Ferrah Aiko <[email protected]>"]
license = "MIT"
version = "1.7.2"
version = "1.8.0"
edition = "2021"
readme = ""

Expand Down
2 changes: 1 addition & 1 deletion PKGBUILD
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
32 changes: 23 additions & 9 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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 {} ({})",
Expand All @@ -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!(
Expand Down
107 changes: 107 additions & 0 deletions src/console.rs
Original file line number Diff line number Diff line change
@@ -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<Spinner>,
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(())
}
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
93 changes: 1 addition & 92 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<ProgressBar> =
Lazy::new(|| ProgressBar::new(0).with_style(bar_style()));

pub struct ConsoleMsg {
spinner: Option<Spinner>,
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<ImageFile> {
let paths = fs::read_dir(dir).unwrap();

Expand Down

0 comments on commit 4f9470f

Please sign in to comment.