Add colors
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 1m46s
Cargo CI / Lint (pull_request) Successful in 1m14s

This commit is contained in:
Wojciech Kozlowski 2024-03-07 22:58:54 +01:00
parent 66913b8e76
commit 61ad045365
2 changed files with 42 additions and 8 deletions

View File

@ -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),
}

View File

@ -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: IAppAccess>(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::<Vec<ListItem>>(),
);
@ -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<Artist> = 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;