diff options
author | Jacob Janzen <jacob.a.s.janzen@gmail.com> | 2022-08-23 12:05:47 -0500 |
---|---|---|
committer | Jacob Janzen <jacob.a.s.janzen@gmail.com> | 2022-08-23 12:05:47 -0500 |
commit | c463fb7d0ab36c44046a5a7edc88902f5b90e710 (patch) | |
tree | 532549c913d9bdcd49c6ef6804700441a49d1eb1 | |
parent | 1c08e24cabe887765d2d5de0c8ce20f63fa83792 (diff) |
added proper cmd args
-rw-r--r-- | Cargo.lock | 223 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | output.gif | bin | 464257 -> 551037 bytes | |||
-rw-r--r-- | src/lib.rs | 139 | ||||
-rw-r--r-- | src/main.rs | 14 |
5 files changed, 290 insertions, 87 deletions
@@ -3,9 +3,33 @@ version = 3 [[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.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] name = "bubbles" version = "0.1.0" dependencies = [ + "clap", "gif", "rand", ] @@ -17,6 +41,45 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] +name = "clap" +version = "3.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29e724a68d9319343bb3328c9cc2dfde263f4b3142ee1059a9980580171c954b" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13547f7012c01ab4a0e8f8967730ada8f9fdf419e8b6c792788f39cf4e46eefa" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + +[[package]] name = "color_quant" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -44,18 +107,103 @@ dependencies = [ ] [[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] name = "libc" version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" [[package]] +name = "once_cell" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" + +[[package]] +name = "os_str_bytes" +version = "6.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" + +[[package]] name = "ppv-lite86" version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" [[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] name = "rand" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -86,6 +234,50 @@ dependencies = [ ] [[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -96,3 +288,34 @@ name = "weezl" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb" + +[[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-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" @@ -8,3 +8,4 @@ edition = "2021" [dependencies] gif = "0.11.4" rand = "0.8.5" +clap = { version = "3.2.17", features = ["derive"]} Binary files differ@@ -15,53 +15,22 @@ use rand::Rng; use std::vec; -pub struct Config { +use clap::Parser; + +pub struct Gif { pub height: u16, pub width: u16, pub frames: u16, - pub out_file: String, + pub pixels: Vec<u8>, + point_data: Vec<PointData>, + cross_distance: f64, + points: Vec<Point>, } -impl Config { - pub fn build(mut args: impl Iterator<Item = String>) -> Result<Self, &'static str> { - args.next(); - - let width: u16 = match args.next() { - Some(arg) => match arg.parse() { - Ok(num) => num, - Err(_) => return Err("Width is not a number"), - }, - None => return Err("Didn't get a width"), - }; - - let height: u16 = match args.next() { - Some(arg) => match arg.parse() { - Ok(num) => num, - Err(_) => return Err("Height is not a number"), - }, - None => return Err("Didn't get a height"), - }; - - let frames: u16 = match args.next() { - Some(arg) => match arg.parse() { - Ok(num) => num, - Err(_) => return Err("Frames is not a number"), - }, - None => return Err("Didn't get a frame count"), - }; - - let out_file = match args.next() { - Some(arg) => arg, - None => return Err("Didn't get an output file"), - }; - - Ok(Config { - height, - width, - frames, - out_file, - }) - } +#[derive(Clone)] +struct Point { + pub x: u16, + pub y: u16, } #[derive(Clone)] @@ -70,65 +39,79 @@ struct PointData { closest_point: Point, } -impl PointData { - fn get_point_data(gif: &Gif, p: Point) -> Self { - let mut pd = PointData { - min_dist: gif.cross_distance, - closest_point: Point { x: 0, y: 0 }, - }; +#[derive(Parser, Debug)] +#[clap(author, version, about, long_about = None)] +pub struct Args { + /// width of the image + #[clap(short, long, value_parser)] + pub width: u16, - for point in &gif.points { - let d = distance(&p, point); - if d < pd.min_dist { - pd.min_dist = d; - pd.closest_point = point.clone(); - } - } + /// height of the image + #[clap(short, long, value_parser)] + pub height: u16, - pd - } + /// number of gif frames + #[clap(short, long, value_parser)] + pub frames: u16, + + /// number of cells to generate + #[clap(short, long, value_parser)] + pub num_cells: usize, + + /// output file + #[clap(short, long, value_parser)] + pub out: String, } -pub struct Gif { - pub height: u16, - pub width: u16, - pub frames: u16, - pub pixels: Vec<u8>, - point_data: Vec<PointData>, - cross_distance: f64, - points: Vec<Point>, +impl Args { + pub fn read() -> Self { + Args::parse() + } } impl Gif { - pub fn create_from_config(config: &Config, num_cells: usize) -> Self { + pub fn create_from_args(args: &Args) -> Self { Gif { - height: config.height, - width: config.width, - frames: config.frames, - pixels: vec![0; config.height as usize * config.width as usize * 3], + height: args.height, + width: args.width, + frames: args.frames, + pixels: vec![0; args.height as usize * args.width as usize * 3], point_data: vec![ PointData { min_dist: 0.0, closest_point: Point { x: 0, y: 0 } }; - config.height as usize * config.width as usize + args.height as usize * args.width as usize ], cross_distance: distance( &Point { x: 0, y: 0 }, &Point { - x: config.width - 1, - y: config.height - 1, + x: args.width - 1, + y: args.height - 1, }, ), - points: generate_points(config.width, config.height, num_cells), + points: generate_points(args.width, args.height, args.num_cells), } } } -#[derive(Clone)] -struct Point { - pub x: u16, - pub y: u16, +impl PointData { + fn get_point_data(gif: &Gif, p: Point) -> Self { + let mut pd = PointData { + min_dist: gif.cross_distance, + closest_point: Point { x: 0, y: 0 }, + }; + + for point in &gif.points { + let d = distance(&p, point); + if d < pd.min_dist { + pd.min_dist = d; + pd.closest_point = point.clone(); + } + } + + pd + } } pub fn fill_canvas(gif: &mut Gif) { diff --git a/src/main.rs b/src/main.rs index 8fcac37..59a14c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,25 +12,21 @@ You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. */ -use std::env; use std::fs::File; use std::process; -use bubbles::Config; +use bubbles::Args; use bubbles::Gif; fn main() { - let config = Config::build(env::args()).unwrap_or_else(|err| { - eprintln!("Error printing arguments: {err}"); - process::exit(1); - }); + let args = Args::read(); // create Gif data - let mut gif = Gif::create_from_config(&config, 100); + let mut gif = Gif::create_from_args(&args); // Create encoder - let mut image = File::create(config.out_file).unwrap(); - let mut encoder = gif::Encoder::new(&mut image, config.width, config.height, &[]).unwrap(); + let mut image = File::create(args.out).unwrap(); + let mut encoder = gif::Encoder::new(&mut image, args.width, args.height, &[]).unwrap(); // Repeat infinitely if let Err(_) = encoder.set_repeat(gif::Repeat::Infinite) { |