From 0fe8504b8ce452f5fce42c17a380978f0aa820be Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Mon, 26 Aug 2024 16:10:26 +0200 Subject: [PATCH] Small improvements --- src/tui/app/machine/browse.rs | 18 ++++++++++++++---- src/tui/app/machine/matches.rs | 21 +++++++++++++++------ src/tui/app/mod.rs | 1 + src/tui/ui.rs | 21 ++++++++++++++++----- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/tui/app/machine/browse.rs b/src/tui/app/machine/browse.rs index 0818b37..1101985 100644 --- a/src/tui/app/machine/browse.rs +++ b/src/tui/app/machine/browse.rs @@ -8,7 +8,7 @@ use musichoard::{ use crate::tui::{ app::{ - machine::{App, AppInner, AppMachine}, + machine::{matches::AppMatchInfo, App, AppInner, AppMachine}, selection::{Delta, ListSelection}, AppPublic, AppState, IAppInteractBrowse, }, @@ -119,13 +119,23 @@ impl IAppInteractBrowse for AppMachine { } let mut artist_album_matches = vec![]; - for album in artist.albums.iter() { + let mut album_iter = artist.albums.iter().peekable(); + while let Some(album) = album_iter.next() { + if album.musicbrainz.is_some() { + continue; + } + match api.search_release_group(arid, album) { - Ok(matches) => artist_album_matches.push(matches), + Ok(matches) => artist_album_matches.push(AppMatchInfo { + matching: album.clone(), + matches, + }), Err(err) => return AppMachine::error(self.inner, err.to_string()).into(), } - thread::sleep(time::Duration::from_secs(1)); + if album_iter.peek().is_some() { + thread::sleep(time::Duration::from_secs(1)); + } } AppMachine::matches(self.inner, artist_album_matches).into() diff --git a/src/tui/app/machine/matches.rs b/src/tui/app/machine/matches.rs index 297c455..c56abfe 100644 --- a/src/tui/app/machine/matches.rs +++ b/src/tui/app/machine/matches.rs @@ -10,18 +10,23 @@ use crate::tui::{ lib::IMusicHoard, }; +pub struct AppMatchInfo { + pub matching: Album, + pub matches: Vec>, +} + pub struct AppMatches { - matches: Vec>>, + matches: Vec, index: usize, state: WidgetState, } impl AppMachine { - pub fn matches(inner: AppInner, matches: Vec>>) -> Self { + pub fn matches(inner: AppInner, matches: Vec) -> Self { assert!(!matches.is_empty()); let mut state = WidgetState::default(); - if !matches[0].is_empty() { + if !matches[0].matches.is_empty() { state.list.select(Some(0)); } @@ -47,7 +52,8 @@ impl<'a, MH: IMusicHoard> From<&'a mut AppMachine> for AppPublic AppPublic { inner: (&mut machine.inner).into(), state: AppState::Matches(AppPublicMatches { - matches: &machine.state.matches[machine.state.index], + matching: &machine.state.matches[machine.state.index].matching, + matches: &machine.state.matches[machine.state.index].matches, state: &mut machine.state.state, }), } @@ -69,7 +75,10 @@ impl IAppInteractMatches for AppMachine { fn next_match(mut self) -> Self::APP { if let Some(index) = self.state.state.list.selected() { let result = index.saturating_add(1); - let to = cmp::min(result, self.state.matches[self.state.index].len() - 1); + let to = cmp::min( + result, + self.state.matches[self.state.index].matches.len() - 1, + ); self.state.state.list.select(Some(to)); } @@ -80,7 +89,7 @@ impl IAppInteractMatches for AppMachine { self.state.index = self.state.index.saturating_add(1); if self.state.index < self.state.matches.len() { self.state.state = WidgetState::default(); - if !self.state.matches[self.state.index].is_empty() { + if !self.state.matches[self.state.index].matches.is_empty() { self.state.state.list.select(Some(0)); } self.into() diff --git a/src/tui/app/mod.rs b/src/tui/app/mod.rs index 35cab66..8aec4aa 100644 --- a/src/tui/app/mod.rs +++ b/src/tui/app/mod.rs @@ -131,6 +131,7 @@ pub struct AppPublicInner<'app> { } pub struct AppPublicMatches<'app> { + pub matching: &'app Album, pub matches: &'app [Match], pub state: &'app mut WidgetState, } diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 741f4ab..9891904 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -529,12 +529,16 @@ impl Minibuffer<'_> { ], columns, }, - AppState::Matches(_) => Minibuffer { + AppState::Matches(public) => Minibuffer { paragraphs: vec![ - Paragraph::new("enter: apply highlighted"), + Paragraph::new(format!( + "Matching: {} | {}", + AlbumState::display_album_date(&public.matching.date), + &public.matching.id.title + )), Paragraph::new("q: abort"), ], - columns, + columns: 2, }, AppState::Error(_) => Minibuffer { paragraphs: vec![Paragraph::new( @@ -816,6 +820,7 @@ impl Ui { } fn render_matches_overlay( + matching: &Album, matches: &[Match], state: &mut WidgetState, frame: &mut Frame, @@ -837,7 +842,12 @@ impl Ui { .collect::>(), ); - Self::render_overlay_list_widget("Matches", list, state, true, area, frame) + let matching_string = format!( + "Matching: {} | {}", + AlbumState::display_album_date(&matching.date), + &matching.id.title + ); + Self::render_overlay_list_widget(&matching_string, list, state, true, area, frame) } fn render_reload_overlay(frame: &mut Frame) { @@ -876,7 +886,7 @@ impl IUi for Ui { match state { AppState::Info(_) => Self::render_info_overlay(collection, selection, frame), AppState::Matches(public) => { - Self::render_matches_overlay(public.matches, public.state, frame) + Self::render_matches_overlay(public.matching, public.matches, public.state, frame) } AppState::Reload(_) => Self::render_reload_overlay(frame), AppState::Error(msg) => Self::render_error_overlay("Error", msg, frame), @@ -912,6 +922,7 @@ mod tests { AppState::Reload(()) => AppState::Reload(()), AppState::Search(s) => AppState::Search(s), AppState::Matches(ref mut m) => AppState::Matches(AppPublicMatches { + matching: m.matching, matches: &m.matches, state: &mut m.state, }),