Print new sequence information in tui
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 1m44s
Cargo CI / Lint (pull_request) Successful in 1m14s

This commit is contained in:
Wojciech Kozlowski 2024-03-05 23:20:23 +01:00
parent 930cb5c0ec
commit dd00a90a10
2 changed files with 43 additions and 7 deletions

View File

@ -40,6 +40,18 @@ pub struct AlbumDate {
pub day: u8, pub day: u8,
} }
impl Display for AlbumDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.month.is_none() {
write!(f, "{}", self.year)
} else if self.day == 0 {
write!(f, "{}{:02}", self.year, self.month as u8)
} else {
write!(f, "{}{:02}{:02}", self.year, self.month as u8, self.day)
}
}
}
/// The album's sequence to determine order when two or more albums have the same release date. /// The album's sequence to determine order when two or more albums have the same release date.
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Ord, Eq, Hash)] #[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Ord, Eq, Hash)]
pub struct AlbumSeq(pub u8); pub struct AlbumSeq(pub u8);
@ -83,6 +95,12 @@ impl From<u8> for AlbumMonth {
} }
} }
impl AlbumMonth {
fn is_none(&self) -> bool {
matches!(self, AlbumMonth::None)
}
}
impl Album { impl Album {
pub fn get_sort_key(&self) -> (&AlbumDate, &AlbumSeq, &AlbumId) { pub fn get_sort_key(&self) -> (&AlbumDate, &AlbumSeq, &AlbumId) {
(&self.date, &self.seq, &self.id) (&self.date, &self.seq, &self.id)
@ -148,6 +166,16 @@ mod tests {
use super::*; use super::*;
impl AlbumDate {
fn new<M: Into<AlbumMonth>>(year: u32, month: M, day: u8) -> Self {
AlbumDate {
year,
month: month.into(),
day,
}
}
}
#[test] #[test]
fn album_month() { fn album_month() {
assert_eq!(<u8 as Into<AlbumMonth>>::into(0), AlbumMonth::None); assert_eq!(<u8 as Into<AlbumMonth>>::into(0), AlbumMonth::None);
@ -167,13 +195,17 @@ mod tests {
assert_eq!(<u8 as Into<AlbumMonth>>::into(255), AlbumMonth::None); assert_eq!(<u8 as Into<AlbumMonth>>::into(255), AlbumMonth::None);
} }
#[test]
fn album_display() {
assert_eq!(AlbumDate::default().to_string(), "0");
assert_eq!(AlbumDate::new(1990, 0, 0).to_string(), "1990");
assert_eq!(AlbumDate::new(1990, 5, 0).to_string(), "199005");
assert_eq!(AlbumDate::new(1990, 5, 6).to_string(), "19900506");
}
#[test] #[test]
fn same_date_seq_cmp() { fn same_date_seq_cmp() {
let date = AlbumDate { let date = AlbumDate::new(2024, 3, 2);
year: 2024,
month: AlbumMonth::March,
day: 2,
};
let album_id_1 = AlbumId { let album_id_1 = AlbumId {
title: String::from("album z"), title: String::from("album z"),

View File

@ -287,9 +287,13 @@ impl<'a, 'b> AlbumState<'a, 'b> {
let album = state.list.selected().map(|i| &albums[i]); let album = state.list.selected().map(|i| &albums[i]);
let info = Paragraph::new(format!( let info = Paragraph::new(format!(
"Title: {}\n\ "Title: {}\n\
Year: {}", Date: {}{}",
album.map(|a| a.id.title.as_str()).unwrap_or(""), album.map(|a| a.id.title.as_str()).unwrap_or(""),
album.map(|a| a.date.year.to_string()).unwrap_or_default(), album.map(|a| a.date.to_string()).unwrap_or_default(),
album
.filter(|a| a.seq.0 > 0)
.map(|a| format!(" ({})", a.seq.0))
.unwrap_or_default()
)); ));
AlbumState { AlbumState {