aboutsummaryrefslogtreecommitdiff
path: root/src/connection.rs
diff options
context:
space:
mode:
authorkrolxon <krolyxon@tutanota.com>2024-04-28 15:53:23 +0530
committerkrolxon <krolyxon@tutanota.com>2024-04-28 15:53:23 +0530
commitb9d61698523b2489fe831e8acaf1684b065a2c17 (patch)
tree08ac98f0852cf56cf8d38ba25f4a92e8361b6653 /src/connection.rs
parent4cbdd634a907d6f78a767cdf844137e40a7650bc (diff)
highlight current playing songs in lists
Diffstat (limited to 'src/connection.rs')
-rwxr-xr-xsrc/connection.rs30
1 files changed, 25 insertions, 5 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<Self> {
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
.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<Option<String>> {
- 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()));
}
}