From 3933db74a8579c461328db7c83bfa7721108252d Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Fri, 2 May 2025 11:48:06 +0200 Subject: Show category in cli output --- src/cli/mod.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/cli') 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 { @@ -17,7 +18,8 @@ pub fn parse_date(s: String) -> Option { pub fn started_at_date(conn: &Connection, date: NaiveDate) -> Result { 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::>(); - 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> { @@ -69,12 +72,19 @@ fn between_inclusive(conn: &Connection, from: NaiveDate, to: NaiveDate) -> Resul Ok(events) } -fn format_events(events: Vec) -> String { +fn format_events(events: Vec, categories: Vec) -> 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::>() .join("") } -- cgit v1.2.3