From 9680a9375f3faa3ae3f6f4b78957de0ea2383dae Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sat, 20 Jul 2024 02:05:37 -0500 Subject: fix: clear on change and improve spacing --- src/bin/mpris-icon.rs | 31 +++++++++++++++++++++++-------- src/bin/mpris-next.rs | 6 ++---- src/bin/mpris-play.rs | 4 +--- src/bin/mpris-prev.rs | 6 ++---- src/bin/mpris-title.rs | 8 +++----- src/bin/mpris-volume.rs | 10 ++++------ src/lib.rs | 30 +++++++++++++++++------------- 7 files changed, 52 insertions(+), 43 deletions(-) diff --git a/src/bin/mpris-icon.rs b/src/bin/mpris-icon.rs index cd1280f..39677f5 100644 --- a/src/bin/mpris-icon.rs +++ b/src/bin/mpris-icon.rs @@ -1,29 +1,44 @@ use i3blocks::{ dbus::{media_player2::MediaPlayer2Proxy, player::PlaybackStatus, playerctld::PlayerctldProxy}, i3bar::{Block, Click}, - Button, Component, Error, + Button, Component, Error, Update, }; use zbus::Connection; #[tokio::main] async fn main() -> Result<(), main_error::MainError> { - i3blocks::run::(Block { - full_text: " 󰝚 ".into(), - ..Default::default() - }) - .await - .map_err(Into::into) + i3blocks::run::().await.map_err(Into::into) } pub struct Icon; impl Component for Icon { const NAME: &'static str = "icon"; - type Updater = (); + type Updater = Self; type Colorer = PlaybackStatus; type Handler = Self; } +impl Update for Icon { + type Value = String; + + async fn listen( + tx: tokio::sync::mpsc::Sender, + _: i3blocks::dbus::player::PlayerProxy<'_>, + ) -> Result<(), Error> { + tx.send(" 󰝚 ".into()).await.map_err(Into::into) + } + + async fn update( + value: Self::Value, + block: std::sync::Arc>, + ) -> Result { + let mut block = block.lock().await; + block.full_text = value; + Ok(true) + } +} + impl Button for Icon { async fn handle(conn: Connection, click: Click) -> Result<(), Error> { let Some(name) = click.instance else { diff --git a/src/bin/mpris-next.rs b/src/bin/mpris-next.rs index 2e3fd95..bdd8538 100644 --- a/src/bin/mpris-next.rs +++ b/src/bin/mpris-next.rs @@ -10,9 +10,7 @@ use zbus::Connection; #[tokio::main] async fn main() -> Result<(), main_error::MainError> { - i3blocks::run::(Default::default()) - .await - .map_err(Into::into) + i3blocks::run::().await.map_err(Into::into) } pub struct Next; @@ -42,7 +40,7 @@ impl Update for Next { async fn update(value: Self::Value, block: Arc>) -> Result { let mut block = block.lock().await; - block.full_text = value.then_some(" 󰒭 ".into()).unwrap_or_default(); + block.full_text = value.then_some(" 󰒭 ".into()).unwrap_or_default(); Ok(true) } } diff --git a/src/bin/mpris-play.rs b/src/bin/mpris-play.rs index 0477887..0656bb9 100644 --- a/src/bin/mpris-play.rs +++ b/src/bin/mpris-play.rs @@ -10,9 +10,7 @@ use zbus::Connection; #[tokio::main] async fn main() -> Result<(), main_error::MainError> { - i3blocks::run::(Default::default()) - .await - .map_err(Into::into) + i3blocks::run::().await.map_err(Into::into) } pub struct Play; diff --git a/src/bin/mpris-prev.rs b/src/bin/mpris-prev.rs index 82b85ee..7c6ecee 100644 --- a/src/bin/mpris-prev.rs +++ b/src/bin/mpris-prev.rs @@ -10,9 +10,7 @@ use zbus::Connection; #[tokio::main] async fn main() -> Result<(), main_error::MainError> { - i3blocks::run::(Default::default()) - .await - .map_err(Into::into) + i3blocks::run::().await.map_err(Into::into) } pub struct Previous; @@ -42,7 +40,7 @@ impl Update for Previous { async fn update(value: Self::Value, block: Arc>) -> Result { let mut block = block.lock().await; - block.full_text = value.then_some("󰒮 ".into()).unwrap_or_default(); + block.full_text = value.then_some(" 󰒮 ".into()).unwrap_or_default(); Ok(true) } } diff --git a/src/bin/mpris-title.rs b/src/bin/mpris-title.rs index 1428583..1976f82 100644 --- a/src/bin/mpris-title.rs +++ b/src/bin/mpris-title.rs @@ -15,9 +15,7 @@ const TICK_RATE: Duration = Duration::from_millis(500); #[tokio::main] async fn main() -> Result<(), main_error::MainError> { - i3blocks::run::(Default::default()) - .await - .map_err(Into::into) + i3blocks::run::<Title>().await.map_err(Into::into) } pub struct Title; @@ -98,9 +96,9 @@ impl Title { *old_title = Some(title.clone()); if title.len() >= 10 { - let mut chars = title.clone().chars().collect::<Vec<char>>(); let tx = tx.clone(); - + let mut chars = title.clone().chars().collect::<Vec<char>>(); + chars.push(' '); *rotator = Some(join_set.spawn(async move { let mut interval = tokio::time::interval(TICK_RATE); loop { diff --git a/src/bin/mpris-volume.rs b/src/bin/mpris-volume.rs index b030247..c6a457e 100644 --- a/src/bin/mpris-volume.rs +++ b/src/bin/mpris-volume.rs @@ -10,9 +10,7 @@ use zbus::Connection; #[tokio::main] async fn main() -> Result<(), main_error::MainError> { - i3blocks::run::<Volume>(Default::default()) - .await - .map_err(Into::into) + i3blocks::run::<Volume>().await.map_err(Into::into) } pub struct Volume; @@ -43,9 +41,9 @@ impl Update for Volume { async fn update(value: Self::Value, block: Arc<Mutex<Block>>) -> Result<bool, Error> { let mut block = block.lock().await; block.full_text = match (value * 100_f64) as u32 { - v @ 66.. => format!("󰕾 {v}% "), - v @ 33.. => format!("󰖀 {v}% "), - v @ 0.. => format!("󰕿 {v}% "), + v @ 66.. => format!(" 󰕾 {v}% "), + v @ 33.. => format!(" 󰖀 {v}% "), + v @ 0.. => format!(" 󰕿 {v}% "), }; Ok(true) } diff --git a/src/lib.rs b/src/lib.rs index d369be5..aeefa01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,27 +20,28 @@ pub mod i3bar; const IGNORED: [&str; 2] = ["playerctld", "kdeconnect"]; -pub async fn run<C>(mut block: Block) -> Result<(), Error> +pub async fn run<C>() -> Result<(), Error> where C: Component, { let conn = Connection::session().await?; - block.name = Some(format!("mpris-{}", C::NAME)); - tokio::spawn(listeners::<C>(conn.clone(), block)); + tokio::spawn(listeners::<C>(conn.clone())); for click in std::io::stdin() .lines() .map_while(Result::ok) .flat_map(|s| serde_json::from_str::<Click>(&s)) { - <<C as Component>::Handler as Button>::handle(conn.clone(), click).await?; + if let Err(err) = <<C as Component>::Handler as Button>::handle(conn.clone(), click).await { + eprintln!("Error running button handler: {err}"); + } } Ok(()) } -async fn listeners<C>(conn: Connection, block: Block) -> Result<(), Error> +async fn listeners<C>(conn: Connection) -> Result<(), Error> where C: Component, { @@ -76,19 +77,20 @@ where Result::<_, Error>::Ok(()) }); - let block = Arc::new(Mutex::new(block)); + let block = Arc::new(Mutex::new(Block { + name: Some(format!("mpris-{}", C::NAME)), + ..Default::default() + })); loop { let updated = tokio::select! { Some(name) = rx_player.recv() => { join_set.shutdown().await; - if name.is_empty() { - let mut block = block.lock().await; - block.full_text = String::new(); - block.instance = None; - true - } else { - let mut block = block.lock().await; + + let mut block = block.lock().await; + block.full_text = String::new(); + block.instance = None; + if !name.is_empty() { block.instance.clone_from(&Some(name.clone())); let proxy = PlayerProxy::builder(&conn) .destination(name)? @@ -97,6 +99,8 @@ where join_set.spawn(<<C as Component>::Colorer as Update>::listen(tx_status.clone(), proxy.clone())); join_set.spawn(<<C as Component>::Updater as Update>::listen(tx_value.clone(), proxy)); false + } else { + true } } Some(color) = rx_status.recv() => <<C as Component>::Colorer as Update>::update(color, block.clone()).await?, -- cgit v1.2.3-70-g09d2