feat: main.rs hinzufügen
This commit is contained in:
+79
@@ -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::<f64>().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::<Update>();
|
||||||
|
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user