diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/pomoru-core/src/config.rs | 57 | ||||
| -rw-r--r-- | crates/pomoru-core/src/notification.rs | 10 | ||||
| -rw-r--r-- | crates/pomoru-core/src/status.rs | 55 |
3 files changed, 122 insertions, 0 deletions
diff --git a/crates/pomoru-core/src/config.rs b/crates/pomoru-core/src/config.rs new file mode 100644 index 0000000..74b92cf --- /dev/null +++ b/crates/pomoru-core/src/config.rs @@ -0,0 +1,57 @@ +use crate::task::Task; +use directories::ProjectDirs; +use serde::{Deserialize, Serialize}; +use std::fs; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Config { + pub work_time_mins: u64, + pub short_break_mins: u64, + pub long_break_mins: u64, + pub tasks: Vec<Task>, + + #[serde(default = "default_auto_switch")] + pub auto_switch_sessions: bool, +} + +fn default_auto_switch() -> bool { + true +} + +impl Default for Config { + fn default() -> Self { + Self { + work_time_mins: 25, + short_break_mins: 5, + long_break_mins: 15, + tasks: Vec::new(), + auto_switch_sessions: true, + } + } +} + +pub fn config_dir() -> Result<std::path::PathBuf, String> { + ProjectDirs::from("", "", "pomoru") + .map(|dirs| dirs.config_dir().to_path_buf()) + .ok_or_else(|| "Could not find config directory".to_string()) +} + +pub fn load() -> Config { + 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) + && let Ok(config) = toml::from_str::<Config>(&content) + { + return config; + } + } + Config::default() +} + +pub fn save(config: &Config) -> Result<(), Box<dyn std::error::Error>> { + let toml_str = toml::to_string_pretty(config)?; + let dir = config_dir().map_err(|e| e.to_string())?; + fs::create_dir_all(&dir)?; + fs::write(dir.join("config.toml"), toml_str)?; + Ok(()) +} diff --git a/crates/pomoru-core/src/notification.rs b/crates/pomoru-core/src/notification.rs new file mode 100644 index 0000000..3b75281 --- /dev/null +++ b/crates/pomoru-core/src/notification.rs @@ -0,0 +1,10 @@ +use notify_rust::Notification; + +pub fn send_notification(title: &str, message: &str) { + let _ = Notification::new() + .summary(title) + .body(message) + .appname("pomoru") + .timeout(5000) + .show(); +}
\ No newline at end of file diff --git a/crates/pomoru-core/src/status.rs b/crates/pomoru-core/src/status.rs new file mode 100644 index 0000000..b6d3646 --- /dev/null +++ b/crates/pomoru-core/src/status.rs @@ -0,0 +1,55 @@ +use crate::session::SessionMode; +use crate::timer::format_duration; +use directories::ProjectDirs; +use serde::Serialize; +use std::{fs, time::Duration}; + +#[derive(Debug, Clone, Serialize)] +pub struct CurrentStatus { + pub text: String, + pub tooltip: String, + pub class: String, +} + +pub fn build_status(mode: SessionMode, time_remaining_secs: u64) -> CurrentStatus { + let duration = Duration::from_secs(time_remaining_secs); + CurrentStatus { + text: format!( + "{} {} {}", + mode.icon(), + mode.label(), + format_duration(duration) + ), + tooltip: mode.label().to_string(), + class: mode.class().to_string(), + } +} + +pub fn export_status(mode: SessionMode, time_remaining_secs: u64) -> Result<(), Box<dyn std::error::Error>> { + let status = build_status(mode, time_remaining_secs); + let json = serde_json::to_string(&status)?; + + let cache_dir = ProjectDirs::from("", "", "pomoru") + .ok_or("Could not find cache directory")? + .cache_dir() + .to_path_buf(); + + fs::create_dir_all(&cache_dir)?; + fs::write(cache_dir.join("status.json"), json)?; + + Ok(()) +} + +pub fn clear_status() -> Result<(), Box<dyn std::error::Error>> { + let cache_dir = ProjectDirs::from("", "", "pomoru") + .ok_or("Could not find cache directory")? + .cache_dir() + .to_path_buf(); + + let status_file = cache_dir.join("status.json"); + if status_file.exists() { + fs::remove_file(status_file)?; + } + + Ok(()) +}
\ No newline at end of file |