From ac53e8cc6572024c37e56655267025abee1bffab Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 17 Mar 2024 18:04:03 +0100 Subject: [PATCH] Add type to album display --- src/tui/ui.rs | 82 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 13 deletions(-) diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 8949e13..8281f65 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use musichoard::collection::{ - album::{Album, AlbumDate, AlbumSeq, AlbumStatus}, + album::{Album, AlbumDate, AlbumPrimaryType, AlbumSecondaryType, AlbumSeq, AlbumStatus}, artist::Artist, musicbrainz::IMusicBrainzRef, track::{Track, TrackFormat, TrackQuality}, @@ -296,14 +296,15 @@ impl<'a, 'b> AlbumState<'a, 'b> { let album = state.list.selected().map(|i| &albums[i]); let info = Paragraph::new(format!( "Title: {}\n\ - Date: {}{}\n\ + Date: {}\n\ + Type: {}\n\ Status: {}", album.map(|a| a.id.title.as_str()).unwrap_or(""), album - .map(|a| Self::display_album_date(&a.date)) + .map(|a| Self::display_date(&a.date, &a.seq)) .unwrap_or_default(), album - .map(|a| Self::display_album_seq(&a.seq)) + .map(|a| Self::display_type(&a.primary_type, &a.secondary_types)) .unwrap_or_default(), album .map(|a| Self::display_album_status(&a.get_status())) @@ -329,6 +330,14 @@ impl<'a, 'b> AlbumState<'a, 'b> { ListItem::new(line) } + fn display_date(date: &AlbumDate, seq: &AlbumSeq) -> String { + if seq.0 > 0 { + format!("{} ({})", Self::display_album_date(date), seq.0) + } else { + format!("{}", Self::display_album_date(date)) + } + } + fn display_album_date(date: &AlbumDate) -> String { match date.year { Some(year) => match date.month { @@ -342,14 +351,57 @@ impl<'a, 'b> AlbumState<'a, 'b> { } } - fn display_album_seq(seq: &AlbumSeq) -> String { - if seq.0 > 0 { - format!(" ({})", seq.0) - } else { - String::new() + fn display_type( + primary: &Option, + secondary: &Vec, + ) -> String { + match primary { + Some(ref primary) => { + if secondary.is_empty() { + format!("{}", Self::display_primary_type(primary)) + } else { + format!( + "{} ({})", + Self::display_primary_type(primary), + Self::display_secondary_types(secondary) + ) + } + } + None => String::default(), } } + fn display_primary_type(value: &AlbumPrimaryType) -> &'static str { + match value { + AlbumPrimaryType::Album => "Album", + AlbumPrimaryType::Single => "Single", + AlbumPrimaryType::Ep => "EP", + AlbumPrimaryType::Broadcast => "Broadcast", + AlbumPrimaryType::Other => "Other", + } + } + + fn display_secondary_types(values: &Vec) -> String { + let mut types: Vec<&'static str> = vec![]; + for value in values { + match value { + AlbumSecondaryType::Compilation => types.push("Compilation"), + AlbumSecondaryType::Soundtrack => types.push("Soundtrack"), + AlbumSecondaryType::Spokenword => types.push("Spokenword"), + AlbumSecondaryType::Interview => types.push("Interview"), + AlbumSecondaryType::Audiobook => types.push("Audiobook"), + AlbumSecondaryType::AudioDrama => types.push("Audio drama"), + AlbumSecondaryType::Live => types.push("Live"), + AlbumSecondaryType::Remix => types.push("Remix"), + AlbumSecondaryType::DjMix => types.push("DJ-mix"), + AlbumSecondaryType::MixtapeStreet => types.push("Mixtape/Street"), + AlbumSecondaryType::Demo => types.push("Demo"), + AlbumSecondaryType::FieldRecording => types.push("Field recording"), + } + } + types.join(", ") + } + fn display_album_status(status: &AlbumStatus) -> &'static str { match status { AlbumStatus::None => "None", @@ -818,10 +870,14 @@ mod tests { } #[test] - fn display_album_seq() { - assert_eq!(AlbumState::display_album_seq(&AlbumSeq::default()), ""); - assert_eq!(AlbumState::display_album_seq(&AlbumSeq(0)), ""); - assert_eq!(AlbumState::display_album_seq(&AlbumSeq(5)), " (5)"); + fn display_date() { + let date: AlbumDate = 1990.into(); + assert_eq!( + AlbumState::display_date(&date, &AlbumSeq::default()), + "1990" + ); + assert_eq!(AlbumState::display_date(&date, &AlbumSeq(0)), "1990"); + assert_eq!(AlbumState::display_date(&date, &AlbumSeq(5)), "1990 (5)"); } #[test]