stdin read

This commit is contained in:
2024-07-07 15:41:53 +02:00
parent e0f043bb77
commit 2e87792a7f
5 changed files with 87 additions and 2 deletions

49
Cargo.lock generated
View File

@@ -61,11 +61,23 @@ dependencies = [
name = "ascii-gen" name = "ascii-gen"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"atty",
"clap", "clap",
"colored", "colored",
"image", "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]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.3.0" version = "1.3.0"
@@ -280,6 +292,15 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" 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]] [[package]]
name = "image" name = "image"
version = "0.24.9" version = "0.24.9"
@@ -325,6 +346,12 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8" checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8"
[[package]]
name = "libc"
version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.12" version = "0.4.12"
@@ -487,6 +514,28 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082" 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]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.48.0" version = "0.48.0"

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
atty = "0.2.14"
clap = { version = "4.5.8", features = ["derive"] } clap = { version = "4.5.8", features = ["derive"] }
colored = "2.1.0" colored = "2.1.0"
image = "0.24.9" image = "0.24.9"

View File

@@ -3,7 +3,7 @@ use clap::Parser;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
pub struct Args { pub struct Args {
#[arg(short, long)] #[arg(short, long, default_value_t = String::from(""))]
pub image: String, pub image: String,
#[arg(long, default_value_t = false)] #[arg(long, default_value_t = false)]

View File

@@ -1,5 +1,28 @@
use image::io::Reader;
use image; use image;
use image::GenericImageView; use image::GenericImageView;
use std::io::{self, BufReader, BufRead, Cursor};
use atty::Stream;
pub fn load_image_from_stdin() -> Result<image::DynamicImage, image::ImageError> {
let mut buffer: Vec<u8> = Vec::new();
let mut raw_reader: Box<dyn BufRead> = 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 { pub fn load_image(file_name: &str) -> image::DynamicImage {
let img = image::open(file_name).expect("File not found!"); let img = image::open(file_name).expect("File not found!");

View File

@@ -5,8 +5,20 @@ fn main() {
println!("ASCII Generator\n----------------"); println!("ASCII Generator\n----------------");
let args = libs::args::Args::parse(); 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); libs::image::print_size(&img);
if !args.noresize { if !args.noresize {