From b9d61698523b2489fe831e8acaf1684b065a2c17 Mon Sep 17 00:00:00 2001 From: krolxon Date: Sun, 28 Apr 2024 15:53:23 +0530 Subject: [PATCH] highlight current playing songs in lists --- src/connection.rs | 30 +++++++++++++++++++++++++----- src/ui.rs | 31 +++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index 51444dd..2add4e2 100755 --- a/src/connection.rs +++ b/src/connection.rs @@ -19,24 +19,40 @@ pub struct Connection { pub volume: u8, pub repeat: bool, pub random: bool, + pub current_song: Song, } impl Connection { /// Create a new connection pub fn new(addrs: &str) -> Result { let mut conn = Client::connect(addrs).unwrap(); + + let empty_song = Song { + file: "No Song in the Queue".to_string(), + artist: None, + title: None, + duration: None, + last_mod: None, + name: None, + place: None, + range: None, + tags: vec![("".to_string(), "".to_string())], + }; + let songs_filenames: Vec = conn .listall() .unwrap() .into_iter() .map(|x| x.file) .collect(); + let status = conn.status().unwrap(); let (elapsed, total) = status.time.unwrap_or_default(); let volume: u8 = status.volume as u8; let repeat = status.repeat; let random = status.random; + let current_song = conn.currentsong().unwrap_or_else(|_| Some(empty_song.clone())).unwrap_or_else(|| empty_song); Ok(Self { conn, songs_filenames, @@ -46,6 +62,7 @@ impl Connection { volume, repeat, random, + current_song, }) } @@ -76,6 +93,8 @@ impl Connection { /// Update status pub fn update_status(&mut self) { let status = self.conn.status().unwrap(); + let empty_song = self.get_song_with_only_filename("No Song in the Queue"); + let current_song = self.conn.currentsong().unwrap_or_else(|_| Some(empty_song.clone())).unwrap_or_else(|| empty_song); // Playback State match status.state { @@ -97,6 +116,8 @@ impl Connection { // Random mode self.random = status.random; + + self.current_song = current_song; } /// Get progress ratio of current playing song @@ -200,15 +221,14 @@ impl Connection { /// Gives title of current playing song pub fn now_playing(&mut self) -> Result> { - let song = self.conn.currentsong()?.unwrap_or_default(); - if let Some(s) = song.title { - if let Some(a) = song.artist { + if let Some(s) = &self.current_song.title { + if let Some(a) = &self.current_song.artist { return Ok(Some(format!("\"{}\" By {}", s, a))); } else { - return Ok(Some(s)); + return Ok(Some(s.to_string())); } } else { - return Ok(Some(song.file)); + return Ok(Some(self.current_song.file.clone())); } } diff --git a/src/ui.rs b/src/ui.rs index 0bd87af..d2066ad 100755 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,4 +1,5 @@ use crate::app::{App, SelectedTab}; +use clap::builder::styling::RgbColor; use ratatui::{ prelude::*, widgets::{block::Title, *}, @@ -51,12 +52,25 @@ pub fn render(app: &mut App, frame: &mut Frame) { fn draw_directory_browser(frame: &mut Frame, app: &mut App, size: Rect) { let mut song_state = ListState::default(); let total_songs = app.conn.conn.stats().unwrap().songs.to_string(); - let mut list: Vec = vec![]; + let mut list: Vec = vec![]; for (t, s) in app.browser.filetree.iter() { if t == "file" { - list.push(s.to_string()); + let mut status: bool = false; + for sn in app.queue_list.list.iter() { + if sn.contains(s) { + status = true; + } + } + if status { + list.push(ListItem::new(s.clone().bold())); + } else { + list.push(ListItem::new(Line::styled(s, Style::default()))); + } } else { - list.push(format!("[{}]", *s)); + list.push(ListItem::new(Line::styled( + format!("[{}]", *s), + Style::default(), + ))); } } let list = List::new(list) @@ -97,7 +111,16 @@ fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) { Title::from(format!("Volume: {}%", app.conn.volume).bold().green()) .alignment(Alignment::Right), ); - let list = List::new(app.queue_list.list.clone()) + + let mut items: Vec = vec![]; + for item in app.queue_list.list.iter() { + if item.contains(&app.conn.current_song.file) { + items.push(ListItem::new(item.clone().bold())) + } else { + items.push(ListItem::new(item.clone())); + } + } + let list = List::new(items) .block(title.borders(Borders::ALL)) .highlight_style( Style::new()