From e4aa7c624c30bc7bc2b8f6c662d4dc73a3b638d7 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 23 Aug 2022 13:16:58 +0200 Subject: [PATCH] Add stuff to build the table --- src/timetable/models.rs | 52 +++++++++++++++++++++++++++++++++++++++++ src/utils.rs | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/src/timetable/models.rs b/src/timetable/models.rs index 1b67bcf..a62f476 100644 --- a/src/timetable/models.rs +++ b/src/timetable/models.rs @@ -33,3 +33,55 @@ pub struct Day { /// Ordered list of all the courses of the day pub courses: Vec>, } + +/// Collection of char for the table +pub enum TabChar { + /// Vertical bar + Bv, + /// Horizontal bar + Bh, + /// Joint left + Jl, + /// Joint right + Jr, + /// Joint bottom left + Jbl, + /// Joint bottom right + Jbr, + /// Joint top left + Jtl, + /// Joint top right + Jtr, + /// Joint to top + Jtt, + /// Joint to bottom + Jtb, + /// Joint of the middle + Jm, +} + +impl TabChar { + /// Value of the element + pub fn val(&self) -> char { + match *self { + Self::Bv => '│', + Self::Bh => '─', + Self::Jl => '├', + Self::Jr => '┤', + Self::Jbl => '└', + Self::Jbr => '┘', + Self::Jtl => '┌', + Self::Jtr => '┐', + Self::Jtt => '┴', + Self::Jtb => '┬', + Self::Jm => '┼', + } + } +} + +/// Position for lines inside the table +pub enum Position { + Top, + Middle, + Bottom +} diff --git a/src/utils.rs b/src/utils.rs index 44dec89..46a0c95 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,5 @@ +use crate::timetable::models::{Position, TabChar}; + /// Panic if an error happened pub fn check_errors(html: &String, loc: &str) { match html { @@ -13,3 +15,51 @@ pub fn check_errors(html: &String, loc: &str) { fn err_code(code: i32) -> String { format!("HTTP Code : {}", code) } + +/// Print a line for the table +/// +/// `pos` defines what separator should be used and can be: +/// - 0 -> top of the table +/// - 1 -> middle of the table +/// - 2 -> bottom of the table +pub fn line_table(cell_length: usize, number_cell: usize, pos: Position) { + let err_msg = "Unknown position"; + + // Left side + let ls = match pos { + Position::Top => TabChar::Jtl.val(), + Position::Middle => TabChar::Jl.val(), + Position::Bottom => TabChar::Jbl.val(), + _ => panic!("{}", err_msg), + }; + + // Middle + let ms = match pos { + Position::Top => TabChar::Jtb.val(), + Position::Middle => TabChar::Jm.val(), + Position::Bottom => TabChar::Jtt.val(), + _ => panic!("{}", err_msg), + }; + + // Right side + let rs = match pos { + Position::Top => TabChar::Jtr.val(), + Position::Middle => TabChar::Jr.val(), + Position::Bottom => TabChar::Jbr.val(), + _ => panic!("{}", err_msg), + }; + + let line = TabChar::Bh.val().to_string().repeat(cell_length); + + // Print the line + print!( + "\n{}{}{}", + ls, + line, + ms + ); + for _ in 2..number_cell { + print!("{}{}", line, ms); + } + println!("{}{}", line, rs); +}