i3status_rs/blocks/
focused_window.rs

1//! Currently focused window
2//!
3//! This block displays the title and/or the active marks (when used with `sway`/`i3`) of the currently
4//! focused window. Supported WMs are: `sway`, `i3` and most wlroots-based compositors. See `driver`
5//! option for more info.
6//!
7//! # Configuration
8//!
9//! Key | Values | Default
10//! ----|--------|--------
11//! `format` | A string to customise the output of this block. See below for available placeholders. | <code>\" $title.str(max_w:21) \|\"</code>
12//! `driver` | Which driver to use. Available values: `sway_ipc` - for `i3` and `sway`, `wlr_toplevel_management` - for Wayland compositors that implement [wlr-foreign-toplevel-management-unstable-v1](https://gitlab.freedesktop.org/wlroots/wlr-protocols/-/blob/master/unstable/wlr-foreign-toplevel-management-unstable-v1.xml), `auto` - try to automatically guess which driver to use. | `"auto"`
13//!
14//! Placeholder     | Value                                                                 | Type | Unit
15//! ----------------|-----------------------------------------------------------------------|------|-----
16//! `title`         | Window's title (may be absent)                                        | Text | -
17//! `marks`         | Window's marks (present only with sway/i3)                            | Text | -
18//! `visible_marks` | Window's marks that do not start with `_` (present only with sway/i3) | Text | -
19//!
20//! # Example
21//!
22//! ```toml
23//! [[block]]
24//! block = "focused_window"
25//! [block.format]
26//! full = " $title.str(max_w:15) |"
27//! short = " $title.str(max_w:10) |"
28//! ```
29//!
30//! This example instead of hiding block when the window's title is empty displays "Missing"
31//!
32//! ```toml
33//! [[block]]
34//! block = "focused_window"
35//! format = " $title.str(0,21) | Missing "
36//! ```
37
38mod sway_ipc;
39mod wlr_toplevel_management;
40
41use sway_ipc::SwayIpc;
42use wlr_toplevel_management::WlrToplevelManagement;
43
44use super::prelude::*;
45
46#[derive(Deserialize, Debug, SmartDefault)]
47#[serde(deny_unknown_fields, default)]
48pub struct Config {
49    pub format: FormatConfig,
50    pub driver: Driver,
51}
52
53#[derive(Deserialize, Debug, SmartDefault)]
54#[serde(rename_all = "snake_case")]
55pub enum Driver {
56    #[default]
57    Auto,
58    SwayIpc,
59    WlrToplevelManagement,
60}
61
62pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
63    let format = config.format.with_default(" $title.str(max_w:21) |")?;
64
65    let mut backend: Box<dyn Backend> = match config.driver {
66        Driver::Auto => match SwayIpc::new().await {
67            Ok(swayipc) => Box::new(swayipc),
68            Err(_) => Box::new(WlrToplevelManagement::new().await?),
69        },
70        Driver::SwayIpc => Box::new(SwayIpc::new().await?),
71        Driver::WlrToplevelManagement => Box::new(WlrToplevelManagement::new().await?),
72    };
73
74    loop {
75        let Info { title, marks } = backend.get_info().await?;
76
77        let mut widget = Widget::new().with_format(format.clone());
78
79        if !title.is_empty() {
80            let join_marks = |mut s: String, m: &String| {
81                let _ = write!(s, "[{m}]"); // writing to String never fails
82                s
83            };
84
85            let marks_str = marks.iter().fold(String::new(), join_marks);
86            let visible_marks_str = marks
87                .iter()
88                .filter(|m| !m.starts_with('_'))
89                .fold(String::new(), join_marks);
90
91            widget.set_values(map! {
92                "title" => Value::text(title),
93                "marks" => Value::text(marks_str),
94                "visible_marks" => Value::text(visible_marks_str),
95            });
96        }
97
98        api.set_widget(widget)?;
99    }
100}
101
102#[async_trait]
103trait Backend {
104    async fn get_info(&mut self) -> Result<Info>;
105}
106
107#[derive(Clone, Default)]
108struct Info {
109    title: String,
110    marks: Vec<String>,
111}