From 4869a274e115b467fd3a122df38b7d0d5841b4d4 Mon Sep 17 00:00:00 2001 From: Oscar Cortez Date: Sun, 14 Aug 2022 00:40:49 -0600 Subject: [PATCH] Barebones implementation of calendar view --- Cargo.lock | 2 +- src/main.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c06cef7..82002f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -529,7 +529,7 @@ dependencies = [ [[package]] name = "neocal" -version = "0.3.0" +version = "0.4.0" dependencies = [ "chrono", "clap", diff --git a/src/main.rs b/src/main.rs index 89561bd..56e64e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,6 +99,10 @@ enum Commands { #[clap(short, long, value_parser, forbid_empty_values = true, validator = validate_option_value)] /// Name of the Time Zone to return the events timezone: Option, + + #[clap(long)] + /// Get calendar entries for the current week + week: bool, }, } @@ -178,7 +182,63 @@ fn render_agenda_view(events: Vec) -> std::io::Result<()> { } fn render_calendar_view(events: Vec) -> std::io::Result<()> { - eprintln!("This command is not yet implemented."); + if events.iter().len() == 0 { + eprintln!("No Events were found."); + }; + + let mut table = Table::new(); + let mut event_date = ""; + + table.style = TableStyle::extended(); + table.max_column_width = if let Some((w, _h)) = term_size::dimensions() { + w - 60 + } else { + 80 + }; + table.add_row(Row::new(vec![ + TableCell::new("Monday"), + TableCell::new("Tuesday"), + TableCell::new("Wednesday"), + TableCell::new("Thursday"), + TableCell::new("Friday"), + TableCell::new("Saturday"), + TableCell::new("Sunday"), + ])); + + let (init_date, end_date) = current_week_bounds(); + let mut calendar_date = init_date; + let mut row = Vec::new(); + while calendar_date <= end_date { + let mut cell_text = calendar_date.format("%d").to_string(); + for event in events.iter() { + println!("{} {}", &event.start_date, calendar_date.to_string()); + if event.start_date != calendar_date.format("%a %b %d %Y").to_string() { + continue; + }; + let event_time = if &event.start_date_time != "" && &event.end_date_time != "" { + format!( + "{} - {}", + DateTime::parse_from_rfc3339(&event.start_date_time) + .unwrap() + .format("%H:%M") + .to_string(), + DateTime::parse_from_rfc3339(&event.end_date_time) + .unwrap() + .format("%H:%M") + .to_string(), + ) + } else { + "All Day".to_string() + }; + cell_text = cell_text + "\n\n" + &event_time; + cell_text = cell_text + &format!("\n- {}\n{}", &event.summary, &event.call); + } + row.push(TableCell::new(cell_text)); + calendar_date = calendar_date + Duration::days(1); + } + table.add_row(Row::new(row)); + + println!("{}", table.render()); return Ok(()); } @@ -333,6 +393,7 @@ async fn main() -> Result<(), Box> { r#for, search, timezone, + week, }) => { let calendar_to_use = if r#for.is_none() { config.get("neocal", "default").unwrap() @@ -363,7 +424,7 @@ async fn main() -> Result<(), Box> { url, &search.as_ref().unwrap_or(&"".to_string()).to_string(), &selected_timezone, - &false, + &week, &false, &false, ) @@ -404,7 +465,7 @@ async fn main() -> Result<(), Box> { url, &cli.search.unwrap_or("".to_string()).to_string(), &timezone, - &false, + &cli.week, &false, &false, )