diff options
Diffstat (limited to 'CLI/rust/src/main.rs')
| -rw-r--r-- | CLI/rust/src/main.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/CLI/rust/src/main.rs b/CLI/rust/src/main.rs index 2136dcb..e97a9e6 100644 --- a/CLI/rust/src/main.rs +++ b/CLI/rust/src/main.rs @@ -1,9 +1,27 @@ +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; +use std::{thread, time}; + mod game_of_life; /// Create and run a Game of Life simulations fn main() { + let running = Arc::new(AtomicBool::new(true)); let opts = game_of_life::opts::Opts::from(game_of_life::app().get_matches()) .unwrap_or_else(|e| e.exit()); - let universe = game_of_life::generate(opts).unwrap_or_else(|e| e.exit()); - println!("{:?}", universe); + let mut universe = game_of_life::generate(opts).unwrap_or_else(|e| e.exit()); + + let instance = running.clone(); + ctrlc::set_handler(move || { + instance.store(false, Ordering::SeqCst); + }) + .expect("Error setting Ctrl-C handler"); + + while running.load(Ordering::SeqCst) { + println!("\x1B[?1049h\x1B[H{}", universe); + universe = universe.evolve(); + thread::sleep(time::Duration::from_millis(universe.opts.delay as u64)); + } + print!("\x1B[?1049l"); + print!("{}\n\x1B[0m", universe.opts.banner()); } |
