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 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) {
if let Some(menus) = m.nth(args.days, args.vegetarian) {
if args.plaintext {
for i in menus.iter() {
for i in menus.1.iter() {
i.print_short_info();
}
return;
}
if args.oneline {
menus.first().unwrap().print_very_short_info();
menus.1.first().unwrap().print_very_short_info();
return;
}
// Default case --> print fancy
if let Some(dt) = Local::now().checked_add_days(chrono::Days::new(args.days as u64)) {
println!("Datum: {}", dt.date_naive());
}
println!("Datum: {}", menus.0);
println!("{}", m.name());
table_short(
menus
.1
.iter()
.map(|&x| x.get_short_info())
.collect::<Vec<(&str, String, &str)>>(),

View File

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