diff options
| author | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-01-19 01:46:36 +0530 |
|---|---|---|
| committer | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-01-19 01:46:36 +0530 |
| commit | dcd61c807d95fd5ccd0d106865122788e7322fc8 (patch) | |
| tree | df81438a8bd0ce3173823806838bc24a415841e9 | |
| parent | 2e1032f34c3180720289d264869655206a3548a8 (diff) | |
| download | pomoru-dcd61c807d95fd5ccd0d106865122788e7322fc8.tar.xz pomoru-dcd61c807d95fd5ccd0d106865122788e7322fc8.zip | |
add persistance using toml file
| -rw-r--r-- | Cargo.lock | 129 | ||||
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/pomo/mod.rs | 46 | ||||
| -rw-r--r-- | src/pomo/state.rs | 12 |
5 files changed, 185 insertions, 8 deletions
@@ -280,6 +280,27 @@ dependencies = [ ] [[package]] +name = "directories" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16f5094c54661b38d03bd7e50df373292118db60b585c08a411c6d840017fe7d" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.61.2", +] + +[[package]] name = "document-features" version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -376,6 +397,17 @@ dependencies = [ [[package]] name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "getrandom" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" @@ -416,6 +448,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" [[package]] +name = "indexmap" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] name = "indoc" version = "2.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -492,6 +534,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" [[package]] +name = "libredox" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d0b95e02c851351f877147b7deea7b1afb1df71b63aa5f8270716e0c5720616" +dependencies = [ + "bitflags 2.10.0", + "libc", +] + +[[package]] name = "line-clipping" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -650,6 +702,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] name = "ordered-float" version = "4.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -787,9 +845,11 @@ name = "pomoru" version = "0.1.0" dependencies = [ "crossterm", + "directories", "ratatui", "serde", "tokio", + "toml", ] [[package]] @@ -938,6 +998,17 @@ dependencies = [ ] [[package]] +name = "redox_users" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4e608c6638b9c18977b00b475ac1f28d14e84b27d8d42f70e0bf1e3dec127ac" +dependencies = [ + "getrandom 0.2.17", + "libredox", + "thiserror 2.0.17", +] + +[[package]] name = "regex" version = "1.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1043,6 +1114,15 @@ dependencies = [ ] [[package]] +name = "serde_spanned" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776" +dependencies = [ + "serde_core", +] + +[[package]] name = "sha2" version = "0.10.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1314,6 +1394,45 @@ dependencies = [ ] [[package]] +name = "toml" +version = "0.9.11+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3afc9a848309fe1aaffaed6e1546a7a14de1f935dc9d89d32afd9a44bab7c46" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "0.7.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.0.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3198b4b0a8e11f09dd03e133c0280504d0801269e9afa46362ffde1cbeebf44" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.0.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab16f14aed21ee8bfd8ec22513f7287cd4a91aa92e44edfe2c17ddd004e92607" + +[[package]] name = "typenum" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1367,7 +1486,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2e054861b4bd027cd373e18e8d8d8e6548085000e41290d95ce0c373a654b4a" dependencies = [ "atomic", - "getrandom", + "getrandom 0.3.4", "js-sys", "wasm-bindgen", ] @@ -1463,7 +1582,7 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "692daff6d93d94e29e4114544ef6d5c942a7ed998b37abdc19b17136ea428eb7" dependencies = [ - "getrandom", + "getrandom 0.3.4", "mac_address", "sha2", "thiserror 1.0.69", @@ -1631,6 +1750,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" [[package]] +name = "winnow" +version = "0.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829" + +[[package]] name = "wit-bindgen" version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -5,6 +5,8 @@ edition = "2024" [dependencies] crossterm = "0.29.0" +directories = "6.0.0" ratatui = "0.30.0" -serde = "1.0.228" +serde = { version = "1.0.228", features = ["derive"] } tokio = { version = "1.49.0", features = ["full"] } +toml = "0.9.11" diff --git a/src/main.rs b/src/main.rs index bd5deb5..d221785 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ fn setup_panic_hook() { #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { setup_panic_hook(); - let mut app = Pomo::new(); + let mut app = Pomo::load(); app.run().await?; Ok(()) } diff --git a/src/pomo/mod.rs b/src/pomo/mod.rs index 1d38ed2..a99df5d 100644 --- a/src/pomo/mod.rs +++ b/src/pomo/mod.rs @@ -1,17 +1,53 @@ pub mod state; pub mod ui; -use crate::pomo::state::{Pomo, AppScreen, InputMode, Task, SessionMode}; +use crate::pomo::state::{Pomo, AppScreen, InputMode, Task, SessionMode, Config}; use crossterm::{ event::{self, Event, KeyCode}, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; use ratatui::prelude::*; -use std::io; -use std::time::Duration; +use std::{ io, time::Duration, fs }; +use directories::ProjectDirs; impl Pomo { + pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> { + let config = Config { + work_time_mins: self.work_time.as_secs() / 60, + short_break_mins: self.short_break_time.as_secs() / 60, + long_break_mins: self.long_break_time.as_secs() / 60, + tasks: self.tasks.clone(), + }; + + let toml = toml::to_string_pretty(&config)?; + let config_dir = ProjectDirs::from("", "", "pomoru") + .ok_or("Could not find config directory")? + .config_dir() + .to_path_buf(); + + fs::create_dir_all(&config_dir)?; + fs::write(config_dir.join("config.toml"), toml)?; + Ok(()) + } + + pub fn load() -> Self { + let mut app = Pomo::new(); + if let Some(proj_dirs) = ProjectDirs::from("", "", "pomoru") { + let config_path = proj_dirs.config_dir().join("config.toml"); + if let Ok(content) = fs::read_to_string(config_path) { + if let Ok(config) = toml::from_str::<Config>(&content) { + app.work_time = Duration::from_secs(config.work_time_mins * 60); + app.short_break_time = Duration::from_secs(config.short_break_mins * 60); + app.long_break_time = Duration::from_secs(config.long_break_mins * 60); + app.tasks = config.tasks; + app.reset_timer_to_mode(); + } + } + } + app + } + pub async fn run(&mut self) -> io::Result<()> { enable_raw_mode()?; let mut stdout = io::stdout(); @@ -38,6 +74,10 @@ impl Pomo { } } } + + if self.should_quit { + let _ = self.save(); + } } disable_raw_mode()?; diff --git a/src/pomo/state.rs b/src/pomo/state.rs index 305a300..e224c9d 100644 --- a/src/pomo/state.rs +++ b/src/pomo/state.rs @@ -1,5 +1,6 @@ use ratatui::widgets::ListState; use std::time::Duration; +use serde::{ Serialize, Deserialize }; #[derive(PartialEq, Clone, Copy)] pub enum SessionMode { Work, ShortBreak, LongBreak } @@ -10,9 +11,18 @@ pub enum AppScreen { Timer, Tasks } #[derive(PartialEq, Clone, Copy)] pub enum InputMode { Normal, Insert, Edit, TimerEdit } +#[derive(Serialize, Deserialize)] +pub struct Config { + pub work_time_mins: u64, + pub short_break_mins: u64, + pub long_break_mins: u64, + pub tasks: Vec<Task> +} + +#[derive(Serialize, Deserialize, Clone)] pub struct Task { pub title: String, - pub is_done: bool, + pub is_done: bool } pub struct Pomo { |