From fbe6787acb3c844339e34c3bf4509c36281693e6 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 17 Nov 2024 13:00:35 +0100 Subject: Add --start-between CLI option --- src/cli/mod.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 8 deletions(-) (limited to 'src/cli') diff --git a/src/cli/mod.rs b/src/cli/mod.rs index b952a75..e7439b5 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,21 +1,71 @@ use anyhow::Result; -use chrono::Local; +use chrono::{Local, NaiveDate, NaiveDateTime, TimeZone}; use rusqlite::Connection; use crate::{db, model::event}; +use crate::model::event::Event; pub fn today(conn: &Connection) -> Result { let today = Local::now().date_naive(); - let mut events = db::events::list_non_recurring_between(conn, today, today)?; + let events = between_inclusive(conn, today, today)?; + Ok(format_events(events)) +} + +pub fn parse_timestamp_range(s: String) -> Option<(NaiveDateTime, NaiveDateTime)> { + match s.split("..").collect::>()[..] { + [from, to] => { + let from = from.parse().ok()?; + let to = to.parse().ok()?; + + let from = Local.timestamp_opt(from, 0).single()?; + let to = Local.timestamp_opt(to, 0).single()?; + + Some((from.naive_local(), to.naive_local())) + } + _ => None, + } +} + +pub fn start_between(conn: &Connection, from: NaiveDateTime, to: NaiveDateTime) -> Result { + let from_date = from.date(); + let to_date = to.date(); + let events = between_inclusive(conn, from_date, to_date)?; + println!("{events:?}"); + let events: Vec = events + .iter() + .filter(|e| { + match e.start { + None => false, + Some(t) => { + let dt = NaiveDateTime::new(e.date, t); + dt >= from && dt < to + } + } + }) + .map(|e| e.clone()) + .collect::>(); + Ok(format_events(events)) +} + +fn between_inclusive(conn: &Connection, from: NaiveDate, to: NaiveDate) -> Result> { + let mut events = db::events::list_non_recurring_between(conn, from, to)?; let recurring_events = db::events::list_recurring(conn)?; - let repetitions = event::repetitions_between(&recurring_events, today, today); - for repetition in repetitions.values().flatten() { - events.push(repetition.clone()); + let repetitions = event::repetitions_between(&recurring_events, from, to); + for (date, original_events) in repetitions.iter() { + for original_event in original_events { + let event = Event { date: *date, ..original_event.clone() }; + events.push(event); + } } - events.sort_by_key(|e| e.start); - Ok(events + Ok(events) +} + +fn format_events(events: Vec) -> String { + let mut events = events; + events.sort_by_key(|e| e.local_timestamp()); + events .iter() .map(|e| format!("{}\n", e.pprint())) .collect::>() - .join("")) + .join("") } -- cgit v1.2.3