From cbc17ba85d8134e96f80bdd1f1da962222cdf24c Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 23 Aug 2022 18:30:01 +0200 Subject: [PATCH] make the table more readable --- src/timetable.rs | 22 ++++++++++++++++++---- src/utils.rs | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/timetable.rs b/src/timetable.rs index ff1b9bb..f291cd3 100644 --- a/src/timetable.rs +++ b/src/timetable.rs @@ -335,6 +335,8 @@ pub fn display(timetable: (Vec, (usize, Vec))) { let clh = 11; // Cell number let cn = 6; + // 3/4 of cell length + let quarter = (3 * cl) / 4; let sep = TabChar::Bv.val(); @@ -376,15 +378,27 @@ pub fn display(timetable: (Vec, (usize, Vec))) { Some(course) => { // Check the course's hour if i == course.start { + // If the course uses more than one time slot if course.size > 1 { - // If the course uses more than one time slot - next_skip.insert(j, &course.name); - print!("{}{:^cl$}", sep, ""); + // If the data is too long + if course.name.len() > quarter { + let data = utils::split_half(&course.name); + next_skip.insert(j, data.1); + print!("{}{:^cl$}", sep, data.0); + } else { + next_skip.insert(j, &course.name); + print!("{}{:^cl$}", sep, ""); + } info_slot = true; break; } else { // Else simply print the course - print!("{}{:^cl$}", sep, &course.name); + // If the data is too long + if course.name.len() > quarter { + print!("{}{:^cl$}", sep, utils::etc_str(&course.name)); + } else { + print!("{}{:^cl$}", sep, &course.name); + } info_slot = true; break; } diff --git a/src/utils.rs b/src/utils.rs index c1f5a44..765e03f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -82,3 +82,21 @@ pub fn line_table( } println!("{}{}", line, rs); } + +// Split a string in half with respect of words +pub fn split_half(text: &str) -> (&str, &str) { + let mid = text.len() / 2; + for (i, j) in (mid..text.len()).enumerate() { + if text.as_bytes()[j] == b' ' { + return text.split_at(mid + i); + } + } + + text.split_at(mid) +} + + +// Reduce size of string by adding etc. to it, and cutting some info +pub fn etc_str(text: &str) -> String { + format!("{}...", split_half(text).0) +}