Krolyxon
Commit b9d6169

rmptuib9d6169

highlight current playing songs in lists

Author
krolxon
Date
2024-04-28 15:53 +0530
Parent
4cbdd63
Commit
b9d61698523b2489fe831e8acaf1684b065a2c17

+52 −9

2 files changed

  1. M src/connection.rs +25 −5
  2. M src/ui.rs +27 −4
M src/connection.rs View
@@ -19,24 +19,40 @@ pub struct Connection {
1919 pub volume: u8,
2020 pub repeat: bool,
2121 pub random: bool,
22+ pub current_song: Song,
2223 }
2324
2425 impl Connection {
2526 /// Create a new connection
2627 pub fn new(addrs: &str) -> Result<Self> {
2728 let mut conn = Client::connect(addrs).unwrap();
29+
30+ let empty_song = Song {
31+ file: "No Song in the Queue".to_string(),
32+ artist: None,
33+ title: None,
34+ duration: None,
35+ last_mod: None,
36+ name: None,
37+ place: None,
38+ range: None,
39+ tags: vec![("".to_string(), "".to_string())],
40+ };
41+
2842 let songs_filenames: Vec<String> = conn
2943 .listall()
3044 .unwrap()
3145 .into_iter()
3246 .map(|x| x.file)
3347 .collect();
48+
3449 let status = conn.status().unwrap();
3550 let (elapsed, total) = status.time.unwrap_or_default();
3651 let volume: u8 = status.volume as u8;
3752 let repeat = status.repeat;
3853 let random = status.random;
3954
55+ let current_song = conn.currentsong().unwrap_or_else(|_| Some(empty_song.clone())).unwrap_or_else(|| empty_song);
4056 Ok(Self {
4157 conn,
4258 songs_filenames,
@@ -46,6 +62,7 @@ impl Connection {
4662 volume,
4763 repeat,
4864 random,
65+ current_song,
4966 })
5067 }
5168
@@ -76,6 +93,8 @@ impl Connection {
7693 /// Update status
7794 pub fn update_status(&mut self) {
7895 let status = self.conn.status().unwrap();
96+ let empty_song = self.get_song_with_only_filename("No Song in the Queue");
97+ let current_song = self.conn.currentsong().unwrap_or_else(|_| Some(empty_song.clone())).unwrap_or_else(|| empty_song);
7998
8099 // Playback State
81100 match status.state {
@@ -97,6 +116,8 @@ impl Connection {
97116
98117 // Random mode
99118 self.random = status.random;
119+
120+ self.current_song = current_song;
100121 }
101122
102123 /// Get progress ratio of current playing song
@@ -200,15 +221,14 @@ impl Connection {
200221
201222 /// Gives title of current playing song
202223 pub fn now_playing(&mut self) -> Result<Option<String>> {
203− let song = self.conn.currentsong()?.unwrap_or_default();
204− if let Some(s) = song.title {
205− if let Some(a) = song.artist {
224+ if let Some(s) = &self.current_song.title {
225+ if let Some(a) = &self.current_song.artist {
206226 return Ok(Some(format!("\"{}\" By {}", s, a)));
207227 } else {
208− return Ok(Some(s));
228+ return Ok(Some(s.to_string()));
209229 }
210230 } else {
211− return Ok(Some(song.file));
231+ return Ok(Some(self.current_song.file.clone()));
212232 }
213233 }
214234
M src/ui.rs View
@@ -1,4 +1,5 @@
11 use crate::app::{App, SelectedTab};
2+use clap::builder::styling::RgbColor;
23 use ratatui::{
34 prelude::*,
45 widgets::{block::Title, *},
@@ -51,12 +52,25 @@ pub fn render(app: &mut App, frame: &mut Frame) {
5152 fn draw_directory_browser(frame: &mut Frame, app: &mut App, size: Rect) {
5253 let mut song_state = ListState::default();
5354 let total_songs = app.conn.conn.stats().unwrap().songs.to_string();
54− let mut list: Vec<String> = vec![];
55+ let mut list: Vec<ListItem> = vec![];
5556 for (t, s) in app.browser.filetree.iter() {
5657 if t == "file" {
57− list.push(s.to_string());
58+ let mut status: bool = false;
59+ for sn in app.queue_list.list.iter() {
60+ if sn.contains(s) {
61+ status = true;
62+ }
63+ }
64+ if status {
65+ list.push(ListItem::new(s.clone().bold()));
66+ } else {
67+ list.push(ListItem::new(Line::styled(s, Style::default())));
68+ }
5869 } else {
59− list.push(format!("[{}]", *s));
70+ list.push(ListItem::new(Line::styled(
71+ format!("[{}]", *s),
72+ Style::default(),
73+ )));
6074 }
6175 }
6276 let list = List::new(list)
@@ -97,7 +111,16 @@ fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) {
97111 Title::from(format!("Volume: {}%", app.conn.volume).bold().green())
98112 .alignment(Alignment::Right),
99113 );
100− let list = List::new(app.queue_list.list.clone())
114+
115+ let mut items: Vec<ListItem> = vec![];
116+ for item in app.queue_list.list.iter() {
117+ if item.contains(&app.conn.current_song.file) {
118+ items.push(ListItem::new(item.clone().bold()))
119+ } else {
120+ items.push(ListItem::new(item.clone()));
121+ }
122+ }
123+ let list = List::new(items)
101124 .block(title.borders(Borders::ALL))
102125 .highlight_style(
103126 Style::new()