summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-07-20 02:05:37 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-07-20 02:05:37 -0500
commit9680a9375f3faa3ae3f6f4b78957de0ea2383dae (patch)
treed1386e223bc00bea9cc8591b11f43715236b00fb
parentd34d28aee953469df98eea2e6592ac43db2faeba (diff)
fix: clear on change and improve spacing
-rw-r--r--src/bin/mpris-icon.rs31
-rw-r--r--src/bin/mpris-next.rs6
-rw-r--r--src/bin/mpris-play.rs4
-rw-r--r--src/bin/mpris-prev.rs6
-rw-r--r--src/bin/mpris-title.rs8
-rw-r--r--src/bin/mpris-volume.rs10
-rw-r--r--src/lib.rs30
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::<Icon>(Block {
- full_text: " 󰝚 ".into(),
- ..Default::default()
- })
- .await
- .map_err(Into::into)
+ i3blocks::run::<Icon>().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<Self::Value>,
+ _: i3blocks::dbus::player::PlayerProxy<'_>,
+ ) -> Result<(), Error> {
+ tx.send(" 󰝚 ".into()).await.map_err(Into::into)
+ }
+
+ async fn update(
+ value: Self::Value,
+ block: std::sync::Arc<tokio::sync::Mutex<Block>>,
+ ) -> Result<bool, Error> {
+ 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::<Next>(Default::default())
- .await
- .map_err(Into::into)
+ i3blocks::run::<Next>().await.map_err(Into::into)
}
pub struct Next;
@@ -42,7 +40,7 @@ impl Update for Next {
async fn update(value: Self::Value, block: Arc<Mutex<Block>>) -> Result<bool, Error> {
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::<Play>(Default::default())
- .await
- .map_err(Into::into)
+ i3blocks::run::<Play>().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::<Previous>(Default::default())
- .await
- .map_err(Into::into)
+ i3blocks::run::<Previous>().await.map_err(Into::into)
}
pub struct Previous;
@@ -42,7 +40,7 @@ impl Update for Previous {
async fn update(value: Self::Value, block: Arc<Mutex<Block>>) -> Result<bool, Error> {
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::<Title>(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?,