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