summaryrefslogtreecommitdiffstats
path: root/CLI/rust/src/main.rs
blob: e97a9e63fdfc5f09b5ade1d0df3795fab281841b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 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());
}