1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! The collection of blocks
//!
//! Blocks are defined as a [TOML array of tables](https://github.com/toml-lang/toml/blob/main/toml.md#user-content-array-of-tables): `[[block]]`
//!
//! Key | Description | Default
//! ----|-------------|----------
//! `block` | Name of the i3status-rs block you want to use. See [modules](#modules) below for valid block names. | -
//! `signal` | Signal value that causes an update for this block with `0` corresponding to `-SIGRTMIN+0` and the largest value being `-SIGRTMAX` | None
//! `if_command` | Only display the block if the supplied command returns 0 on startup. | None
//! `merge_with_next` | If true this will group the block with the next one, so rendering such as alternating_tint will apply to the whole group | `false`
//! `icons_format` | Overrides global `icons_format` | None
//! `error_format` | Overrides global `error_format` | None
//! `error_fullscreen_format` | Overrides global `error_fullscreen_format` | None
//! `error_interval` | How long to wait until restarting the block after an error occurred. | `5`
//! `[block.theme_overrides]` | Same as the top-level config option, but for this block only. Refer to `Themes and Icons` below. | None
//! `[block.icons_overrides]` | Same as the top-level config option, but for this block only. Refer to `Themes and Icons` below. | None
//! `[[block.click]]` | Set or override click action for the block. See below for details. | Block default / None
//!
//! Per block click configuration `[[block.click]]`:
//!
//! Key | Description | Default
//! ----|-------------|----------
//! `button` | `left`, `middle`, `right`, `up`, `down`, `forward`, `back` or [`double_left`](MouseButton). | -
//! `widget` | To which part of the block this entry applies (accepts regex) | `"block"`
//! `cmd` | Command to run when the mouse button event is detected. | None
//! `action` | Which block action to trigger | None
//! `sync` | Whether to wait for command to exit or not. | `false`
//! `update` | Whether to update the block on click. | `false`

mod prelude;

use futures::future::FutureExt;
use futures::stream::FuturesUnordered;
use serde::de::{self, Deserialize};
use tokio::sync::{mpsc, Notify};

use std::borrow::Cow;
use std::sync::Arc;
use std::time::Duration;

use crate::click::MouseButton;
use crate::errors::*;
use crate::widget::Widget;
use crate::{BoxedFuture, Request, RequestCmd};

