summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authora14m <[email protected]>2021-04-08 03:37:44 +0200
committera14m <[email protected]>2021-04-08 03:37:44 +0200
commit6992f391100873a54b93d48b611c0daff74c0423 (patch)
treec26a06e1a920bea8db009cd226e717d64d9c3f27
parente240de9160319f60036845854fc4887f8ea659eb (diff)
Add the running loop and ctrlc handling
-rw-r--r--CLI/rust/Cargo.lock29
-rw-r--r--CLI/rust/Cargo.toml1
-rw-r--r--CLI/rust/src/main.rs22
3 files changed, 48 insertions, 4 deletions
diff --git a/CLI/rust/Cargo.lock b/CLI/rust/Cargo.lock
index 0b6c5ff..eeade1f 100644
--- a/CLI/rust/Cargo.lock
+++ b/CLI/rust/Cargo.lock
@@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
+version = 3
+
[[package]]
name = "aho-corasick"
version = "0.7.15"
@@ -78,6 +80,16 @@ dependencies = [
]
[[package]]
+name = "ctrlc"
+version = "3.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c15b8ec3b5755a188c141c1f6a98e76de31b936209bf066b647979e2a84764a9"
+dependencies = [
+ "nix",
+ "winapi",
+]
+
+[[package]]
name = "curl"
version = "0.4.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -112,6 +124,7 @@ name = "game-of-life"
version = "0.1.0"
dependencies = [
"clap",
+ "ctrlc",
"curl",
"mocktopus",
"rand",
@@ -172,9 +185,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
-version = "0.2.81"
+version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
+checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
[[package]]
name = "libz-sys"
@@ -215,6 +228,18 @@ dependencies = [
]
[[package]]
+name = "nix"
+version = "0.20.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fa9b4819da1bc61c0ea48b63b7bc8604064dd43013e7cc325df098d49cd7c18a"
+dependencies = [
+ "bitflags",
+ "cc",
+ "cfg-if",
+ "libc",
+]
+
+[[package]]
name = "openssl-probe"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/CLI/rust/Cargo.toml b/CLI/rust/Cargo.toml
index a8e42e5..7c4f7ca 100644
--- a/CLI/rust/Cargo.toml
+++ b/CLI/rust/Cargo.toml
@@ -9,6 +9,7 @@ clap = { version = "3.0.0-beta.2", features = ["wrap_help"] }
curl = "0.4.35"
rand = "0.8.3"
regex = "1.4.5"
+ctrlc = "3.1.8"
terminal_size = "0.1.16"
[dev-dependencies]
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());
}