diff options
| author | Joris | 2024-11-17 13:00:35 +0100 | 
|---|---|---|
| committer | Joris | 2024-11-17 13:00:35 +0100 | 
| commit | fbe6787acb3c844339e34c3bf4509c36281693e6 (patch) | |
| tree | 20f6d2147b18324ce2b6ba9b40e7757ec1cf9c28 /src/cli | |
| parent | 0fad55124684989ec9fd9a742b5731359d0238ce (diff) | |
Add --start-between CLI option
Diffstat (limited to 'src/cli')
| -rw-r--r-- | src/cli/mod.rs | 66 | 
1 files changed, 58 insertions, 8 deletions
| 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<String> {      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::<Vec<&str>>()[..] { +        [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<String> { +    let from_date = from.date(); +    let to_date = to.date(); +    let events = between_inclusive(conn, from_date, to_date)?; +    println!("{events:?}"); +    let events: Vec<Event> = 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::<Vec<Event>>(); +    Ok(format_events(events)) +} + +fn between_inclusive(conn: &Connection, from: NaiveDate, to: NaiveDate) -> Result<Vec<Event>> { +    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<Event>) -> String { +    let mut events = events; +    events.sort_by_key(|e| e.local_timestamp()); +    events          .iter()          .map(|e| format!("{}\n", e.pprint()))          .collect::<Vec<String>>() -        .join("")) +        .join("")  } | 
