i3status_rs/blocks/
rofication.rs1use super::prelude::*;
34use tokio::net::UnixStream;
35
36#[derive(Deserialize, Debug, SmartDefault)]
37#[serde(deny_unknown_fields, default)]
38pub struct Config {
39 #[default(1.into())]
40 pub interval: Seconds,
41 #[default("/tmp/rofi_notification_daemon".into())]
42 pub socket_path: ShellString,
43 pub format: FormatConfig,
44}
45
46pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
47 let format = config.format.with_default(" $icon $num.eng(w:1) ")?;
48
49 let path = config.socket_path.expand()?;
50 let mut timer = config.interval.timer();
51
52 loop {
53 let (num, crit) = rofication_status(&path).await?;
54
55 let mut widget = Widget::new().with_format(format.clone());
56
57 widget.set_values(map!(
58 "icon" => Value::icon("bell"),
59 "num" => Value::number(num)
60 ));
61
62 widget.state = if crit > 0 {
63 State::Warning
64 } else if num > 0 {
65 State::Info
66 } else {
67 State::Idle
68 };
69
70 api.set_widget(widget)?;
71
72 tokio::select! {
73 _ = timer.tick() => (),
74 _ = api.wait_for_update_request() => (),
75 }
76 }
77}
78
79async fn rofication_status(socket_path: &str) -> Result<(usize, usize)> {
80 let mut stream = UnixStream::connect(socket_path)
81 .await
82 .error("Failed to connect to socket")?;
83
84 stream
86 .write_all(b"num:\n")
87 .await
88 .error("Failed to write to socket")?;
89
90 let mut response = String::new();
91 stream
92 .read_to_string(&mut response)
93 .await
94 .error("Failed to read from socket")?;
95
96 let (num, crit) = response
98 .split_once([',', '\n'])
99 .error("Incorrect response")?;
100 Ok((
101 num.parse().error("Incorrect response")?,
102 crit.parse().error("Incorrect response")?,
103 ))
104}