From: Huck Boles Date: Sat, 27 May 2023 21:29:23 +0000 (-0500) Subject: added: tui map display X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=504c6dcc528491a9d914126a4fa9f346a14f5287;p=cellseq.git added: tui map display --- diff --git a/Cargo.lock b/Cargo.lock index 19218cd..53878fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,10 +8,18 @@ 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 = "cellseq" version = "0.1.0" dependencies = [ + "crossterm", + "eyre", "ndarray", "rand", ] @@ -22,6 +30,41 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "crossterm" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a84cda67535339806297f1b331d6dd6320470d2a0fe65381e79ee9e156dd3d13" +dependencies = [ + "bitflags", + "crossterm_winapi", + "libc", + "mio", + "parking_lot", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ae1b35a484aa10e07fe0638d02301c5ad24de82d310ccbd2f3693da5f09bf1c" +dependencies = [ + "winapi", +] + +[[package]] +name = "eyre" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb" +dependencies = [ + "indenter", + "once_cell", +] + [[package]] name = "getrandom" version = "0.2.9" @@ -33,12 +76,37 @@ dependencies = [ "wasi", ] +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "libc" version = "0.2.144" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + [[package]] name = "matrixmultiply" version = "0.3.7" @@ -49,6 +117,18 @@ dependencies = [ "rawpointer", ] +[[package]] +name = "mio" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys", +] + [[package]] name = "ndarray" version = "0.15.6" @@ -90,6 +170,35 @@ dependencies = [ "autocfg", ] +[[package]] +name = "once_cell" +version = "1.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -132,8 +241,147 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "signal-hook" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "732768f1176d21d09e076c23a93123d40bba92d50c4058da34d45c8de8e682b9" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + +[[package]] +name = "smallvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[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.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" diff --git a/Cargo.toml b/Cargo.toml index 9b02f26..c5e719c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,5 @@ edition = "2021" [dependencies] ndarray = "0.15" rand = "0.8" +crossterm = "0.26" +eyre = "0.6" diff --git a/src/cells.rs b/src/cells.rs index d7feed9..e5d9b1b 100644 --- a/src/cells.rs +++ b/src/cells.rs @@ -22,7 +22,8 @@ impl Map { } pub fn update(&mut self) { - let previous = self.clone(); + let mut previous = self.clone(); + previous.wrap_walls(); for (next, prev) in self .windows((3, 3)) .into_iter() @@ -39,7 +40,7 @@ impl Map { } } State::Alive => { - if count < 2 || count > 3 { + if count < 3 || count > 4 { cell.set(State::Dead); } } @@ -50,24 +51,23 @@ impl Map { pub fn randomize(&mut self, val: f64) { let mut rng = thread_rng(); for cell in self.map.iter() { - if rng.gen::() < val { + if rng.gen::() > val { cell.set(State::Alive) } else { cell.set(State::Dead) } } + } - let walls = vec![ - self.row(0), - self.row(self.rows - 1), - self.column(0), - self.column(self.cols - 1), - ]; + fn wrap_walls(&mut self) { + for (bottom, top) in self.row(self.rows - 1).iter().zip(self.row(0).iter()) { + bottom.set(State::Dead); + top.set(State::Dead); + } - for wall in walls { - for cell in wall.iter() { - cell.set(State::Dead) - } + for (right, left) in self.column(self.cols - 1).iter().zip(self.column(0).iter()) { + right.set(State::Dead); + left.set(State::Dead); } } } @@ -78,23 +78,3 @@ impl Deref for Map { &self.map } } - -impl Display for Map { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let map: String = self - .outer_iter() - .into_iter() - .map(|row| { - row.iter() - .map(|x| match x.get() { - State::Dead => ' ', - State::Alive => 'o', - }) - .collect::() - }) - .collect::>() - .join("\n"); - - write!(f, "{map}") - } -} diff --git a/src/cli.rs b/src/cli.rs index e69de29..96bda3f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -0,0 +1,45 @@ +use crate::{Map, State}; +use crossterm::{ + cursor, execute, queue, + style::{self, Stylize}, + terminal, Result, +}; +use std::io::{stdout, Write}; + +pub fn draw_frame(map: &Map, origin: (usize, usize)) -> Result<()> { + for y in 1..(map.cols - 1) { + for x in 1..(map.rows - 1) { + let x_off: u16 = (x + origin.0).try_into().unwrap(); + let y_off: u16 = (y + origin.1).try_into().unwrap(); + if map.get((x, y)).unwrap().get() == State::Alive { + queue!( + stdout(), + cursor::Hide, + cursor::MoveTo(x_off, y_off), + style::PrintStyledContent("●".green()) + )?; + } else { + queue!( + stdout(), + cursor::Hide, + cursor::MoveTo(x_off, y_off), + style::PrintStyledContent("◌".grey()) + )?; + } + } + } + stdout().flush() +} + +pub fn run_map(map: &mut Map, origin: (usize, usize), time: usize) -> Result<()> { + let mut stdout = stdout(); + loop { + map.update(); + + execute!(stdout, terminal::Clear(terminal::ClearType::All))?; + + draw_frame(&map, origin)?; + + std::thread::sleep(std::time::Duration::from_millis(time.try_into().unwrap())); + } +} diff --git a/src/lib.rs b/src/lib.rs index 52236d9..6f19c4e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ mod cells; mod cli; +mod music; pub use cells::*; pub use cli::*; +pub use music::*; diff --git a/src/main.rs b/src/main.rs index 283257f..7014fce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,10 @@ use cellseq::*; +use eyre::Result; -fn main() { - let mut map = Map::new(32, 64); - map.randomize(0.5); +fn main() -> Result<()> { + let mut map = Map::new(48, 24); + map.randomize(0.75); - loop { - map.update(); - - std::process::Command::new("clear").status().unwrap(); - - println!("{map}"); - - std::thread::sleep(std::time::Duration::from_millis(1000)); - } + run_map(&mut map, (10, 5), 300)?; + Ok(()) }