diff --git a/.gitignore b/.gitignore index d787b70..5669dab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target /result + +war_in_tunnels.log diff --git a/Cargo.lock b/Cargo.lock index 2b7418f..aa0ef78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1245,6 +1245,17 @@ dependencies = [ "libc", ] +[[package]] +name = "simplelog" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16257adbfaef1ee58b1363bdc0664c9b8e1e30aed86049635fb5f147d065a9c0" +dependencies = [ + "log", + "termcolor", + "time", +] + [[package]] name = "siphasher" version = "1.0.2" @@ -1312,6 +1323,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "terminfo" version = "0.9.0" @@ -1422,12 +1442,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "743bd48c283afc0388f9b8827b976905fb217ad9e647fae3a379a9283c4def2c" dependencies = [ "deranged", + "itoa", "libc", "num-conv", "num_threads", "powerfmt", "serde_core", "time-core", + "time-macros", ] [[package]] @@ -1436,6 +1458,16 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7694e1cfe791f8d31026952abf09c69ca6f6fa4e1a1229e18988f06a04a12dca" +[[package]] +name = "time-macros" +version = "0.2.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e70e4c5a0e0a8a4823ad65dfe1a6930e4f4d756dcd9dd7939022b5e8c501215" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "typenum" version = "1.19.0" @@ -1521,7 +1553,9 @@ name = "war_in_tunnels" version = "0.1.0" dependencies = [ "clap", + "log", "ratatui", + "simplelog", ] [[package]] @@ -1715,6 +1749,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 64fea6a..9ada30f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,6 @@ strip = true [dependencies] clap = { version = "4.5.53", features = ["derive"] } ratatui = "0.30.0" + +log = "0.4.29" +simplelog = "0.12.2" diff --git a/src/app/widgets/board.rs b/src/app/widgets/board.rs index 23c2b8e..fc25a75 100644 --- a/src/app/widgets/board.rs +++ b/src/app/widgets/board.rs @@ -23,13 +23,13 @@ impl BoardWidget { let rows: u16 = area_height / CELL_HIGHT; let cols: u16 = area_width / CELL_WIDTH; - let h_max_offset: usize = if app.states.settings.map_height as u16 > rows { + let v_max_offset: usize = if app.states.settings.map_height as u16 > rows { app.states.settings.map_height as u16 - rows } else { 0 } as usize; - let v_max_offset: usize = if app.states.settings.map_width as u16 > cols { + let h_max_offset: usize = if app.states.settings.map_width as u16 > cols { app.states.settings.map_width as u16 - cols } else { 0 diff --git a/src/cli.rs b/src/cli.rs index 0e4676d..37c2b69 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -97,6 +97,13 @@ pub struct Cli { default_value = "120" )] pub skill_points_limit: u16, + + #[arg( + long, + help = "Enable logging to file (default: disabled)", + default_value_t = false + )] + pub log: bool, } pub fn get_args() -> Cli { diff --git a/src/lib.rs b/src/lib.rs index b323080..f1aeafe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod app; pub mod cli; +pub mod logs; diff --git a/src/logs.rs b/src/logs.rs new file mode 100644 index 0000000..50aee47 --- /dev/null +++ b/src/logs.rs @@ -0,0 +1,11 @@ +use simplelog::{Config, LevelFilter, WriteLogger}; +use std::fs::File; + +pub fn init_logger() { + WriteLogger::init( + LevelFilter::Info, + Config::default(), + File::create("war_in_tunnels.log").expect("Failed to create log file"), + ) + .expect("Failed to initialize logger"); +} diff --git a/src/main.rs b/src/main.rs index aa3ca74..4b9a91b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,10 +7,15 @@ use std::{ use war_in_tunnels::{ app::{App, Event, handle_input_events}, cli::{Cli, get_args}, + logs::init_logger, }; fn main() -> Result<()> { let args: Cli = get_args(); + if args.log { + init_logger(); + } + let mut terminal: Terminal> = ratatui::init(); let mut app: App = App::new(args);