diff --git a/Cargo.lock b/Cargo.lock index fb211e1..8c5d4fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1236,6 +1236,17 @@ dependencies = [ "libloading 0.7.4", ] +[[package]] +name = "client_cli" +version = "0.1.0" +dependencies = [ + "anyhow", + "bincode", + "pico-args", + "renet", + "store", +] + [[package]] name = "client_tui" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 4bc64ce..e521f37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ resolver="2" members = [ "client", "client_tui", + "client_cli", "server", "store" ] diff --git a/README.md b/README.md index 084267b..d2808fa 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# trictrac +# Trictrac Game of [Trictrac](https://en.wikipedia.org/wiki/Trictrac) in rust. diff --git a/client_cli/Cargo.toml b/client_cli/Cargo.toml new file mode 100644 index 0000000..d2bed0c --- /dev/null +++ b/client_cli/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "client_cli" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.75" +bincode = "1.3.3" +pico-args = "0.5.0" +renet = "0.0.13" +store = { path = "../store" } diff --git a/client_cli/src/app.rs b/client_cli/src/app.rs new file mode 100644 index 0000000..1c5d500 --- /dev/null +++ b/client_cli/src/app.rs @@ -0,0 +1,57 @@ +// Application. +#[derive(Debug, Default)] +pub struct App { + // should the application exit? + pub should_quit: bool, + // counter + pub counter: u8, +} + +impl App { + // Constructs a new instance of [`App`]. + pub fn new() -> Self { + Self::default() + } + + pub fn input(&mut self, input: &str) { + println!("'{}'", input); + if input == "quit" { + self.quit(); + } + } + + // Set running to false to quit the application. + pub fn quit(&mut self) { + self.should_quit = true; + } + + pub fn increment_counter(&mut self) { + if let Some(res) = self.counter.checked_add(1) { + self.counter = res; + } + } + + pub fn decrement_counter(&mut self) { + if let Some(res) = self.counter.checked_sub(1) { + self.counter = res; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_app_increment_counter() { + let mut app = App::default(); + app.increment_counter(); + assert_eq!(app.counter, 1); + } + + #[test] + fn test_app_decrement_counter() { + let mut app = App::default(); + app.decrement_counter(); + assert_eq!(app.counter, 0); + } +} diff --git a/client_cli/src/main.rs b/client_cli/src/main.rs new file mode 100644 index 0000000..3107db3 --- /dev/null +++ b/client_cli/src/main.rs @@ -0,0 +1,21 @@ +// Application. +pub mod app; + +use anyhow::Result; +use app::App; +use std::io; + +fn main() -> Result<()> { + // Create an application. + let mut app = App::new(); + + // Start the main loop. + while !app.should_quit { + println!("what?>"); + let mut input = String::new(); + let _bytecount = io::stdin().read_line(&mut input)?; + app.input(input.trim()); + } + + Ok(()) +}