diff --git a/Cargo.lock b/Cargo.lock index 0c761e8..1dcc4e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,55 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4aa90d7ce82d4be67b64039a3d588d38dbcc6736577de4a847025ce5b0c468d1" +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "anyhow" version = "1.0.86" @@ -147,12 +196,58 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" + [[package]] name = "color_quant" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + [[package]] name = "console" version = "0.15.8" @@ -393,6 +488,12 @@ dependencies = [ "syn", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itertools" version = "0.12.1" @@ -628,6 +729,7 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" name = "prisma" version = "0.1.0" dependencies = [ + "clap", "image", "indicatif", ] @@ -858,6 +960,12 @@ dependencies = [ "lock_api", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "2.0.69" @@ -965,6 +1073,12 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "v_frame" version = "0.3.8" diff --git a/Cargo.toml b/Cargo.toml index 51aa0c5..34e8bed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] +clap = { version = "4.5.8", features = ["derive"] } image = "0.25.1" indicatif = "0.17.8" diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..5dff30c --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,47 @@ +use clap::Parser; +use std::fmt::{self, Display}; +use std::str::FromStr; + +#[derive(Parser)] +#[command(version)] +pub struct Cli { + /// The image size of the output + #[arg(short, long, default_value_t = Size::new(1024, 768))] + pub size: Size, + + /// The path to the output + #[arg(short, long, default_value_t = String::from("output.png"))] + pub output: String, +} + +#[derive(Clone)] +pub struct Size { + pub width: u32, + pub height: u32, +} + +impl Size { + pub fn new(width: u32, height: u32) -> Self { + Self { width, height } + } +} + +impl FromStr for Size { + type Err = String; + + fn from_str(s: &str) -> Result { + let size: Vec<_> = s.split('x').collect(); + if size.len() != 2 { + return Err(String::from("invalid number of dimensions found in string")); + } + let width = size[0].parse().map_err(|e| format!("{e}"))?; + let height = size[1].parse().map_err(|e| format!("{e}"))?; + Ok(Self::new(width, height)) + } +} + +impl Display for Size { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}x{}", self.width, self.height) + } +} diff --git a/src/main.rs b/src/main.rs index e39b3a8..119ef11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,21 @@ +mod cli; + +use clap::Parser; +use cli::{Cli, Size}; use image::{Rgb, RgbImage}; use indicatif::ProgressBar; fn main() { - let image_width = 256; - let image_height = 256; + let cli = Cli::parse(); + let Size { width, height } = cli.size; - let mut image = RgbImage::new(image_width, image_height); - let progress_bar = ProgressBar::new(image_height as u64); + let mut image = RgbImage::new(width, height); + let progress_bar = ProgressBar::new(height as u64); - for y in 0..image_height { - for x in 0..image_width { - let r = x as f64 / (image_width - 1) as f64; - let g = y as f64 / (image_height - 1) as f64; + for y in 0..height { + for x in 0..width { + let r = x as f64 / (width - 1) as f64; + let g = y as f64 / (height - 1) as f64; let b = 0.0; let r = (255.999 * r) as u8; @@ -23,6 +27,6 @@ fn main() { progress_bar.inc(1); } - image.save("output.png").unwrap(); + image.save(cli.output).unwrap(); progress_bar.finish(); }