diff --git a/Cargo.lock b/Cargo.lock index b9dc7ea..ce96d88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -642,6 +642,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a41953f86f8a05768a6cda24def994fd2f424b04ec5c719cf89989779f199071" dependencies = [ "powerfmt", + "serde_core", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 874a5db..5660ddd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ tracing = "0.1.41" tracing-subscriber = { version = "0.3.20", features = ["env-filter"] } directories = "5.0.1" clap = { version = "4.5.48", features = ["derive"] } -time = { version = "0.3.44", features = ["formatting", "local-offset", "parsing", "macros"] } +time = { version = "0.3.44", features = ["formatting", "local-offset", "parsing", "macros", "serde"] } notify-rust = "4.11.7" rodio = { version = "0.20.1", features = [ "symphonia-mp3", diff --git a/src/app.rs b/src/app.rs index 30f0174..be7f2b9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,7 +2,7 @@ use crate::{ args::Args, common::{AppEditMode, AppTime, AppTimeFormat, ClockTypeId, Content, Style, Toggle}, constants::TICK_VALUE_MS, - event::{Event, get_default_event}, + event::Event, events::{self, TuiEventHandler}, storage::AppStorage, terminal::Terminal, @@ -135,7 +135,7 @@ impl From for App { None => stg.elapsed_value_countdown, }, current_value_timer: stg.current_value_timer, - event: args.event.unwrap_or_else(get_default_event), + event: args.event.unwrap_or(stg.event), app_tx, #[cfg(feature = "sound")] sound_path: args.sound, @@ -470,6 +470,7 @@ impl App { ), elapsed_value_countdown: Duration::from(*self.countdown.get_elapsed_value()), current_value_timer: Duration::from(*self.timer.get_clock().get_current_value()), + event: self.event.get_event(), footer_app_time: self.footer.app_time_format().is_some().into(), } } diff --git a/src/event.rs b/src/event.rs index 93f33b0..e3edc9d 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,21 +1,20 @@ -use time::macros::format_description; +use serde::{Deserialize, Serialize}; +use time::macros::{datetime, format_description}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct Event { pub date_time: time::PrimitiveDateTime, pub title: Option, } -pub fn get_default_event() -> Event { - Event { - date_time: time::PrimitiveDateTime::parse( +impl Default for Event { + fn default() -> Self { + Self { // Mario Bros. "...entered mass production in Japan on June 21" 1983 // https://en.wikipedia.org/wiki/Mario_Bros.#Release - "1983-06-21 00:00", - format_description!("[year]-[month]-[day] [hour]:[minute]"), - ) - .unwrap(), - title: Some("Release date of Mario Bros. in Japan".into()), + date_time: datetime!(1983-06-21 00:00), + title: Some("Release date of Mario Bros. in Japan".into()), + } } } diff --git a/src/storage.rs b/src/storage.rs index 000645e..9d64378 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,5 +1,6 @@ use crate::{ common::{AppTimeFormat, Content, Style, Toggle}, + event::Event, widgets::pomodoro::Mode as PomodoroMode, }; use color_eyre::eyre::Result; @@ -44,6 +45,8 @@ pub struct AppStorage { pub elapsed_value_countdown: Duration, // timer pub current_value_timer: Duration, + // event + pub event: Event, // footer pub footer_app_time: Toggle, } @@ -75,6 +78,8 @@ impl Default for AppStorage { elapsed_value_countdown: Duration::ZERO, // timer current_value_timer: Duration::ZERO, + // event + event: Event::default(), // footer footer_app_time: Toggle::Off, } diff --git a/src/widgets/clock_test.rs b/src/widgets/clock_test.rs index a6b56aa..5935d57 100644 --- a/src/widgets/clock_test.rs +++ b/src/widgets/clock_test.rs @@ -203,7 +203,7 @@ fn test_format_by_duration_boundaries() { Format::YyyDddHhMmSs ); - // YyyDdHhMmSs + // YyyyDdHhMmSs assert_eq!( format_by_duration::(&(1000 * ONE_YEAR + 10 * ONE_DAY).into()), Format::YyyyDdHhMmSs diff --git a/src/widgets/event.rs b/src/widgets/event.rs index 91a37d4..a7295d0 100644 --- a/src/widgets/event.rs +++ b/src/widgets/event.rs @@ -73,6 +73,13 @@ impl EventState { self.with_decis = with_decis; } + pub fn get_event(&self) -> Event { + Event { + title: self.title.clone(), + date_time: time::PrimitiveDateTime::new(self.event_time.date(), self.event_time.time()), + } + } + pub fn get_percentage_done(&self) -> u16 { get_percentage(self.start_time, self.event_time, self.app_time) }