Skip to content

Commit

Permalink
feat: Display closest birthday first
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Jan 1, 2024
1 parent 084aef1 commit 6b9e42a
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,42 @@ struct DisplayedBirthday {
age: String,
#[tabled(rename = "Next birthday")]
next_birthday: String,

#[tabled(skip)]
next_birthday_in_days: i64,
}

impl DisplayedBirthday {
fn from_birthday(birthday: &Birthday, today: NaiveDate) -> DisplayedBirthday {
let age = birthday.age(today).unwrap();
let birthyear = birthday.date.year();
let next_birthday_in_days = (birthday.next(today) - today).num_days();
let next_birthday = if birthday.is_today(today) {
"today".to_string()
let next_birthday_in_days = if birthday.is_today(today) {
0
} else {
match next_birthday_in_days {
1 => "1 day".to_string(),
_ => format!("{next_birthday_in_days} days"),
}
(birthday.next(today) - today).num_days()
};
let next_birthday = match next_birthday_in_days {
0 => "today 🎂".to_string(),
1 => "1 day".to_string(),
_ => format!("{next_birthday_in_days} days"),
};
DisplayedBirthday {
id: birthday.id,
name: birthday.name.clone(),
birthday: birthday.date.format("%d %B").to_string(),
age: format!("{age} ({birthyear})"),
next_birthday,
next_birthday_in_days,
}
}
}

pub fn output(birthdays: Vec<Birthday>, today: NaiveDate) {
let displayed_birthdays: Vec<DisplayedBirthday> = birthdays
let mut displayed_birthdays: Vec<DisplayedBirthday> = birthdays
.iter()
.map(|birthday| DisplayedBirthday::from_birthday(birthday, today))
.collect();
displayed_birthdays.sort_by_key(|birthday| birthday.next_birthday_in_days);
let table = Table::new(displayed_birthdays)
.with(Style::rounded())
.to_string();
Expand Down

0 comments on commit 6b9e42a

Please sign in to comment.