From 75d0ebbc0bd5e90167c4353fdd43e4f0e3c85e77 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 27 Aug 2026 20:32:46 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20main.rs=20hinzuf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/main.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0331555 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,79 @@ +use std::io::{self, Write}; +use std::process::Command; +use std::sync::mpsc; +use std::time::Duration; + +use chrono::Local; +use signal_hook::consts::SIGUSR1; +use signal_hook::iterator::Signals; + +enum Update { + Clock(String), + Volume(String), +} + +fn query_volume() -> String { + let output = Command::new("wpctl") + .args(["get-volume", "@DEFAULT_AUDIO_SINK@"]) + .output(); + + let text = match output { + Ok(out) => String::from_utf8_lossy(&out.stdout).trim().to_string(), + Err(_) => return "vol:?".to_string(), + }; + + let muted = text.contains("[MUTED]"); + let percent = text + .split_whitespace() + .nth(1) + .and_then(|s| s.parse::().ok()) + .map(|v| (v * 100.0).round() as i64); + + match (percent, muted) { + (Some(p), true) => format!("vol:{p}% [muted]"), + (Some(p), false) => format!("vol:{p}%"), + (None, _) => "vol:?".to_string(), + } +} + +fn main() { + let (tx, rx) = mpsc::channel::(); + + { + let tx = tx.clone(); + std::thread::spawn(move || loop { + let now = Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); + tx.send(Update::Clock(now)).unwrap(); + std::thread::sleep(Duration::from_secs(1)); + }); + } + + { + let tx = tx.clone(); + std::thread::spawn(move || { + tx.send(Update::Volume(query_volume())).unwrap(); + + let mut signals = + Signals::new([SIGUSR1]).expect("failed to register signal handler"); + for _ in signals.forever() { + tx.send(Update::Volume(query_volume())).unwrap(); + } + }); + } + + drop(tx); + + let mut clock = String::new(); + let mut volume = String::new(); + let stdout = io::stdout(); + let mut handle = stdout.lock(); + + for update in rx { + match update { + Update::Clock(s) => clock = s, + Update::Volume(s) => volume = s, + } + writeln!(handle, "{volume} | {clock}").expect("failed to write to stdout"); + handle.flush().expect("failed to flush stdout"); + } +}