From 84014a9ec486098d8104534471297717c01449b6 Mon Sep 17 00:00:00 2001 From: Rhelvetican <113826858+Rhelvetican@users.noreply.github.com> Date: Wed, 15 May 2024 23:43:17 +0700 Subject: [PATCH] feat: Add argument for specifying output path. --- Cargo.lock | 3 ++- Cargo.toml | 11 ++++++++++- README.md | 6 ++++-- src/error.rs | 4 +--- src/main.rs | 12 ++++++------ src/utils/cli.rs | 4 ++++ src/utils/mod.rs | 28 ++++++++++++++++++++-------- 7 files changed, 47 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 863e294..d8dd8da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -433,8 +433,9 @@ dependencies = [ [[package]] name = "glassy" -version = "0.1.1" +version = "0.1.2" dependencies = [ + "anyhow", "clap", "env_logger", "image", diff --git a/Cargo.toml b/Cargo.toml index e180b30..4db4a78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,11 @@ [package] name = "glassy" authors = ["decipher", "Rhelvetican"] -version = "0.1.1" +version = "0.1.2" edition = "2021" [dependencies] +anyhow = "1" image = "0.25" imageproc = "0.24" thiserror = "1.0" @@ -12,3 +13,11 @@ clap = { version = "4.5", features = ["derive"] } libblur = "0.9" log = "0.4" env_logger = "0.11" + +[profile.release] +lto = "fat" +opt-level = 3 +codegen-units = 1 +rpath = true +debug = false +panic = "abort" diff --git a/README.md b/README.md index c855954..8212f0d 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,12 @@ It adds variable blur and noise to image as overlay to give them a glassy look. ## Usage -``` +```sh ~ $ glassy -h A simple CLI tool to apply glass-like overlay effect to images -Usage: glassy [OPTIONS] +Usage: glassy.exe [OPTIONS] Arguments: Path to image file @@ -20,6 +20,8 @@ Options: Strength of the glass effect [default: medium] [possible values: low, medium, high] --no-grain Apply effect without grain + -o, --output + Specify output file path -v, --verbose Explain what is being done -h, --help diff --git a/src/error.rs b/src/error.rs index 1044228..2074904 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,5 @@ use image::ImageError as ImgError; -use std::{io::Error as IoError, result::Result as StdResult}; +use std::io::Error as IoError; use thiserror::Error; #[derive(Error, Debug)] @@ -9,5 +9,3 @@ pub enum Error { #[error("Invalid image file")] DecodeError(#[from] ImgError), } - -pub type Result = StdResult; diff --git a/src/main.rs b/src/main.rs index 9b2dc2c..da0db76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ -use clap::Parser; - use crate::utils::{cli::CliArgs, proc_image}; -use std::process; +use clap::Parser; +use log::error; +use std::process::exit; -mod utils; mod error; mod logger; +mod utils; fn main() { let cli_args: CliArgs = CliArgs::parse(); @@ -13,7 +13,7 @@ fn main() { logger::init_logger(&cli_args); if let Err(e) = proc_image(cli_args) { - log::error!("{e}"); - process::exit(1); + error!("{e}"); + exit(1); }; } diff --git a/src/utils/cli.rs b/src/utils/cli.rs index ceb266d..d2cd54b 100644 --- a/src/utils/cli.rs +++ b/src/utils/cli.rs @@ -25,6 +25,10 @@ pub struct CliArgs { #[arg(long = "no-grain")] pub no_grain: bool, + /// Specify output file path + #[arg(short, long, value_name = "PATH", value_hint = ValueHint::FilePath)] + pub output: Option, + /// Explain what is being done #[arg(short, long)] pub verbose: bool, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 1198553..7bde442 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,9 +1,9 @@ -use crate::error::Result; +use anyhow::Result; use self::{blur::add_blur, noise::add_noise}; use image::io::Reader; use log::info; -use std::path::Path; +use std::{fs::DirBuilder, path::Path}; mod blur; pub mod cli; @@ -27,12 +27,24 @@ pub fn proc_image(cli_args: cli::CliArgs) -> Result<()> { add_noise(img, noise_opts) }; - let output_path = format!( - "{}_{}.{}", - path.file_stem().unwrap().to_str().unwrap_or("Image"), - cli_args.effect_strength, - path.extension().unwrap().to_str().unwrap_or("png") - ); + let output_path = if let Some(output) = cli_args.output { + output + } else { + format!( + "{}_{}.{}", + path.file_stem().unwrap().to_str().unwrap_or("Image"), + cli_args.effect_strength, + path.extension().unwrap().to_str().unwrap_or("png") + ) + }; + + // Create the output directory if it doesn't exist + let output = Path::new(output_path.as_str()); + if !output.exists() { + DirBuilder::new() + .recursive(true) + .create(output.parent().unwrap())?; + } info!("Saving Image: {output_path}");