i3status_rs/blocks/
rofication.rs

1//! The number of pending notifications in rofication-daemon
2//!
3//! A different color is used if there are critical notifications.
4//!
5//! # Configuration
6//!
7//! Key | Values | Default
8//! ----|--------|--------
9//! `interval` | Refresh rate in seconds. | `1`
10//! `format` | A string to customise the output of this block. See below for placeholders. | `" $icon $num.eng(w:1) "`
11//! `socket_path` | Socket path for the rofication daemon. Supports path expansions e.g. `~`. | `"/tmp/rofi_notification_daemon"`
12//!
13//!  Placeholder | Value | Type | Unit
14//! -------------|-------|------|-----
15//! `icon`       | A static icon  | Icon | -
16//! `num`        | Number of pending notifications | Number | -
17//!
18//! # Example
19//!
20//! ```toml
21//! [[block]]
22//! block = "rofication"
23//! interval = 1
24//! socket_path = "/tmp/rofi_notification_daemon"
25//! [[block.click]]
26//! button = "left"
27//! cmd = "rofication-gui"
28//! ```
29//!
30//! # Icons Used
31//! - `bell`
32
33use 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    // Request count
85    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    // Response must be two integers: regular and critical, separated either by a comma or a \n
97    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}