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
111
112
113
114
115
116
117
118
use async_trait::async_trait;

use crate::{
    executor::{Executor, TokioExecutor},
    model::LsOutput,
    remote_command::{LsOptions, SendTextOptions},
    KittyTerminal, Result,
};

pub struct Kitty {
    executor: Box<dyn Executor + Send + Sync + 'static>,
}

impl Kitty {
    #[must_use]
    pub fn new() -> Self {
        Self {
            executor: Box::new(TokioExecutor),
        }
    }
}

impl Default for Kitty {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl KittyTerminal for Kitty {
    async fn ls(&self, options: &LsOptions) -> Result<LsOutput> {
        let mut cmd: tokio::process::Command = options.into();
        let output = self.executor.output(&mut cmd).await?;
        let ls_output = serde_json::from_slice::<LsOutput>(&output.stdout)?;

        Ok(ls_output)
    }

    async fn send_text(&self, options: &SendTextOptions, args: &[&str]) -> Result<()> {
        let mut cmd: tokio::process::Command = options.into();
        cmd.args(args);

        self.executor.output(&mut cmd).await?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::process::{ExitStatus, Output};

    use super::Kitty;
    use super::KittyTerminal;
    use crate::remote_command::LsOptions;
    use crate::remote_command::Matcher;
    use crate::remote_command::SendTextOptions;
    use crate::{executor::MockExecutor, model::test_fixture};
    use pretty_assertions::assert_eq;

    #[tokio::test]
    async fn test_ls() {
        let mut executor = MockExecutor::new();
        executor
            .expect_output()
            .withf(|cmd| format!("{:?}", cmd.as_std()) == r#""kitty" "@" "ls""#)
            .times(1)
            .returning(|_| {
                Ok(Output {
                    status: ExitStatus::default(),
                    stdout: test_fixture::LS_OUTPUT_JSON.as_bytes().to_vec(),
                    stderr: Vec::new(),
                })
            });

        let kitty = Kitty {
            executor: Box::new(executor),
        };

        let result = kitty
            .ls(&LsOptions::default())
            .await
            .expect("ls() returned an error");

        assert_eq!(result, *test_fixture::LS_OUTPUT);
    }

    #[tokio::test]
    async fn test_send_text() {
        let mut executor = MockExecutor::new();
        executor
            .expect_output()
            .withf(|cmd| {
                format!("{:?}", cmd.as_std())
                    == r#""kitty" "@" "send-text" "--match" "id:1" "some text""#
            })
            .times(1)
            .returning(|_| {
                Ok(Output {
                    status: ExitStatus::default(),
                    stdout: Vec::new(),
                    stderr: Vec::new(),
                })
            });

        let kitty = Kitty {
            executor: Box::new(executor),
        };

        let mut options = SendTextOptions::default();
        options.matcher(Matcher::Id(1));

        kitty
            .send_text(&options, &["some text"])
            .await
            .expect("send_text() returned an error");
    }
}