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 /src/pomo/mod.rs | |
| parent | 2e1032f34c3180720289d264869655206a3548a8 (diff) | |
| download | pomoru-dcd61c807d95fd5ccd0d106865122788e7322fc8.tar.xz pomoru-dcd61c807d95fd5ccd0d106865122788e7322fc8.zip | |
add persistance using toml file
Diffstat (limited to 'src/pomo/mod.rs')
| -rw-r--r-- | src/pomo/mod.rs | 46 |
1 files changed, 43 insertions, 3 deletions
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()?; |