From dcbae98ed3211be8fb10348bcbf4df57a0dd8b38 Mon Sep 17 00:00:00 2001 From: Rico Riedel Date: Wed, 3 Aug 2022 17:33:15 +0200 Subject: [PATCH] Implement main function --- Cargo.lock | 228 +++++++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 2 + src/main.rs | 147 ++++++++++++++++++++++++++++++++- 3 files changed, 362 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b247b43..5a92595 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,17 @@ dependencies = [ "num-traits", ] +[[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" @@ -38,6 +49,45 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "3.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3dbbb6653e7c55cc8595ad3e1f7be8f32aba4eb7ff7f0fd1163d4f3d137c0a9" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ba52acd3b0a5c33aeada5cdaa3267cdc7c594a98731d4268cdc1532f4264cb4" +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 = "convert_case" version = "0.4.0" @@ -115,6 +165,48 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85dcb89d2b10c5f6133de2efd8c11959ce9dbb46a2f7a4cab208c4eeda6ce1ab" +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[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 = "itertools" version = "0.10.3" @@ -215,6 +307,18 @@ dependencies = [ "autocfg", ] +[[package]] +name = "once_cell" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" + +[[package]] +name = "os_str_bytes" +version = "6.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "648001efe5d5c0102d8cea768e348da85d90af8ba91f0bea908f157951493cd4" + [[package]] name = "parking_lot" version = "0.12.1" @@ -238,6 +342,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + [[package]] name = "predicates" version = "2.1.1" @@ -269,23 +379,77 @@ dependencies = [ ] [[package]] -name = "proc-macro2" -version = "1.0.42" +name = "proc-macro-error" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c278e965f1d8cf32d6e0e96de3d3e79712178ae67986d9cf9151f51e95aac89b" +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.20" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + [[package]] name = "redox_syscall" version = "0.2.16" @@ -329,9 +493,9 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "semver" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2333e6df6d6598f2b1974829f853c2b4c5f4a6e503c10af918081aa6f8564e1" +checksum = "93f6841e709003d68bb2deee8c343572bf446003ec20a583e76f7b15cebf3711" [[package]] name = "signal-hook" @@ -370,16 +534,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" [[package]] -name = "syn" -version = "1.0.98" +name = "strsim" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +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 = "termtree" version = "0.2.4" @@ -387,10 +566,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b" [[package]] -name = "unicode-ident" -version = "1.0.2" +name = "textwrap" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15c61ba63f9235225a22310255a29b806b907c9b8c964bcbd0a2c70f3f2deea7" +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" @@ -414,6 +605,15 @@ 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" @@ -468,7 +668,9 @@ name = "wipe" version = "2.0.0" dependencies = [ "approx", + "clap", "crossterm", "derive_more", "mockall", + "rand", ] diff --git a/Cargo.toml b/Cargo.toml index ab6fd1b..b502112 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,10 @@ repository = "https://github.com/ricoriedel/wipe" authors = ["Rico Riedel"] [dependencies] +clap = { version = "3.2", features = ["derive"] } crossterm = "0.24" derive_more = "0.99" +rand = "0.8" [dev-dependencies] mockall = "0.11" diff --git a/src/main.rs b/src/main.rs index c2c25e8..4198e58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,149 @@ pub use term::*; pub use timer::*; pub use vec::*; -fn main() -> Result<(), Error> { - Ok(()) +use crate::convert::*; +use crate::pattern::*; +use clap::{Parser, ValueEnum}; +use crossterm::style::Color; +use crossterm::style::Color::*; +use rand::prelude::*; +use std::io::stdout; +use std::time::Duration; + +#[derive(Parser)] +#[clap( + author = env!("CARGO_PKG_AUTHORS"), + version = env!("CARGO_PKG_VERSION"), + about = env!("CARGO_PKG_DESCRIPTION"), +)] +struct Args { + #[clap(long, default_value_t = 1000)] + duration: u64, + #[clap(long, default_value_t = 60)] + fps: u64, + #[clap(long, default_value = ".:+#")] + chars: String, + #[clap(long, value_enum)] + char_pattern: Vec, + #[clap(long, value_enum)] + colors: Vec, + #[clap(long, value_enum)] + color_pattern: Vec, +} + +#[derive(ValueEnum, Clone)] +enum PalletEnum { + Red, + Yellow, + Green, + Blue, + Magenta, + Cyan, + Rainbow, + + DarkRed, + DarkYellow, + DarkGreen, + DarkBlue, + DarkMagenta, + DarkCyan, + DarkRainbow, + + RedYellow, + YellowGreen, + GreenBlue, + BlueCyan, + CyanMagenta, + MagentaRed, + + Gray, +} + +#[derive(ValueEnum, Clone)] +enum PatternEnum { + Circle, + Line, + Rhombus, + Wheel, +} + +fn main() -> Result<(), Error> { + let args = Args::parse(); + let rand = &mut thread_rng(); + + let char: Box = create_pattern(choose(args.char_pattern, rand)); + let color: Box = create_pattern(choose(args.color_pattern, rand)); + let pallet = create_pallet(choose(args.colors, rand)); + + let sampler = SamplerFactoryImpl::new(char, color); + let char_converter = CharConverterImpl::new(args.chars); + let color_converter = ColorConverterImpl::new(pallet); + let converter = ConverterImpl::new(char_converter, color_converter); + let term = TerminalImpl::new(stdout()); + let printer = PrinterImpl::new(term)?; + let renderer = RendererImpl::new(sampler, converter, printer); + + let clock = ClockImpl::default(); + let duration = Duration::from_millis(args.duration); + let delay = Duration::from_nanos(1_000_000_000 / args.fps); + let timer = Timer::new(clock, duration, delay); + + timer.run(renderer) +} + +fn choose(options: Vec, rand: &mut TRand) -> TValue { + if options.is_empty() { + TValue::value_variants() + .iter() + .choose(rand) + .unwrap() + .clone() + } else { + options.iter().choose(rand).unwrap().clone() + } +} + +fn create_pattern(pattern: PatternEnum) -> Box { + match pattern { + PatternEnum::Circle => Box::new(CircleFactory::default()), + PatternEnum::Line => Box::new(LineFactory::default()), + PatternEnum::Rhombus => Box::new(RhombusFactory::default()), + PatternEnum::Wheel => Box::new(WheelFactory::default()), + } +} + +fn create_pallet(colors: PalletEnum) -> Vec { + match colors { + PalletEnum::Red => vec![DarkRed, Red, White], + PalletEnum::Yellow => vec![DarkYellow, Yellow, White], + PalletEnum::Green => vec![DarkGreen, Green, White], + PalletEnum::Blue => vec![DarkBlue, Blue, White], + PalletEnum::Magenta => vec![DarkMagenta, Magenta, White], + PalletEnum::Cyan => vec![DarkCyan, Cyan, White], + PalletEnum::Rainbow => vec![Red, Yellow, Green, Blue, Cyan, Magenta], + + PalletEnum::DarkRed => vec![Black, DarkRed, Red], + PalletEnum::DarkYellow => vec![Black, DarkYellow, Yellow], + PalletEnum::DarkGreen => vec![Black, DarkGreen, Green], + PalletEnum::DarkBlue => vec![Black, DarkBlue, Blue], + PalletEnum::DarkMagenta => vec![Black, DarkMagenta, Magenta], + PalletEnum::DarkCyan => vec![Black, DarkCyan, Cyan], + PalletEnum::DarkRainbow => vec![ + DarkRed, + DarkYellow, + DarkGreen, + DarkBlue, + DarkCyan, + DarkMagenta, + ], + + PalletEnum::RedYellow => vec![Red, DarkRed, DarkYellow, Yellow], + PalletEnum::YellowGreen => vec![Yellow, DarkYellow, DarkGreen, Green], + PalletEnum::GreenBlue => vec![Green, DarkGreen, DarkBlue, Blue], + PalletEnum::BlueCyan => vec![Blue, DarkBlue, DarkCyan, Cyan], + PalletEnum::CyanMagenta => vec![Cyan, DarkCyan, DarkMagenta, Magenta], + PalletEnum::MagentaRed => vec![Magenta, DarkMagenta, DarkRed, Red], + + PalletEnum::Gray => vec![Black, DarkGrey, Grey, White], + } }