macro_rules! define_blocks {
    {
        $(
            $(#[cfg(feature = $feat: literal)])?
            $(#[deprecated($($dep_k: ident = $dep_v: literal),+)])?
            $block: ident $(,)?
        )*
    } => {
        $(
            $(#[cfg(feature = $feat)])?
            $(#[cfg_attr(docsrs, doc(cfg(feature = $feat)))])?
            $(#[deprecated($($dep_k = $dep_v),+)])?
            pub mod $block;
        )*

        #[derive(Debug)]
        pub enum BlockConfig {
            $(
                $(#[cfg(feature = $feat)])?
                #[allow(non_camel_case_types)]
                #[allow(deprecated)]
                $block($block::Config),
            )*
            Err(&'static str, Error),
        }

        impl BlockConfig {
            pub fn name(&self) -> &'static str {
                match self {
                    $(
                        $(#[cfg(feature = $feat)])?
                        Self::$block { .. } => stringify!($block),
                    )*
                    Self::Err(name, _err) => name,
                }
            }

            pub fn spawn(self, api: CommonApi, futures: &mut FuturesUnordered<BoxedFuture<()>>) {
                match self {
                    $(
                        $(#[cfg(feature = $feat)])?
                        #[allow(deprecated)]
                        Self::$block(config) => futures.push(async move {
                            while let Err(err) = $block::run(&config, &api).await {
                                if api.set_error(err).is_err() {
                                    return;
                                }
                                tokio::select! {
                                    _ = tokio::time::sleep(api.error_interval) => (),
                                    _ = api.wait_for_update_request() => (),
                                }
                            }
                        }.boxed_local()),
                    )*
                    Self::Err(_name, err) => {
                        let _ = api.set_error(Error {
                            message: Some("Configuration error".into()),
                            cause: Some(Arc::new(err)),
                        });
                    },
                }
            }
        }

        impl<'de> Deserialize<'de> for BlockConfig {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: de::Deserializer<'de>,
            {
                use de::Error;

                let mut table = toml::Table::deserialize(deserializer)?;
                let block_name = table.remove("block").ok_or_else(|| D::Error::missing_field("block"))?;
                let block_name = block_name.as_str().ok_or_else(|| D::Error::custom("block must be a string"))?;

                match block_name {
                    $(
                        $(#[cfg(feature = $feat)])?
                        #[allow(deprecated)]
                        stringify!($block) => match $block::Config::deserialize(table) {
                            Ok(config) => Ok(BlockConfig::$block(config)),
                            Err(err) => Ok(BlockConfig::Err(stringify!($block), crate::errors::Error::new(err.to_string()))),
                        }
                        $(
                            #[cfg(not(feature = $feat))]
                            stringify!($block) => Err(D::Error::custom(format!(
                                "block {} is behind a feature gate '{}' which must be enabled at compile time",
                                stringify!($block),
                                $feat,
                            ))),
                        )?
                    )*
                    other => Err(D::Error::custom(format!("unknown block '{other}'")))
                }
            }
        }
    };
}

define_blocks!(
    amd_gpu,
    #[deprecated(
        since = "0.33.0",
        note = "The block has been deprecated in favor of the the packages block"
    )]
    apt,
    backlight,
    battery,
    bluetooth,
    cpu,
    custom,
    custom_dbus,
    disk_space,
    #[deprecated(
        since = "0.33.0",
        note = "The block has been deprecated in favor of the the packages block"
    )]
    dnf,
    docker,
    external_ip,
    focused_window,
    github,
    hueshift,
    kdeconnect,
    load,
    #[cfg(feature = "maildir")]
    maildir,
    menu,
    memory,
    music,
    net,
    notify,
    #[cfg(feature = "notmuch")]
    notmuch,
    nvidia_gpu,
    packages,
    #[deprecated(
        since = "0.33.0",
        note = "The block has been deprecated in favor of the the packages block"
    )]
    pacman,
    pomodoro,
    privacy,
    rofication,
    service_status,
    sound,
    speedtest,
    keyboard_layout,
    taskwarrior,
    temperature,
    time,
    tea_timer,
    toggle,
    uptime,
    vpn,
    watson,
    weather,
    xrandr,
);

/// An error which originates from a block
#[derive(Debug, thiserror::Error)]
#[error("In block {}: {}", .block_name, .error)]
pub struct BlockError {
    pub block_id: usize,
    pub block_name: &'static str,
    pub error: Error,
}

pub type BlockAction = Cow<'static, str>;

#[derive(Clone)]
pub struct CommonApi {
    pub(crate) id: usize,
    pub(crate) update_request: Arc<Notify>,
    pub(crate) request_sender: mpsc::UnboundedSender<Request>,
    pub(crate) error_interval: Duration,
}

impl CommonApi {
    /// Sends the widget to be displayed.
    pub fn set_widget(&self, widget: Widget) -> Result<()> {
        self.request_sender
            .send(Request {
                block_id: self.id,
                cmd: RequestCmd::SetWidget(widget),
            })
            .error("Failed to send Request")
    }

    /// Hides the block. Send new widget to make it visible again.
    pub fn hide(&self) -> Result<()> {
        self.request_sender
            .send(Request {
                block_id: self.id,
                cmd: RequestCmd::UnsetWidget,
            })
            .error("Failed to send Request")
    }

    /// Sends the error to be displayed.
    pub fn set_error(&self, error: Error) -> Result<()> {
        self.request_sender
            .send(Request {
                block_id: self.id,
                cmd: RequestCmd::SetError(error),
            })
            .error("Failed to send Request")
    }

    pub fn set_default_actions(
        &self,
        actions: &'static [(MouseButton, Option<&'static str>, &'static str)],
    ) -> Result<()> {
        self.request_sender
            .send(Request {
                block_id: self.id,
                cmd: RequestCmd::SetDefaultActions(actions),
            })
            .error("Failed to send Request")
    }

    pub fn get_actions(&self) -> Result<mpsc::UnboundedReceiver<BlockAction>> {
        let (tx, rx) = mpsc::unbounded_channel();
        self.request_sender
            .send(Request {
                block_id: self.id,
                cmd: RequestCmd::SubscribeToActions(tx),
            })
            .error("Failed to send Request")?;
        Ok(rx)
    }

    pub async fn wait_for_update_request(&self) {
        self.update_request.notified().await;
    }
}