jay_config/
status.rs

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
//! Knobs for changing the status text.

use {
    crate::{exec::Command, io::Async, tasks::spawn},
    bstr::ByteSlice,
    error_reporter::Report,
    futures_util::{io::BufReader, AsyncBufReadExt},
    serde::Deserialize,
    std::borrow::BorrowMut,
    uapi::{c, OwnedFd},
};

/// Sets the status text.
///
/// The status text is displayed at the right end of the bar.
///
/// The status text should be specified in [pango][pango] markup language.
///
/// [pango]: https://docs.gtk.org/Pango/pango_markup.html
pub fn set_status(status: &str) {
    get!().set_status(status);
}

/// The format of a status command output.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum MessageFormat {
    /// The output is plain text.
    ///
    /// The command should output one line every time it wants to change the status.
    /// The content of the line will be interpreted as plain text.
    Plain,
    /// The output uses [pango][pango] markup.
    ///
    /// The command should output one line every time it wants to change the status.
    /// The content of the line will be interpreted as pango markup.
    ///
    /// [pango]: https://docs.gtk.org/Pango/pango_markup.html
    Pango,
    /// The output uses the [i3bar][i3bar] protocol.
    ///
    /// The separator between individual components can be set using [`set_i3bar_separator`].
    ///
    /// [i3bar]: https://github.com/i3/i3/blob/next/docs/i3bar-protocol
    I3Bar,
}

/// Sets a command whose output will be used as the status text.
///
/// The [`stdout`](Command::stdout) and [`stderr`](Command::stderr)` of the command will
/// be overwritten by this function. The stdout will be used for the status text and the
/// stderr will be appended to the compositor log.
///
/// The format of stdout is determined by the `format` parameter.
pub fn set_status_command(format: MessageFormat, mut command: impl BorrowMut<Command>) {
    macro_rules! pipe {
        () => {{
            let (read, write) = match uapi::pipe2(c::O_CLOEXEC) {
                Ok(p) => p,
                Err(e) => {
                    log::error!("Could not create a pipe: {}", Report::new(e));
                    return;
                }
            };
            let read = match Async::new(read) {
                Ok(r) => BufReader::new(r),
                Err(e) => {
                    log::error!("Could not create an Async object: {}", Report::new(e));
                    return;
                }
            };
            (read, write)
        }};
    }
    let (mut read, write) = pipe!();
    let (mut stderr_read, stderr_write) = pipe!();
    let command = command.borrow_mut();
    command.stdout(write).stderr(stderr_write).spawn();
    let name = command.prog.clone();
    let name2 = command.prog.clone();
    let stderr_handle = spawn(async move {
        let mut line = vec![];
        loop {
            line.clear();
            if let Err(e) = stderr_read.read_until(b'\n', &mut line).await {
                log::warn!("Could not read from {name2} stderr: {}", Report::new(e));
                return;
            }
            if line.len() == 0 {
                return;
            }
            log::warn!(
                "{name2} emitted a message on stderr: {}",
                line.trim_with(|c| c == '\n').as_bstr()
            );
        }
    });
    let handle = spawn(async move {
        if format == MessageFormat::I3Bar {
            handle_i3bar(name, read).await;
            return;
        }
        let mut line = String::new();
        let mut cleaned = String::new();
        loop {
            line.clear();
            if let Err(e) = read.read_line(&mut line).await {
                log::error!("Could not read from `{name}`: {}", Report::new(e));
                return;
            }
            if line.is_empty() {
                log::info!("{name} closed stdout");
                return;
            }
            let line = line.strip_suffix("\n").unwrap_or(&line);
            cleaned.clear();
            if format != MessageFormat::Pango && escape_pango(line, &mut cleaned) {
                set_status(&cleaned);
            } else {
                set_status(line);
            }
        }
    });
    get!().set_status_tasks(vec![handle, stderr_handle]);
}

/// Unsets the previously set status command.
pub fn unset_status_command() {
    get!().set_status_tasks(vec![]);
}

/// Sets the separator for i3bar status commands.
///
/// The separator should be specified in [pango][pango] markup language.
///
/// [pango]: https://docs.gtk.org/Pango/pango_markup.html
pub fn set_i3bar_separator(separator: &str) {
    get!().set_i3bar_separator(separator);
}

async fn handle_i3bar(name: String, mut read: BufReader<Async<OwnedFd>>) {
    use std::fmt::Write;

    #[derive(Deserialize)]
    struct Version {
        version: i32,
    }
    #[derive(Deserialize)]
    struct Component {
        markup: Option<String>,
        full_text: String,
        color: Option<String>,
        background: Option<String>,
    }
    let mut line = String::new();
    macro_rules! read_line {
        () => {{
            line.clear();
            if let Err(e) = read.read_line(&mut line).await {
                log::error!("Could not read from `{name}`: {}", Report::new(e));
                return;
            }
            if line.is_empty() {
                log::info!("{name} closed stdout");
                return;
            }
        }};
    }
    read_line!();
    match serde_json::from_str::<Version>(&line) {
        Ok(v) if v.version == 1 => {}
        Ok(v) => log::warn!("Unexpected i3bar format version: {}", v.version),
        Err(e) => {
            log::warn!(
                "Could not deserialize i3bar version message: {}",
                Report::new(e)
            );
            return;
        }
    }
    read_line!();
    let mut status = String::new();
    loop {
        read_line!();
        let mut line = line.as_str();
        if let Some(l) = line.strip_prefix(",") {
            line = l;
        }
        let components = match serde_json::from_str::<Vec<Component>>(line) {
            Ok(c) => c,
            Err(e) => {
                log::warn!(
                    "Could not deserialize i3bar status message: {}",
                    Report::new(e)
                );
                continue;
            }
        };
        let separator = get!().get_i3bar_separator();
        let separator = match &separator {
            Some(s) => s.as_str(),
            _ => r##" <span color="#333333">|</span> "##,
        };
        status.clear();
        let mut first = true;
        for component in &components {
            if component.full_text.is_empty() {
                continue;
            }
            if !first {
                status.push_str(separator);
            }
            first = false;
            let have_span = component.color.is_some() || component.background.is_some();
            if have_span {
                status.push_str("<span");
                if let Some(color) = &component.color {
                    let _ = write!(status, r#" color="{color}""#);
                }
                if let Some(color) = &component.background {
                    let _ = write!(status, r#" bgcolor="{color}""#);
                }
                status.push_str(">");
            }
            if component.markup.as_deref() == Some("pango")
                || !escape_pango(&component.full_text, &mut status)
            {
                status.push_str(&component.full_text);
            }
            if have_span {
                status.push_str("</span>");
            }
        }
        set_status(&status);
    }
}

fn escape_pango(src: &str, dst: &mut String) -> bool {
    if src
        .bytes()
        .any(|b| matches!(b, b'&' | b'<' | b'>' | b'\'' | b'"'))
    {
        for c in src.chars() {
            match c {
                '&' => dst.push_str("&amp;"),
                '<' => dst.push_str("&lt;"),
                '>' => dst.push_str("&gt;"),
                '\'' => dst.push_str("&apos;"),
                '"' => dst.push_str("&quot;"),
                _ => dst.push(c),
            }
        }
        true
    } else {
        false
    }
}