highlight current playing songs in lists
This commit is contained in:
parent
4cbdd634a9
commit
b9d6169852
|
|
@ -19,24 +19,40 @@ pub struct Connection {
|
||||||
pub volume: u8,
|
pub volume: u8,
|
||||||
pub repeat: bool,
|
pub repeat: bool,
|
||||||
pub random: bool,
|
pub random: bool,
|
||||||
|
pub current_song: Song,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
/// Create a new connection
|
/// Create a new connection
|
||||||
pub fn new(addrs: &str) -> Result<Self> {
|
pub fn new(addrs: &str) -> Result<Self> {
|
||||||
let mut conn = Client::connect(addrs).unwrap();
|
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<String> = conn
|
let songs_filenames: Vec<String> = conn
|
||||||
.listall()
|
.listall()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|x| x.file)
|
.map(|x| x.file)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let status = conn.status().unwrap();
|
let status = conn.status().unwrap();
|
||||||
let (elapsed, total) = status.time.unwrap_or_default();
|
let (elapsed, total) = status.time.unwrap_or_default();
|
||||||
let volume: u8 = status.volume as u8;
|
let volume: u8 = status.volume as u8;
|
||||||
let repeat = status.repeat;
|
let repeat = status.repeat;
|
||||||
let random = status.random;
|
let random = status.random;
|
||||||
|
|
||||||
|
let current_song = conn.currentsong().unwrap_or_else(|_| Some(empty_song.clone())).unwrap_or_else(|| empty_song);
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
conn,
|
conn,
|
||||||
songs_filenames,
|
songs_filenames,
|
||||||
|
|
@ -46,6 +62,7 @@ impl Connection {
|
||||||
volume,
|
volume,
|
||||||
repeat,
|
repeat,
|
||||||
random,
|
random,
|
||||||
|
current_song,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -76,6 +93,8 @@ impl Connection {
|
||||||
/// Update status
|
/// Update status
|
||||||
pub fn update_status(&mut self) {
|
pub fn update_status(&mut self) {
|
||||||
let status = self.conn.status().unwrap();
|
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
|
// Playback State
|
||||||
match status.state {
|
match status.state {
|
||||||
|
|
@ -97,6 +116,8 @@ impl Connection {
|
||||||
|
|
||||||
// Random mode
|
// Random mode
|
||||||
self.random = status.random;
|
self.random = status.random;
|
||||||
|
|
||||||
|
self.current_song = current_song;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get progress ratio of current playing song
|
/// Get progress ratio of current playing song
|
||||||
|
|
@ -200,15 +221,14 @@ impl Connection {
|
||||||
|
|
||||||
/// Gives title of current playing song
|
/// Gives title of current playing song
|
||||||
pub fn now_playing(&mut self) -> Result<Option<String>> {
|
pub fn now_playing(&mut self) -> Result<Option<String>> {
|
||||||
let song = self.conn.currentsong()?.unwrap_or_default();
|
if let Some(s) = &self.current_song.title {
|
||||||
if let Some(s) = song.title {
|
if let Some(a) = &self.current_song.artist {
|
||||||
if let Some(a) = song.artist {
|
|
||||||
return Ok(Some(format!("\"{}\" By {}", s, a)));
|
return Ok(Some(format!("\"{}\" By {}", s, a)));
|
||||||
} else {
|
} else {
|
||||||
return Ok(Some(s));
|
return Ok(Some(s.to_string()));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Ok(Some(song.file));
|
return Ok(Some(self.current_song.file.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
31
src/ui.rs
31
src/ui.rs
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::app::{App, SelectedTab};
|
use crate::app::{App, SelectedTab};
|
||||||
|
use clap::builder::styling::RgbColor;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
widgets::{block::Title, *},
|
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) {
|
fn draw_directory_browser(frame: &mut Frame, app: &mut App, size: Rect) {
|
||||||
let mut song_state = ListState::default();
|
let mut song_state = ListState::default();
|
||||||
let total_songs = app.conn.conn.stats().unwrap().songs.to_string();
|
let total_songs = app.conn.conn.stats().unwrap().songs.to_string();
|
||||||
let mut list: Vec<String> = vec![];
|
let mut list: Vec<ListItem> = vec![];
|
||||||
for (t, s) in app.browser.filetree.iter() {
|
for (t, s) in app.browser.filetree.iter() {
|
||||||
if t == "file" {
|
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 {
|
} else {
|
||||||
list.push(format!("[{}]", *s));
|
list.push(ListItem::new(Line::styled(
|
||||||
|
format!("[{}]", *s),
|
||||||
|
Style::default(),
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let list = List::new(list)
|
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())
|
Title::from(format!("Volume: {}%", app.conn.volume).bold().green())
|
||||||
.alignment(Alignment::Right),
|
.alignment(Alignment::Right),
|
||||||
);
|
);
|
||||||
let list = List::new(app.queue_list.list.clone())
|
|
||||||
|
let mut items: Vec<ListItem> = 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))
|
.block(title.borders(Borders::ALL))
|
||||||
.highlight_style(
|
.highlight_style(
|
||||||
Style::new()
|
Style::new()
|
||||||
|
|
|
||||||
Reference in New Issue