diff options
Diffstat (limited to 'src/cli')
-rw-r--r-- | src/cli/mod.rs | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 3674a08..8069a10 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,14 +1,25 @@ use anyhow::Result; -use chrono::{Local, NaiveDate, NaiveDateTime, TimeZone}; +use chrono::{Local, NaiveDate, NaiveDateTime, TimeDelta, TimeZone}; use rusqlite::Connection; +use std::ops::{Add, Sub}; use crate::model::event::Event; +use crate::model::category::Category; use crate::{db, model::event}; -pub fn today(conn: &Connection) -> Result<String> { - let today = Local::now().date_naive(); - let events = between_inclusive(conn, today, today)?; - Ok(format_events(events)) +pub fn parse_date(s: String) -> Option<NaiveDate> { + match s.as_str() { + "yesterday" => Some(Local::now().sub(TimeDelta::days(1)).date_naive()), + "today" => Some(Local::now().date_naive()), + "tomorrow" => Some(Local::now().add(TimeDelta::days(1)).date_naive()), + _ => NaiveDate::parse_from_str(&s, "%Y-%m-%d").ok(), + } +} + +pub fn started_at_date(conn: &Connection, date: NaiveDate) -> Result<String> { + let events = between_inclusive(conn, date, date)?; + let categories = db::categories::list(conn)?; + Ok(format_events(events, categories)) } pub fn parse_timestamp_range(s: String) -> Option<(NaiveDateTime, NaiveDateTime)> { @@ -41,7 +52,8 @@ pub fn start_between(conn: &Connection, from: NaiveDateTime, to: NaiveDateTime) }) .cloned() .collect::<Vec<Event>>(); - Ok(format_events(events)) + let categories = db::categories::list(conn)?; + Ok(format_events(events, categories)) } fn between_inclusive(conn: &Connection, from: NaiveDate, to: NaiveDate) -> Result<Vec<Event>> { @@ -60,12 +72,19 @@ fn between_inclusive(conn: &Connection, from: NaiveDate, to: NaiveDate) -> Resul Ok(events) } -fn format_events(events: Vec<Event>) -> String { +fn format_events(events: Vec<Event>, categories: Vec<Category>) -> String { let mut events = events; events.sort_by_key(|e| e.local_timestamp()); events .iter() - .map(|e| format!("{}\n", e.pprint())) + .map(|e| { + let category = e.category.and_then(|c| categories.clone().into_iter().find(|c2| c == c2.id)); + let category_str = match category { + Some(c) => format!("[{}] ", c.name), + None => "".to_string() + }; + return format!("{}{}\n", category_str, e.pprint()) + }) .collect::<Vec<String>>() .join("") } |