diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs index cd17ec2..146d450 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::{process::Stdio, sync::Arc}; use i3blocks_mpris::{ handler::handlers, - i3bar::{Align, Block, Click, MinWidth}, + i3bar::{Align, Block, Click, Header, MinWidth}, listener::listeners, BlockKind, Watcher, }; @@ -95,39 +95,50 @@ async fn main() -> Result<(), main_error::MainError> { tokio::spawn(listeners(conn.clone(), blocks.clone())); tokio::spawn(handlers(conn, rx)); + let mut header = serde_json::to_string(&Header::default())?; + header.push_str("\n[[]\n"); + stdout.write_all(header.as_bytes()).await?; + stdout.flush().await?; + let mut mpris_blocks = String::new(); let mut other_blocks = String::new(); loop { tokio::select! { _ = blocks.notify.notified() => { - mpris_blocks = blocks + let enabled = blocks .value .read() .await .iter() .filter(|b| b.enabled) .map(serde_json::to_string) - .collect::<Result<Vec<_>, _>>()? - .join(","); + .collect::<Result<Vec<_>, _>>()?; + mpris_blocks = if enabled.is_empty() { + r#"{"name":"mpris","full_text":""}"#.to_owned() + } else { + enabled.join(",") + }; } - Ok(Some(line)) = child_stdout.next_line() => other_blocks = line, + Ok(Some(line)) = child_stdout.next_line() => { + if line.starts_with(",[{") { + other_blocks = line + } + }, Ok(Some(line)) = stdin.next_line() => { handle_stdin(&line, tx.clone(), &mut child_stdin).await?; continue; } - else => break, + else => continue, } - let output = other_blocks.replace(r#"{"name":"mpris","full_text":""},"#, &mpris_blocks); - if !output.is_empty() { + if !other_blocks.is_empty() { + let output = other_blocks.replace(r#"{"name":"mpris","full_text":""}"#, &mpris_blocks); stdout.write_all(output.as_bytes()).await?; stdout.write_u8(b'\n').await?; stdout.flush().await?; } } - - Ok(()) } async fn handle_stdin( |