From aa287b87c2c68b6b2855532feccbc3c9f68b481e Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 23 Aug 2022 16:51:02 +0200 Subject: [PATCH] WIP: table --- src/info.rs | 6 ++-- src/timetable.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/src/info.rs b/src/info.rs index a944d87..24e1b7a 100644 --- a/src/info.rs +++ b/src/info.rs @@ -59,7 +59,7 @@ pub async fn info() -> HashMap, i64)>> { /// Get info webpage async fn get_webpage() -> Result> { - let url = "https://informatique.up8.edu/licence-iv/edt"; + /* let url = "https://informatique.up8.edu/licence-iv/edt"; // We don't use reqwest::get() but a client with a custom user-agent // in order to avoid getting rate limit @@ -69,7 +69,9 @@ async fn get_webpage() -> Result> { let html = client.get(url).send().await?.text().await?; // Panic on error - crate::utils::check_errors(&html, url); + crate::utils::check_errors(&html, url); */ + + let html = std::fs::read_to_string("target/debug-sch.htm").unwrap(); Ok(Html::parse_document(&html)) } diff --git a/src/timetable.rs b/src/timetable.rs index 167adcc..3021f0a 100644 --- a/src/timetable.rs +++ b/src/timetable.rs @@ -1,6 +1,13 @@ use chrono::{Datelike, Duration, TimeZone, Utc}; use regex::Regex; use scraper::{Html, Selector}; +use std::collections::HashMap; + +use crate::utils::{ + self, + models::{Position, TabChar}, +}; + pub mod models; /// Fetch the timetable for a class @@ -120,7 +127,7 @@ async fn get_webpage( semester: i8, letter: Option, ) -> Result> { - let url = { + /* let url = { let panic_semester_message = "Unknown semester."; let panic_letter_message = "Unknown letter."; @@ -165,7 +172,8 @@ async fn get_webpage( let html = reqwest::get(&url).await?.text().await?; // Panic on error - crate::utils::check_errors(&html, &url); + crate::utils::check_errors(&html, &url); */ + let html = std::fs::read_to_string("target/debug-l1a.htm").unwrap(); // Parse document let document = Html::parse_document(&html); @@ -210,7 +218,7 @@ type T = ( (usize, Vec), ); // Data builded in the info webpage -type D = std::collections::HashMap< +type D = HashMap< // Semester usize, // List of start and repetition of course weeks @@ -321,4 +329,78 @@ fn get_semester(semester: Option, letter: Option) -> i8 { } /// Display the timetable -pub fn display(timetable: (Vec, (usize, Vec))) {} +pub fn display(timetable: (Vec, (usize, Vec))) { + // Cell length + let cl = 35; + // Cell length for hours + let clh = 11; + // Cell number + let cn = 6; + + let sep = TabChar::Bv.val(); + + // Top of the tab + utils::line_table(clh, cl, cn, Position::Top, HashMap::new()); + + // First empty case + print!("{}{:^clh$}{}", sep, "", sep); + + // Print day's of the week + let mut days = HashMap::new(); + for (i, data) in (&timetable.1 .1).iter().enumerate() { + days.insert(i, &data.name); + print!("{:^cl$}{}", &data.name, sep); + } + + // Store the data of the course for utils::line_table + let mut next_skip = HashMap::new(); + // For each hours + for (i, hour) in timetable.0.into_iter().enumerate() { + // Draw separator line + utils::line_table(clh, cl, cn, Position::Middle, next_skip); + + // Reset + next_skip = HashMap::new(); + + // Print hour + print!("{}{:^clh$}", sep, hour); + + // For all the days + for (j, day) in (&timetable.1 .1).iter().enumerate() { + // For all the courses of each days + for (k, course_opt) in (&day.courses).iter().enumerate() { + match course_opt { + // If there is a course + Some(course) => { + // Check if the course's hour + if i == k { + if course.size != 1 { + // If the course uses more than one time slot + next_skip.insert(course.start, &course.name); + print!("{}{:^cl$}", sep, ""); + break; + } else { + // Else simply print the course + print!("{}{:^cl$}", sep, &course.name); + break; + } + } + } + // If no course was found + None => { + // Verify the "no course" is in the correct day and hour + if *days.get(&j).unwrap() == &day.name.to_string() && k == i { + // If yes print empty row + print!("{}{:^cl$}", sep, ""); + break; + } + // Else it was a course of another day/time + } + }; + } + } + print!("{}", sep); + } + // Bottom of the table + utils::line_table(clh, cl, cn, Position::Bottom, HashMap::new()); +}