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
use std::process::Output;

use kitty_remote_bindings_macros::KittyCommand;

use crate::Result;

use super::{options::Matcher, CommandOutput};

/// Represents the "focus-window" remote command: kitty @ focus-window
#[derive(Debug, PartialEq, KittyCommand)]
#[kitty_command = "focus-window"]
pub struct FocusWindow {
    #[top_level]
    /// Sets the `--to` top level option
    to: Option<String>,
    /// Sets the `--match` option
    #[option = "match"]
    matcher: Option<Matcher>,
}

impl CommandOutput for FocusWindow {
    type R = ();

    fn result(output: &Output) -> Result<Self::R> {
        if output.status.success() {
            Ok(())
        } else {
            Err(crate::Error::ErrorExit("kitty @ focus-window".to_string()))
        }
    }
}

#[cfg(test)]
mod tests {

    use core::panic;
    use std::{
        os::unix::process::ExitStatusExt,
        process::{Command, ExitStatus, Output},
    };

    use pretty_assertions::assert_eq;

    use crate::{
        command::{options::Matcher, CommandOutput},
        model::WindowId,
    };

    use super::FocusWindow;

    #[test]
    fn test_focus_window_command_default() {
        let cmd = Command::from(&FocusWindow::new());

        assert_eq!(cmd.get_program(), "kitty");
        assert_eq!(
            cmd.get_args().collect::<Vec<_>>(),
            vec!["@", "focus-window"]
        );
    }

    #[test]
    fn test_focus_widow_command_to() {
        let cmd = Command::from(&FocusWindow::new().to("unix:/path/to/kitty.sock".to_string()));

        assert_eq!(cmd.get_program(), "kitty");
        assert_eq!(
            cmd.get_args().collect::<Vec<_>>(),
            vec!["@", "--to", "unix:/path/to/kitty.sock", "focus-window"]
        );
    }

    #[test]
    fn test_focus_window_command_match_id() {
        let cmd = Command::from(&FocusWindow::new().matcher(Matcher::Id(WindowId(13))));

        assert_eq!(cmd.get_program(), "kitty");
        assert_eq!(
            cmd.get_args().collect::<Vec<_>>(),
            vec!["@", "focus-window", "--match", "id:13"]
        );
    }

    #[test]
    fn test_focus_window_output_success() {
        let output = Output {
            status: ExitStatus::from_raw(0),
            stdout: "some out put".as_bytes().to_vec(),
            stderr: "debug info".as_bytes().to_vec(),
        };

        FocusWindow::result(&output).expect("Succesful output was expected");
    }

    #[test]
    fn test_focus_window_output_error() {
        let output = Output {
            status: ExitStatus::from_raw(1),
            stdout: "some out put".as_bytes().to_vec(),
            stderr: "debug info".as_bytes().to_vec(),
        };

        let result = FocusWindow::result(&output);

        match result {
            Err(crate::Error::ErrorExit(process)) => assert_eq!(process, "kitty @ focus-window"),
            r => panic!("Unexpected result: {r:?}"),
        };
    }
}