Fix menu date display

This commit is contained in:
2024-04-24 11:02:23 +02:00
parent 93e5230e44
commit 01fb111608
2 changed files with 22 additions and 19 deletions

View File

@@ -1,4 +1,3 @@
use chrono::Local;
use mensa::{Mealplan, Mensa, MensaName}; use mensa::{Mealplan, Mensa, MensaName};
use prettytable::{row, Cell, Row, Table}; use prettytable::{row, Cell, Row, Table};
@@ -30,24 +29,23 @@ fn exec_arguments(args: &cli::Args) {
fn exec_arg_helper(args: &cli::Args, m: &impl Mealplan) { fn exec_arg_helper(args: &cli::Args, m: &impl Mealplan) {
if let Some(menus) = m.nth(args.days, args.vegetarian) { if let Some(menus) = m.nth(args.days, args.vegetarian) {
if args.plaintext { if args.plaintext {
for i in menus.iter() { for i in menus.1.iter() {
i.print_short_info(); i.print_short_info();
} }
return; return;
} }
if args.oneline { if args.oneline {
menus.first().unwrap().print_very_short_info(); menus.1.first().unwrap().print_very_short_info();
return; return;
} }
// Default case --> print fancy // Default case --> print fancy
if let Some(dt) = Local::now().checked_add_days(chrono::Days::new(args.days as u64)) { println!("Datum: {}", menus.0);
println!("Datum: {}", dt.date_naive());
}
println!("{}", m.name()); println!("{}", m.name());
table_short( table_short(
menus menus
.1
.iter() .iter()
.map(|&x| x.get_short_info()) .map(|&x| x.get_short_info())
.collect::<Vec<(&str, String, &str)>>(), .collect::<Vec<(&str, String, &str)>>(),

View File

@@ -12,8 +12,8 @@ pub enum MensaName {
pub trait Mealplan { pub trait Mealplan {
fn id(&self) -> &str; fn id(&self) -> &str;
fn name(&self) -> &str; fn name(&self) -> &str;
fn today(&self) -> Vec<&Menu>; fn today(&self) -> (String, Vec<&Menu>);
fn nth(&self, days: u8, vegetarian: bool) -> Option<Vec<&Menu>>; fn nth(&self, days: u8, vegetarian: bool) -> Option<(String, Vec<&Menu>)>;
} }
fn get_nth_date(days: u8) -> Option<chrono::DateTime<Local>> { fn get_nth_date(days: u8) -> Option<chrono::DateTime<Local>> {
@@ -70,35 +70,40 @@ impl Mealplan for Mensa {
&&self.canteen.canteen &&self.canteen.canteen
} }
fn today(&self) -> Vec<&Menu> { fn today(&self) -> (String, Vec<&Menu>) {
let local = format!("{}", Local::now().format("%Y-%m-%d")); let local = format!("{}", Local::now().format("%Y-%m-%d"));
self.canteen (
.menus local.clone(),
.iter() self.canteen
.filter(|&x| x.menu_date == local) .menus
.collect() .iter()
.filter(|&x| x.menu_date == local)
.collect(),
)
} }
fn nth(&self, days: u8, vegetarian: bool) -> Option<Vec<&Menu>> { fn nth(&self, days: u8, vegetarian: bool) -> Option<(String, Vec<&Menu>)> {
match get_nth_date(days) { match get_nth_date(days) {
Some(dt) => { Some(dt) => {
let local = format!("{}", dt.format("%Y-%m-%d")); let local = format!("{}", dt.format("%Y-%m-%d"));
if vegetarian { if vegetarian {
Some( Some((
local.clone(),
self.canteen self.canteen
.menus .menus
.iter() .iter()
.filter(|&x| x.menu_date == local && x.menu_line.contains("veg")) .filter(|&x| x.menu_date == local && x.menu_line.contains("veg"))
.collect(), .collect(),
) ))
} else { } else {
Some( Some((
local.clone(),
self.canteen self.canteen
.menus .menus
.iter() .iter()
.filter(|&x| x.menu_date == local) .filter(|&x| x.menu_date == local)
.collect(), .collect(),
) ))
} }
} }
_ => None, _ => None,