mirror of
https://github.com/eRgo35/ascii.git
synced 2026-02-04 04:46:09 +01:00
stdin read
This commit is contained in:
49
Cargo.lock
generated
49
Cargo.lock
generated
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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<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 {
|
||||
let img = image::open(file_name).expect("File not found!");
|
||||
|
||||
14
src/main.rs
14
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 {
|
||||
|
||||
Reference in New Issue
Block a user