diff --git a/Cargo.lock b/Cargo.lock index 845dbb4..ccba25d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,11 +61,23 @@ dependencies = [ name = "ascii-gen" version = "0.1.0" dependencies = [ + "atty", "clap", "colored", "image", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.3.0" @@ -280,6 +292,15 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "image" version = "0.24.9" @@ -325,6 +346,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8" +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + [[package]] name = "lock_api" version = "0.4.12" @@ -487,6 +514,28 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 299a586..0133307 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +atty = "0.2.14" clap = { version = "4.5.8", features = ["derive"] } colored = "2.1.0" image = "0.24.9" diff --git a/src/libs/args.rs b/src/libs/args.rs index 84b6a0f..119549c 100644 --- a/src/libs/args.rs +++ b/src/libs/args.rs @@ -3,7 +3,7 @@ use clap::Parser; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] pub struct Args { - #[arg(short, long)] + #[arg(short, long, default_value_t = String::from(""))] pub image: String, #[arg(long, default_value_t = false)] diff --git a/src/libs/image.rs b/src/libs/image.rs index a3a67b9..ca46544 100644 --- a/src/libs/image.rs +++ b/src/libs/image.rs @@ -1,5 +1,28 @@ +use image::io::Reader; use image; use image::GenericImageView; +use std::io::{self, BufReader, BufRead, Cursor}; +use atty::Stream; + +pub fn load_image_from_stdin() -> Result { + let mut buffer: Vec = Vec::new(); + let mut raw_reader: Box = if atty::is(Stream::Stdin) { + eprintln!("Error: No image provided"); + std::process::exit(1); + } else { + Box::new(BufReader::new(io::stdin())) + }; + + raw_reader.read_to_end(&mut buffer).unwrap(); + + let reader = Reader::new(Cursor::new(buffer)) + .with_guessed_format() + .expect("Failed to read image format"); + + println!("Image loaded: stdin"); + + reader.decode() +} pub fn load_image(file_name: &str) -> image::DynamicImage { let img = image::open(file_name).expect("File not found!"); diff --git a/src/main.rs b/src/main.rs index 0f2a46a..8643616 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,20 @@ fn main() { println!("ASCII Generator\n----------------"); let args = libs::args::Args::parse(); + let path = args.image.clone(); + + let mut img = if !path.is_empty() { + libs::image::load_image(&path) + } else { + match libs::image::load_image_from_stdin() { + Ok(img) => img, + Err(e) => { + eprintln!("Error: {e}"); + std::process::exit(1); + } + } + }; - let mut img = libs::image::load_image(&args.image); libs::image::print_size(&img); if !args.noresize {