kitty_remote_bindings/command/
focus_window.rs

1use std::process::Output;
2
3use kitty_remote_bindings_macros::KittyCommand;
4
5use crate::Result;
6
7use super::{options::Matcher, CommandOutput};
8
9/// Represents the "focus-window" remote command: kitty @ focus-window
10#[derive(Debug, PartialEq, KittyCommand)]
11#[kitty_command = "focus-window"]
12pub struct FocusWindow {
13    #[top_level]
14    /// Sets the `--to` top level option
15    to: Option<String>,
16    /// Sets the `--match` option
17    #[option = "match"]
18    matcher: Option<Matcher>,
19}
20
21impl CommandOutput for FocusWindow {
22    type R = ();
23
24    fn result(output: &Output) -> Result<Self::R> {
25        if output.status.success() {
26            Ok(())
27        } else {
28            Err(crate::Error::ErrorExit("kitty @ focus-window".to_string()))
29        }
30    }
31}
32
33#[cfg(test)]
34mod tests {
35
36    use core::panic;
37    use std::{
38        os::unix::process::ExitStatusExt,
39        process::{Command, ExitStatus, Output},
40    };
41
42    use pretty_assertions::assert_eq;
43
44    use crate::{
45        command::{options::Matcher, CommandOutput},
46        model::WindowId,
47    };
48
49    use super::FocusWindow;
50
51    #[test]
52    fn test_focus_window_command_default() {
53        let cmd = Command::from(&FocusWindow::new());
54
55        assert_eq!(cmd.get_program(), "kitten");
56        assert_eq!(
57            cmd.get_args().collect::<Vec<_>>(),
58            vec!["@", "focus-window"]
59        );
60    }
61
62    #[test]
63    fn test_focus_widow_command_to() {
64        let cmd = Command::from(&FocusWindow::new().to("unix:/path/to/kitty.sock".to_string()));
65
66        assert_eq!(cmd.get_program(), "kitten");
67        assert_eq!(
68            cmd.get_args().collect::<Vec<_>>(),
69            vec!["@", "--to", "unix:/path/to/kitty.sock", "focus-window"]
70        );
71    }
72
73    #[test]
74    fn test_focus_window_command_match_id() {
75        let cmd = Command::from(&FocusWindow::new().matcher(Matcher::Id(WindowId(13))));
76
77        assert_eq!(cmd.get_program(), "kitten");
78        assert_eq!(
79            cmd.get_args().collect::<Vec<_>>(),
80            vec!["@", "focus-window", "--match", "id:13"]
81        );
82    }
83
84    #[test]
85    fn test_focus_window_output_success() {
86        let output = Output {
87            status: ExitStatus::from_raw(0),
88            stdout: "some out put".as_bytes().to_vec(),
89            stderr: "debug info".as_bytes().to_vec(),
90        };
91
92        FocusWindow::result(&output).expect("Succesful output was expected");
93    }
94
95    #[test]
96    fn test_focus_window_output_error() {
97        let output = Output {
98            status: ExitStatus::from_raw(1),
99            stdout: "some out put".as_bytes().to_vec(),
100            stderr: "debug info".as_bytes().to_vec(),
101        };
102
103        let result = FocusWindow::result(&output);
104
105        match result {
106            Err(crate::Error::ErrorExit(process)) => assert_eq!(process, "kitty @ focus-window"),
107            r => panic!("Unexpected result: {r:?}"),
108        };
109    }
110}