From 5f1f9065476a46ccced3f1b0e31a90d3a00eccef Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 26 Feb 2022 23:18:13 +0100 Subject: Add --list-today CLI param to list today’s events --- src/cli/mod.rs | 21 +++++++++++++++++++++ src/main.rs | 21 +++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/cli/mod.rs (limited to 'src') diff --git a/src/cli/mod.rs b/src/cli/mod.rs new file mode 100644 index 0000000..862bcbe --- /dev/null +++ b/src/cli/mod.rs @@ -0,0 +1,21 @@ +use anyhow::Result; +use chrono::Local; +use rusqlite::Connection; + +use crate::{db, model::event, model::event::Event}; + +pub fn today(conn: &Connection) -> Result { + let today = Local::today().naive_local(); + let mut events = db::list_non_repeated_between(conn, today, today)?; + let repeated_events = db::list_repeated(conn)?; + let repetitions = event::repetitions_between(&repeated_events, today, today); + for repetition in repetitions.values().flatten() { + events.push(repetition.clone()); + } + events.sort_by_key(|e| e.start); + Ok(events + .iter() + .map(Event::pprint) + .collect::>() + .join("\n")) +} diff --git a/src/main.rs b/src/main.rs index 956e930..1f5a294 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod cli; mod db; mod gui; mod model; @@ -8,13 +9,25 @@ use structopt::StructOpt; #[derive(StructOpt)] #[structopt()] struct Opt { - #[structopt(long, default_value = "database.db")] - database: String, + /// Path of SQLite database in which to store events + #[structopt(long = "database", default_value = "database.db")] + db_path: String, + + /// List today’s events as plain text + #[structopt(long = "list-today")] + list_today: bool, } fn main() -> Result<()> { - let db_path = Opt::from_args().database; + let Opt { + db_path, + list_today, + } = Opt::from_args(); let conn = db::init(&db_path)?; - gui::run(conn); + if list_today { + print!("{}", cli::today(&conn)?); + } else { + gui::run(conn); + } Ok(()) } -- cgit v1.2.3