diff options
author | Toby Vincent <tobyv@tobyvin.dev> | 2024-07-16 17:09:27 -0500 |
---|---|---|
committer | Toby Vincent <tobyv@tobyvin.dev> | 2024-07-16 17:09:27 -0500 |
commit | 9638edcbafea29b76f019ff73e2347cb7c758ba0 (patch) | |
tree | 738adda62ff27798ebf43635665a889ad1dcd1f1 /src/printer.rs | |
parent | 3e7721dfbaeb0b57f48801660a64c13989643198 (diff) |
more wip
Diffstat (limited to 'src/printer.rs')
-rw-r--r-- | src/printer.rs | 107 |
1 files changed, 34 insertions, 73 deletions
diff --git a/src/printer.rs b/src/printer.rs index a609616..db5a554 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -2,19 +2,15 @@ use std::{collections::HashMap, io::Write, sync::Arc, time::Duration}; use futures_util::stream::StreamExt; use tokio::{ - sync::{mpsc::Receiver, Mutex, Notify}, + sync::{Mutex, Notify}, task::{JoinHandle, JoinSet}, }; -use unicode_segmentation::UnicodeSegmentation; use zbus::{proxy::PropertyStream, zvariant::OwnedValue}; -use crate::Result; use crate::{ - dbus::{ - media_player2::MediaPlayer2Proxy, - player::{PlaybackStatus, PlayerProxy}, - }, + dbus::player::{PlaybackStatus, PlayerProxy}, i3bar::Block, + Result, }; type StatusLock = Arc<Mutex<Status>>; @@ -30,7 +26,7 @@ pub enum Signal { Print, } -pub async fn printer(proxy: PlayerProxy<'static>, rx_click: Receiver<(u8, usize)>) -> Result<()> { +pub async fn printer(proxy: PlayerProxy<'static>) -> Result<()> { let mut join_set: JoinSet<Result<()>> = JoinSet::new(); let mut rotator = None; @@ -86,12 +82,39 @@ pub async fn printer(proxy: PlayerProxy<'static>, rx_click: Receiver<(u8, usize) status.clone(), )); - join_set.spawn(click_listener(proxy, rx_click, status.clone())); - loop { notify.notified().await; let mut status = status.lock().await; + //let (color, background) = match proxy.playback_status().await.ok() { + // Some(PlaybackStatus::Playing) => (Some(BLACK.into()), Some(CYAN.into())), + // Some(PlaybackStatus::Paused) => (Some(BLACK.into()), Some(YELLOW.into())), + // Some(PlaybackStatus::Stopped) => (None, None), + //}; + + //let s = match instance { + // Instance::Icon => " ", + // Instance::Title => &status.title, + // Instance::Prev => " ", + // Instance::Play => " ", + // Instance::Pause => " ", + // Instance::Next => " ", + // Instance::Volume => { + // if let Some(volume) = self.volume.map(|v| (v * 100_f64) as u32) { + // match volume { + // v @ 66.. => &format!(" {v}% "), + // v @ 33.. => &format!(" {v}% "), + // v @ 0.. => &format!(" {v}% "), + // } + // } + // } + //}; + // + //let block = Block { + // full_text, + // ..Default::default() + //}; + let mut w = std::io::stdout().lock(); let mut v = serde_json::to_vec(&status.build()).unwrap(); v.push(b'\n'); @@ -100,48 +123,6 @@ pub async fn printer(proxy: PlayerProxy<'static>, rx_click: Receiver<(u8, usize) } } -async fn click_listener( - player_proxy: PlayerProxy<'static>, - mut rx: Receiver<(u8, usize)>, - status: StatusLock, -) -> Result<()> { - let mpris_proxy = MediaPlayer2Proxy::builder(player_proxy.inner().connection()) - .destination(player_proxy.inner().destination())? - .build() - .await?; - - while let Some((button, index)) = dbg!(rx.recv().await) { - let status = status.lock().await; - if let Some(c) = dbg!(&status.indexes) - .iter() - .rev() - .find_map(|(i, b)| (*i <= index).then_some(b)) - { - match dbg!((c, button)) { - (Component::Icon, 1) if mpris_proxy.can_raise().await? => { - mpris_proxy.raise().await? - } - (Component::Title, _) => {} - (Component::Prev, 1) => player_proxy.previous().await?, - (Component::Play, 1) | (Component::Pause, 1) => player_proxy.play_pause().await?, - (Component::Next, 1) => player_proxy.next().await?, - (Component::Volume, 4) => { - player_proxy - .set_volume(player_proxy.volume().await? - 0.05) - .await? - } - (Component::Volume, 5) => { - player_proxy - .set_volume(player_proxy.volume().await? + 0.05) - .await? - } - _ => {} - } - } - } - Ok(()) -} - async fn process_metadata( notify: Arc<Notify>, status_lock: StatusLock, @@ -230,7 +211,7 @@ async fn playback_state( let mut status = status_lock.lock().await; status.playback_status = val; notify.notify_one(); - }; + } } Ok(()) } @@ -290,45 +271,28 @@ pub struct Status { impl Status { fn build(&mut self) -> Block { let mut full_text = String::new(); - self.indexes = Vec::new(); - - self.indexes - .push((full_text.chars().count(), Component::Icon)); full_text.push_str(" "); if let Some(title) = self.title.as_ref() { - self.indexes - .push((full_text.chars().count(), Component::Title)); full_text.push_str(title); full_text.push(' '); } if self.can_go_previous { - self.indexes - .push((full_text.chars().count(), Component::Prev)); full_text.push_str(" "); } if self.playback_status == PlaybackStatus::Playing { - self.indexes - .push((full_text.chars().count(), Component::Pause)); full_text.push_str(" "); } else { - self.indexes - .push((full_text.chars().count(), Component::Play)); full_text.push_str(" "); } if self.can_go_next { - self.indexes - .push((full_text.chars().count(), Component::Next)); full_text.push_str(" "); } if let Some(volume) = self.volume.map(|v| (v * 100_f64) as u32) { - self.indexes - .push((full_text.chars().count(), Component::Volume)); - match volume { v @ 66.. => full_text.push_str(&format!(" {v}% ")), v @ 33.. => full_text.push_str(&format!(" {v}% ")), @@ -336,9 +300,6 @@ impl Status { } } - self.indexes - .push((full_text.chars().count(), Component::Space)); - let (color, background) = match self.playback_status { PlaybackStatus::Playing => (Some(BLACK.into()), Some(CYAN.into())), PlaybackStatus::Paused => (Some(BLACK.into()), Some(YELLOW.into())), |