diff options
Diffstat (limited to 'src/cli')
| -rw-r--r-- | src/cli/mod.rs | 18 | 
1 files changed, 14 insertions, 4 deletions
| diff --git a/src/cli/mod.rs b/src/cli/mod.rs index aa54c08..8069a10 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -4,6 +4,7 @@ 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 parse_date(s: String) -> Option<NaiveDate> { @@ -17,7 +18,8 @@ pub fn parse_date(s: String) -> Option<NaiveDate> {  pub fn started_at_date(conn: &Connection, date: NaiveDate) -> Result<String> {      let events = between_inclusive(conn, date, date)?; -    Ok(format_events(events)) +    let categories = db::categories::list(conn)?; +    Ok(format_events(events, categories))  }  pub fn parse_timestamp_range(s: String) -> Option<(NaiveDateTime, NaiveDateTime)> { @@ -50,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>> { @@ -69,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("")  } | 
