From fabcd6dac94fe019832b3c2392eb4388fa3897ac Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 13 Sep 2024 19:00:18 +0200 Subject: [PATCH] start date chooser (#8) --- src/info.rs | 23 ++++++++++++----------- src/main.rs | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/info.rs b/src/info.rs index 42d5476..5d5b06e 100644 --- a/src/info.rs +++ b/src/info.rs @@ -8,12 +8,12 @@ use crate::utils::{ models::{Info, InfoList, InfoType}, }; -pub async fn info( +pub async fn get_start_date( level: i8, semester_opt: Option, year_opt: Option, user_agent: &str, -) -> Info { +) -> String { let semester = get_semester(semester_opt); let year = get_year(year_opt, semester); @@ -34,14 +34,15 @@ pub async fn info( .inner_html(); let re = Regex::new(r"\d{1,2} (septembre|octobre)").unwrap(); - let date = re.captures(&raw_data).and_then(|caps| caps.get(0)).map_or( - { - let default = "1 septembre"; - println!("Can't find the first week of school, default to : {default}"); - default - }, - |m| m.as_str(), - ); + + re.captures(&raw_data) + .and_then(|caps| caps.get(0)) + .map_or("1 septembre".to_owned(), |m| m.as_str().to_owned()) +} + +pub fn info(semester_opt: Option, year_opt: Option, date: &str) -> Info { + let semester = get_semester(semester_opt); + let year = get_year(year_opt, semester); // 1st semester let weeks_s1_1 = 6; // Weeks before break @@ -119,7 +120,7 @@ fn anglophonization(date: &str) -> String { // Use 12:00 and UTC TZ for chrono parser "{} 12:00 +0000", // Replace french by english month - re.replace_all(date, |cap: &Captures| match &cap[0] { + re.replace_all(&date.to_lowercase(), |cap: &Captures| match &cap[0] { month if dico.contains_key(month) => dico.get(month).unwrap(), month => { panic!("Unknown month: {month}") diff --git a/src/main.rs b/src/main.rs index c496cd7..746f6fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use clap::Parser; +use dialoguer::Input; use regex::Regex; mod filter; @@ -33,6 +34,10 @@ struct Args { /// Doesn't distinguish TD from TP #[clap(short, long)] td_are_tp: bool, + + /// First day of your year + #[clap(short, long)] + first_day: Option, } #[tokio::main] @@ -58,8 +63,17 @@ async fn main() { timetable = filter::timetable(timetable, args.td_are_tp); + let date = match args.first_day { + None => Input::new() + .with_prompt("Début des cours de l'année") + .default(info::get_start_date(level, args.semester, args.year, &user_agent).await) + .interact_text() + .unwrap(), + Some(day) => day, + }; + println!("Récupération des informations par rapport à l'année..."); - let info = info::info(level, args.semester, args.year, &user_agent).await; + let info = info::info(args.semester, args.year, &date); if args.export.is_some() { // Export the calendar