diff options
| author | krolxon <krolyxon@tutanota.com> | 2024-04-25 12:58:13 +0530 |
|---|---|---|
| committer | krolxon <krolyxon@tutanota.com> | 2024-04-25 12:58:13 +0530 |
| commit | f665c4e9f3b4eaa226b0c813f6845e247a4e6977 (patch) | |
| tree | 03119ed4587357a34f3728700f3a3e19ac89e1b2 /src/connection.rs | |
| parent | 4bc03ce8f4dea2ed6d68f2694bd095c9e3857e5e (diff) | |
better event handling with tick, search
Diffstat (limited to 'src/connection.rs')
| -rwxr-xr-x | src/connection.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/connection.rs b/src/connection.rs index b464b23..a741206 100755 --- a/src/connection.rs +++ b/src/connection.rs @@ -2,6 +2,7 @@ use mpd::song::Song; use mpd::{Client, State}; use simple_dmenu::dmenu; use std::process::Command; +use std::time::Duration; pub type Result<T> = core::result::Result<T, Error>; pub type Error = Box<dyn std::error::Error>; @@ -11,6 +12,8 @@ pub struct Connection { pub conn: Client, pub songs_filenames: Vec<String>, pub state: String, + pub elapsed: Duration, + pub total_duration: Duration, } impl Connection { @@ -24,10 +27,14 @@ impl Connection { .map(|x| x.file) .collect(); + let (elapsed, total) = conn.status().unwrap().time.unwrap_or_default(); + Ok(Self { conn, songs_filenames, state: "Stopped".to_string(), + elapsed, + total_duration: total, }) } @@ -64,6 +71,26 @@ impl Connection { self.state.clone() } + pub fn update_progress(&mut self) { + let (elapsed, total) = self.conn.status().unwrap().time.unwrap_or_default(); + self.elapsed = elapsed; + self.total_duration = total; + } + + pub fn get_progress_ratio(&self) -> f64 { + let total = self.total_duration.as_secs_f64(); + if total == 0.0 { + 0.0 + } else { + let ratio = self.elapsed.as_secs_f64() / self.total_duration.as_secs_f64(); + if ratio > 1.0 || ratio == 0.0 { + 1.0 + } else { + ratio + } + } + } + /// push the given song to queue pub fn push(&mut self, song: &Song) -> Result<()> { if self.conn.queue().unwrap().is_empty() { |
