diff options
Diffstat (limited to 'script/calendar_generator/src/main.rs')
-rw-r--r-- | script/calendar_generator/src/main.rs | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/script/calendar_generator/src/main.rs b/script/calendar_generator/src/main.rs new file mode 100644 index 0000000..4acb2c6 --- /dev/null +++ b/script/calendar_generator/src/main.rs @@ -0,0 +1,129 @@ +use chrono::prelude::*; +use std::env; +use chrono::NaiveDate; + +fn get_month( num :i8) -> & 'static str { + match num { + 1 => return "JANVIER", + 2 => return "FEVRIER", + 3 => return "MARS", + 4 => return "AVRIL", + 5 => return "MAI", + 6 => return "JUIN", + 7 => return "JUILLET", + 8 => return "AOUT", + 9 => return "SEPTEMBRE", + 10 => return "OCTOBRE", + 11 => return "NOVEMBRE", + 12 => return "DECEMBRE", + _=> return "", + } +} + +fn nb_empty_space_before(day : Weekday) -> i8{ + match day { + Weekday::Mon => return 0, + Weekday::Tue => return 1, + Weekday::Wed => return 2, + Weekday::Thu => return 3, + Weekday::Fri => return 4, + Weekday::Sat => return 5, + Weekday::Sun => return 6, + } +} + + +fn get_pretty_day(day_nb : i8) -> String { + if day_nb < 10 { + return "0".to_owned() + &day_nb.to_string(); + } + return day_nb.to_string(); +} + + +fn get_ascii_calendar(date : NaiveDate) -> String { + + let binding = date.to_string(); + let split_date = binding.split("-").collect::<Vec<_>>(); + let month = split_date[1]; + let year = split_date[0]; + let month_name = get_month(month.parse::<i8>().expect("REASON")); + let space = 38 - "CALENDRIER".len() - 1 - month_name.len() - 1 - 4; + + let mut calendar = String::from(".——————————————————————————————————————.\n"); + calendar += "|"; + for _ in 0..(space/2){ + calendar += " "; + } + calendar += &("CALENDRIER".to_owned() + " " + month_name + " " + year); + for _ in 0..(space/2 + (space % 2)){ + calendar = calendar.to_string() + " "; + } + calendar += "|\n"; + calendar += "|——————————————————————————————————————|\n"; + + + + let mut top_line = String::from("| "); + let mut middle_line = String::from("| |"); + + let base_date = NaiveDate::from_ymd_opt(year.parse::<i32>().expect("REASON"), + 1 + month.parse::<u32>().expect("REASON"), + 1).unwrap(); + + // fill empty spaces + let nb_space = nb_empty_space_before(base_date.weekday()); + + for _ in 0..nb_space{ + top_line += "———— "; + middle_line += " |" + } + + let mut day_nb = 1; + for day in base_date.iter_days().take(50){ + if day.month() != base_date.month(){ + // complete to the end of the week + top_line += "———— "; + middle_line += " |"; + if day.weekday() == Weekday::Sun { + calendar += &(top_line.clone() + " |\n"); + calendar += &(middle_line .clone() + &(" |\n")); + calendar += &(top_line.clone() + " |\n"); + break; + } + } + else { + if day.weekday() == Weekday::Sun { + calendar += &(top_line.clone() + "———— |\n"); + calendar += &(middle_line .clone() + &(" ".to_owned() + &get_pretty_day(day_nb) + " | |\n")); + top_line = String::from("| "); + middle_line = String::from("| |"); + } + else { + top_line += "———— "; + middle_line += &(" ".to_owned() + &get_pretty_day(day_nb) + " |"); + } + } + day_nb += 1 + } + calendar += "|______________________________________|\n"; + return calendar; +} + + + +fn main() { + let args: Vec<_> = env::args().collect(); + if args.len() <= 1 + { + println!("At least one month must be specified"); + return; + } + + for month in env::args(){ + match NaiveDate::parse_from_str(&("01-".to_owned() + &month), "%d-%m-%Y") { + Ok(date) => println!("{}", get_ascii_calendar(date)), + Err(error) => println!("could not parse .{}. due to: {}", month, error), + } + } +} |