From 634884d4837f662c67b86d7cca65d59ffc3dd19e Mon Sep 17 00:00:00 2001 From: "Jens K." <47693+sectore@users.noreply.github.com> Date: Wed, 4 Dec 2024 13:29:58 +0100 Subject: [PATCH] Logging (#8) - Add `Logger`, `Config` - Back to `nixos-unstable` - `nix flake update` --- Cargo.lock | 298 +++++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 3 + README.md | 25 +++- flake.lock | 14 +-- flake.nix | 19 ++- src/app.rs | 2 + src/config.rs | 34 ++++++ src/constants.rs | 2 + src/logging.rs | 37 ++++++ src/main.rs | 15 ++- src/terminal.rs | 24 ++-- 11 files changed, 418 insertions(+), 55 deletions(-) create mode 100644 src/config.rs create mode 100644 src/logging.rs diff --git a/Cargo.lock b/Cargo.lock index 4a38f2e..c9b0549 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "allocator-api2" version = "0.2.20" @@ -198,6 +207,27 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "directories" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "either" version = "1.13.0" @@ -217,7 +247,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -331,6 +361,17 @@ dependencies = [ "slab", ] +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gimli" version = "0.28.1" @@ -419,6 +460,16 @@ version = "0.2.166" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2ccc108bbc0b1331bd061864e7cd823c0cab660bbe6970e66e2c0614decde36" +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags", + "libc", +] + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -450,6 +501,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "memchr" version = "2.7.4" @@ -475,7 +535,17 @@ dependencies = [ "libc", "log", "wasi", - "windows-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", ] [[package]] @@ -493,6 +563,18 @@ version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "owo-colors" version = "3.5.0" @@ -519,7 +601,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -598,6 +680,61 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata 0.4.9", + "regex-syntax 0.8.5", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.5", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -614,7 +751,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -722,7 +859,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -770,6 +907,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thread_local" version = "1.1.8" @@ -786,6 +943,7 @@ version = "0.1.0" dependencies = [ "color-eyre", "crossterm", + "directories", "futures", "ratatui", "serde", @@ -793,6 +951,8 @@ dependencies = [ "tokio", "tokio-stream", "tokio-util", + "tracing", + "tracing-subscriber", ] [[package]] @@ -810,7 +970,7 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -855,9 +1015,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.33" @@ -870,25 +1042,43 @@ dependencies = [ [[package]] name = "tracing-error" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +checksum = "8b1581020d7a273442f5b45074a6a57d5757ad0a47dac0e9f0bd57b81936f3db" dependencies = [ "tracing", "tracing-subscriber", ] [[package]] -name = "tracing-subscriber" -version = "0.3.18" +name = "tracing-log" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" dependencies = [ - "sharded-slab", - "thread_local", + "log", + "once_cell", "tracing-core", ] +[[package]] +name = "tracing-subscriber" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + [[package]] name = "unicode-ident" version = "1.0.14" @@ -958,13 +1148,37 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -973,28 +1187,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -1007,24 +1239,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index f5b6602..0aa3478 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,6 @@ strum = { version = "0.26.3", features = ["derive"] } tokio = { version = "1.41.1", features = ["full"] } tokio-stream = "0.1.16" tokio-util = "0.7.12" +tracing = "0.1.41" +tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } +directories = "5.0.1" diff --git a/README.md b/README.md index 62bf0ab..5c34745 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,20 @@ -# tim:r +# tim:r -**pronounced `/ˈtʌɪmə/` or `/ˈtaɪmər/`** +**Pronounced `/ˈtʌɪmə/` or `/ˈtaɪmər/`.** Other just say `timer`. -> [!WARNING] +> [!WARNING] > _Everything is still WIP_ 😎 # About -`tim:r` is a TUI to track your `time` built with [`ratatui`](https://ratatui.rs/) ([Rust](https://www.rust-lang.org/)) +`tim:r` is a TUI app to help you to organize one of the most important thing you have in live: `time`! + +- `[t]imer` Check the time on anything you are you doing. +- `[c]ountdown` Use it for your workout, yoga session, meditation, handstand or whatever. +- `[p]omodoro` Organize your working time to be focused all the time by following the [Pomodoro Technique](https://en.wikipedia.org/wiki/Pomodoro_Technique). + + +It's built with [`ratatui`](https://ratatui.rs/) ([Rust](https://www.rust-lang.org/)) # Screens @@ -72,3 +79,13 @@ Available recipes: test # run tests t # alias for `test` ``` + +# Misc. + +## Logs + +In `debug` mode only. + +```sh +tail -f ~/.local/state/timr/logs/timr.log +``` diff --git a/flake.lock b/flake.lock index 0370627..ceabaec 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "crane": { "locked": { - "lastModified": 1733016477, - "narHash": "sha256-Hh0khbqBeCtiNS0SJgqdWrQDem9WlPEc2KF5pAY+st0=", + "lastModified": 1733286231, + "narHash": "sha256-mlIDSv1/jqWnH8JTiOV7GMUNPCXL25+6jmD+7hdxx5o=", "owner": "ipetkov", "repo": "crane", - "rev": "76d64e779e2fbaf172110038492343a8c4e29b55", + "rev": "af1556ecda8bcf305820f68ec2f9d77b41d9cc80", "type": "github" }, "original": { @@ -56,16 +56,16 @@ }, "nixpkgs": { "locked": { - "lastModified": 1720535198, - "narHash": "sha256-zwVvxrdIzralnSbcpghA92tWu2DV2lwv89xZc8MTrbg=", + "lastModified": 1733212471, + "narHash": "sha256-M1+uCoV5igihRfcUKrr1riygbe73/dzNnzPsmaLCmpo=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "205fd4226592cc83fd4c0885a3e4c9c400efabb5", + "rev": "55d15ad12a74eb7d4646254e13638ad0c4128776", "type": "github" }, "original": { "owner": "NixOS", - "ref": "nixos-23.11", + "ref": "nixos-unstable", "repo": "nixpkgs", "type": "github" } diff --git a/flake.nix b/flake.nix index fba3cff..bd4b4e5 100644 --- a/flake.nix +++ b/flake.nix @@ -2,9 +2,7 @@ inputs = { # Disable `nixos-unstable` for now, it introduced some `VScode` related errors: # error: function 'buildVscodeExtension' called without required argument 'pname' - # nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11"; - # nixpkgs.url = "github:NixOS/nixpkgs/a8a983027ca02b363dfc82fbe3f7d9548a8d3dce"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; crane.url = "github:ipetkov/crane"; fenix = { @@ -71,13 +69,14 @@ # Development shell with all necessary tools devShell = with nixpkgs.legacyPackages.${system}; mkShell { - buildInputs = with fenix.packages.${system}.stable; [ - rust-analyzer - clippy - rustfmt - toolchain - pkgs.just - ]; + buildInputs = with fenix.packages.${system}.stable; [ + rust-analyzer + clippy + rustfmt + toolchain + pkgs.just + ]; + diff --git a/src/app.rs b/src/app.rs index f972055..cee618e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -19,6 +19,7 @@ use ratatui::{ layout::{Constraint, Layout, Rect}, widgets::{Block, StatefulWidget, Widget}, }; +use tracing::debug; #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Mode { @@ -90,6 +91,7 @@ impl App { } fn handle_key_event(&mut self, key: KeyEvent) { + debug!("Received key {:?}", key.code); match key.code { KeyCode::Char('q') | KeyCode::Esc => self.mode = Mode::Quit, KeyCode::Char('c') => self.content = Content::Countdown, diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..dc07faa --- /dev/null +++ b/src/config.rs @@ -0,0 +1,34 @@ +use crate::constants::APP_NAME; +use color_eyre::eyre::{eyre, Result}; +use directories::ProjectDirs; +use std::fs; +use std::path::PathBuf; +pub struct Config { + pub log_dir: PathBuf, +} + +impl Config { + pub fn init() -> Result { + let log_dir = get_default_state_dir()?.join("logs"); + fs::create_dir_all(&log_dir)?; + + Ok(Self { log_dir }) + } +} + +// fn new +pub fn get_project_dir() -> Result { + let dirs = ProjectDirs::from("", "", APP_NAME) + .ok_or_else(|| eyre!("Failed to get project directories"))?; + + Ok(dirs) +} + +fn get_default_state_dir() -> Result { + let directory = get_project_dir()? + .state_dir() + .map(|d| d.to_path_buf()) + .ok_or_else(|| eyre!("Failed to get state directory"))?; + + Ok(directory) +} diff --git a/src/constants.rs b/src/constants.rs index 91850fe..da9dd3e 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,2 +1,4 @@ +pub static APP_NAME: &str = env!("CARGO_PKG_NAME"); + pub static TICK_VALUE_MS: u64 = 1000 / 10; // 0.1 sec in milliseconds pub static FPS_VALUE_MS: u64 = 1000 / 60; // 60 FPS in milliseconds diff --git a/src/logging.rs b/src/logging.rs new file mode 100644 index 0000000..861e229 --- /dev/null +++ b/src/logging.rs @@ -0,0 +1,37 @@ +use color_eyre::eyre::Result; +use std::fs; +use std::path::PathBuf; +use tracing::level_filters::LevelFilter; +use tracing_subscriber::{ + self, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, +}; + +use crate::constants::APP_NAME; + +pub struct Logger { + log_dir: PathBuf, +} + +impl Logger { + pub fn new(log_dir: PathBuf) -> Self { + Self { log_dir } + } + + pub fn init(&self) -> Result<()> { + let log_path = self.log_dir.join(format!("{}.log", APP_NAME)); + let log_file = fs::File::create(log_path)?; + let fmt_layer = tracing_subscriber::fmt::layer() + .with_file(true) + .with_line_number(true) + .with_writer(log_file) + .with_target(false) + .with_ansi(false); + let filter = tracing_subscriber::filter::EnvFilter::from_default_env() + .add_directive(LevelFilter::DEBUG.into()); + tracing_subscriber::registry() + .with(fmt_layer) + .with(filter) + .init(); + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index e34cc0c..2a747b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,11 @@ mod app; mod clock; +mod config; mod constants; mod events; +#[cfg(debug_assertions)] +mod logging; + mod terminal; mod utils; mod widgets; @@ -11,11 +15,16 @@ use color_eyre::Result; #[tokio::main] async fn main() -> Result<()> { - color_eyre::install()?; - let terminal = terminal::init()?; + let config = config::Config::init()?; + #[cfg(debug_assertions)] + logging::Logger::new(config.log_dir).init()?; + color_eyre::install()?; + + let terminal = terminal::setup()?; let events = events::Events::new(); App::new().run(terminal, events).await?; - terminal::restore()?; + terminal::teardown()?; + Ok(()) } diff --git a/src/terminal.rs b/src/terminal.rs index bdc68b5..9a56b31 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1,22 +1,26 @@ -use std::io::{stdout, Stdout}; +use std::io; use color_eyre::eyre::Result; -use crossterm::{execute, terminal::*}; +use crossterm::{ + cursor, execute, + terminal::{EnterAlternateScreen, LeaveAlternateScreen}, +}; use ratatui::{backend::CrosstermBackend, Terminal as RatatuiTerminal}; -pub type Terminal = RatatuiTerminal>; +pub type Terminal = RatatuiTerminal>; -pub fn init() -> Result { - enable_raw_mode()?; - execute!(stdout(), EnterAlternateScreen)?; - let mut terminal = RatatuiTerminal::new(CrosstermBackend::new(stdout()))?; +pub fn setup() -> Result { + let mut stdout = std::io::stdout(); + crossterm::terminal::enable_raw_mode()?; + execute!(stdout, EnterAlternateScreen, cursor::Hide)?; + let mut terminal = RatatuiTerminal::new(CrosstermBackend::new(stdout))?; terminal.clear()?; terminal.hide_cursor()?; Ok(terminal) } -pub fn restore() -> Result<()> { - execute!(stdout(), LeaveAlternateScreen)?; - disable_raw_mode()?; +pub fn teardown() -> Result<()> { + execute!(io::stdout(), LeaveAlternateScreen, cursor::Show)?; + crossterm::terminal::disable_raw_mode()?; Ok(()) }