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