summaryrefslogtreecommitdiff
path: root/src/status_cmd.rs
blob: 9824be4a3f3c8988da5b8bea818550297c6bad55 (plain) (blame)
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
use std::cell::RefCell;
use std::io::{BufRead, BufReader, Result, Write};
use std::os::unix::prelude::AsRawFd;
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
use std::rc::Rc;

use smithay_client_toolkit::reexports::calloop::{self, LoopHandle};

use crate::i3bar_protocol::{Block, Event, Protocol};
use crate::BarState;

pub struct StatusCmd {
    child: Child,
    output: BufReader<ChildStdout>,
    input: ChildStdin,
    protocol: Protocol,
}

impl StatusCmd {
    pub fn new(cmd: &str) -> Result<Self> {
        let mut child = Command::new("sh")
            .args(["-c", &format!("exec {cmd}")])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()?;
        let output = child.stdout.take().unwrap();
        let input = child.stdin.take().unwrap();
        Ok(Self {
            child,
            output: BufReader::new(output),
            input,
            protocol: Protocol::Unknown,
        })
    }

    pub fn notify_available(&mut self) -> Result<Option<Vec<Block>>> {
        let buf = self.output.fill_buf()?;
        self.protocol.process_new_bytes(buf)?;
        let consumed = buf.len();
        self.output.consume(consumed);
        Ok(self.protocol.get_blocks())
    }

    pub fn send_click_event(&mut self, event: &Event) -> Result<()> {
        if self.protocol.supports_clicks() {
            writeln!(self.input, "{}", serde_json::to_string(event).unwrap(),)?;
        }
        Ok(())
    }

    pub fn quick_insert(&self, handle: LoopHandle<()>, bar_state: Rc<RefCell<BarState>>) {
        handle
            .insert_source(
                calloop::generic::Generic::new(
                    self.output.get_ref().as_raw_fd(),
                    calloop::Interest {
                        readable: true,
                        writable: false,
                    },
                    calloop::Mode::Level,
                ),
                move |ready, _, _| {
                    if ready.readable {
                        bar_state.borrow_mut().notify_available()?;
                        Ok(calloop::PostAction::Continue)
                    } else {
                        let mut bar_state = bar_state.borrow_mut();
                        bar_state.set_error("error reading from status command");
                        if let Some(mut child) = bar_state.status_cmd.take() {
                            let _ = child.child.kill();
                        }
                        Ok(calloop::PostAction::Remove)
                    }
                },
            )
            .expect("failed to inser calloop source");
    }
}