From 61ad0453650f4f0ae44cfec716be02f4fd33b4ca Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Thu, 7 Mar 2024 22:58:54 +0100 Subject: [PATCH] Add colors --- src/core/collection/album.rs | 2 -- src/tui/ui.rs | 48 +++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/core/collection/album.rs b/src/core/collection/album.rs index e56367f..8186f04 100644 --- a/src/core/collection/album.rs +++ b/src/core/collection/album.rs @@ -102,9 +102,7 @@ impl AlbumMonth { pub struct AlbumSeq(pub u8); /// The album's ownership status. -#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Ord, Eq, Hash)] pub enum AlbumStatus { - #[default] None, Owned(TrackFormat), } diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 7caafd2..b147c27 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -9,12 +9,20 @@ use musichoard::collection::{ use ratatui::{ layout::{Alignment, Rect}, style::{Color, Style}, + text::Line, widgets::{Block, BorderType, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap}, Frame, }; use crate::tui::app::{AppPublicState, AppState, Category, IAppAccess, Selection, WidgetState}; +const COLOR_BG: Color = Color::Black; +const COLOR_BG_HL: Color = Color::DarkGray; +const COLOR_FG: Color = Color::White; +const COLOR_FG_ERR: Color = Color::Red; +const COLOR_FG_WARN: Color = Color::LightYellow; +const COLOR_FG_GOOD: Color = Color::LightGreen; + pub trait IUi { fn render(app: &mut APP, frame: &mut Frame); } @@ -280,7 +288,7 @@ impl<'a, 'b> AlbumState<'a, 'b> { let list = List::new( albums .iter() - .map(|a| ListItem::new(a.id.title.as_str())) + .map(Self::to_list_item) .collect::>(), ); @@ -310,6 +318,17 @@ impl<'a, 'b> AlbumState<'a, 'b> { } } + fn to_list_item(album: &Album) -> ListItem { + let line = match album.get_status() { + AlbumStatus::None => Line::raw(album.id.title.as_str()), + AlbumStatus::Owned(format) => match format { + TrackFormat::Mp3 => Line::styled(album.id.title.as_str(), COLOR_FG_WARN), + TrackFormat::Flac => Line::styled(album.id.title.as_str(), COLOR_FG_GOOD), + }, + }; + ListItem::new(line) + } + fn display_album_date(date: &AlbumDate) -> String { if date.month.is_none() { format!("{}", date.year) @@ -463,11 +482,11 @@ pub struct Ui; impl Ui { fn style(_active: bool, error: bool) -> Style { - let style = Style::default().bg(Color::Black); + let style = Style::default().bg(COLOR_BG); if error { - style.fg(Color::Red) + style.fg(COLOR_FG_ERR) } else { - style.fg(Color::White) + style.fg(COLOR_FG) } } @@ -476,10 +495,11 @@ impl Ui { } fn highlight_style(active: bool) -> Style { + // Do not set foreground colour to not overwrite any list-specific customisation. if active { - Style::default().fg(Color::White).bg(Color::DarkGray) + Style::default().bg(COLOR_BG_HL) } else { - Self::style(false, false) + Style::default().bg(COLOR_BG) } } @@ -721,6 +741,8 @@ impl IUi for Ui { #[cfg(test)] mod tests { + use musichoard::collection::{album::AlbumId, artist::ArtistId}; + use crate::tui::{ app::{AppPublic, AppPublicInner, Delta}, testmod::COLLECTION, @@ -839,6 +861,20 @@ mod tests { draw_test_suite(&artists, &mut selection); } + #[test] + fn empty_album() { + let mut artists: Vec = vec![Artist::new(ArtistId::new("An artist"))]; + artists[0].albums.push(Album { + id: AlbumId::new("An album"), + date: AlbumDate::default(), + seq: AlbumSeq::default(), + tracks: vec![], + }); + let mut selection = Selection::new(&artists); + + draw_test_suite(&artists, &mut selection); + } + #[test] fn collection() { let artists = &COLLECTION;