bspc_rust_lib/bspc/monitor/
command.rs

1use crate::{bspc::selector::Assembleable, socket_communication};
2
3use super::{descriptor::MonitorDescriptor, selector::MonitorSelector};
4
5static DEFAULT_DESCRIPTOR: Option<&MonitorDescriptor> = Some(&MonitorDescriptor::Focused);
6
7pub enum MonitorCommand {
8    /// Focuses the selected monitor.
9    /// # Arugments
10    /// - `monitor_sel`: The `MonitorSelector` that specifies the monitor to focus
11    Focus(MonitorSelector),
12    /// Swaps the selected monitor with the focused monitor.
13    /// # Arguments
14    /// - `monitor_sel`: The `MonitorSelector` that specifies the monitor to swap with the focused monitor
15    Swap(MonitorSelector),
16    /// Adds desktops to the selected monitor.
17    /// # Arguments
18    /// - `monitor_sel`: The monitor to add the desktops to
19    /// - `desktop_names`: The names of the desktops to add
20    AddDesktops(MonitorSelector, Vec<String>),
21    /// Reorders desktops of the selected monitor.
22    /// # Arguments
23    /// - `monitor_sel`: The monitor to reorder the desktops on
24    /// - `desktop_names`: The names of the desktops in the order to set
25    ReorderDesktops(MonitorSelector, Vec<String>),
26    /// Sets the rectangle of the selected monitor.
27    /// # Arguments
28    /// - `monitor_sel`: The monitor to set the rectangle for
29    /// - `width`: The width of the rectangle
30    /// - `height`: The height of the rectangle
31    /// - `x_pos`: The x-position of the rectangle
32    /// - `y_pos`: The y-position of the rectangle
33    Rectangle(MonitorSelector, u32, u32, u32, u32),
34    /// Renames the selected monitor.
35    /// # Arguments
36    /// - `monitor_sel`: The monitor to rename
37    /// - `name`: The name to set for the monitor
38    Rename(MonitorSelector, String),
39    /// Removes the selected monitor.
40    /// # Arguments
41    /// - `monitor_sel`: The monitor to remove
42    Remove(MonitorSelector),
43}
44
45impl MonitorCommand {
46    pub fn get_response(&self) -> Option<Vec<String>> {
47        socket_communication::send_message(self.assemble())
48    }
49
50    fn assemble(&self) -> Vec<String> {
51        let mut result: Vec<String> = Vec::new();
52        result.push(String::from("monitor"));
53        match self {
54            MonitorCommand::Focus(monitor_sel) => {
55                result.push(String::from("--focus"));
56                result.push(monitor_sel.assemble(DEFAULT_DESCRIPTOR));
57            }
58            MonitorCommand::Swap(monitor_sel) => {
59                result.push(String::from("--swap"));
60                result.push(monitor_sel.assemble(DEFAULT_DESCRIPTOR));
61            }
62            MonitorCommand::AddDesktops(monitor_sel, desktops) => {
63                result.push(monitor_sel.assemble(DEFAULT_DESCRIPTOR));
64                result.push(String::from("--add-desktops"));
65                result.push(desktops.join(" "));
66            }
67            MonitorCommand::ReorderDesktops(monitor_sel, desktops) => {
68                result.push(monitor_sel.assemble(DEFAULT_DESCRIPTOR));
69                result.push(String::from("--reorder-desktops"));
70                result.push(desktops.join(" "));
71            }
72            MonitorCommand::Rectangle(monitor_sel, width, height, x, y) => {
73                result.push(monitor_sel.assemble(DEFAULT_DESCRIPTOR));
74                result.push(String::from("--rectangle"));
75                result.push(format!("{}x{}+{}+{}", width, height, x, y));
76            }
77            MonitorCommand::Rename(monitor_sel, name) => {
78                result.push(monitor_sel.assemble(DEFAULT_DESCRIPTOR));
79                result.push(String::from("--rename"));
80                result.push(name.to_string());
81            }
82            MonitorCommand::Remove(monitor_sel) => {
83                result.push(monitor_sel.assemble(DEFAULT_DESCRIPTOR));
84                result.push(String::from("--remove"));
85            }
86        }
87        return result;
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use crate::bspc::{monitor::{command::MonitorCommand, descriptor::MonitorDescriptor, selector::MonitorSelector}, selector::Selector};
94
95    #[test]
96    fn remove() {
97        let cmd = MonitorCommand::Remove(MonitorSelector::new().set_descriptor(MonitorDescriptor::Name("DP1-1".to_string()))).assemble();
98
99        assert_eq!(cmd, vec!["monitor", "DP1-1", "--remove"]);
100    }
101}