tmux_interface/commands/clients_and_sessions/
detach_client_macro.rs

1/// # Manual
2///
3/// tmux ^2.4:
4/// ```text
5/// detach-client [-aP] [-E shell-command] [-s target-session] [-t target-client]
6/// (alias: detach)
7/// ```
8///
9/// tmux ^2.2:
10/// ```text
11/// detach-client [-aP] [-s target-session] [-t target-client]
12/// (alias: detach)
13/// ```
14///
15/// tmux ^1.5:
16/// ```text
17/// detach-client [-P] [-s target-session] [-t target-client]
18/// (alias: detach)
19/// ```
20///
21/// tmux ^0.8:
22/// ```text
23/// detach-client [-t target-client]
24/// (alias: detach)
25/// ```
26#[macro_export]
27macro_rules! detach_client {
28    // `[-a]` - kill all but the client client given with `-t`
29    (@cmd ($cmd:expr) -a, $($tail:tt)*) => {{
30        $crate::detach_client!(@cmd ({
31            $cmd.all()
32        }) $($tail)*)
33    }};
34    // `[-P]` - send SIGHUP to the parent process of the client, typically causing it to exit
35    (@cmd ($cmd:expr) -P, $($tail:tt)*) => {{
36        $crate::detach_client!(@cmd ({
37            $cmd.parent_sighup()
38        }) $($tail)*)
39    }};
40    // `[-E shell-command]` - run shell-command to replace the client
41    (@cmd ($cmd:expr) -E $shell_command:expr, $($tail:tt)*) => {{
42        $crate::detach_client!(@cmd ({
43            $cmd.shell_command($shell_command)
44        }) $($tail)*)
45    }};
46    // `[-s target-session]` - specify the session, all clients currently attached
47    (@cmd ($cmd:expr) -s $target_session:expr, $($tail:tt)*) => {{
48        $crate::detach_client!(@cmd ({
49            $cmd.target_session($target_session)
50        }) $($tail)*)
51    }};
52    // `[-t target-client]` - specify the client
53    (@cmd ($cmd:expr) -t $target_client:expr, $($tail:tt)*) => {{
54        $crate::detach_client!(@cmd ({
55            $cmd.target_client($target_client)
56        }) $($tail)*)
57    }};
58    //(@cmd ($cmd:expr) -$unknown:tt, $($tail:tt)*) => {{
59        //::std::compile_error!("unknown flag, option or parameter");
60    //}};
61    (@cmd ($cmd:expr)) => {{
62        $cmd
63    }};
64    () => {{
65        $crate::DetachClient::new()
66    }};
67    (($cmd:expr), $($tail:tt)*) => {{
68        $crate::detach_client!(@cmd ($cmd) $($tail)*,)
69    }};
70    ($($tail:tt)*) => {{
71        $crate::detach_client!(@cmd ({ $crate::DetachClient::new() }) $($tail)*,)
72    }};
73}
74
75#[test]
76fn detach_client_macro() {
77    use crate::detach_client;
78    use crate::TargetSession;
79    use std::borrow::Cow;
80
81    // Structure for detaching the current client
82    //
83    // # Manual
84    //
85    // tmux ^2.4:
86    // ```text
87    // detach-client [-aP] [-E shell-command] [-s target-session] [-t target-client]
88    // (alias: detach)
89    // ```
90    //
91    // tmux ^2.2:
92    // ```text
93    // detach-client [-aP] [-s target-session] [-t target-client]
94    // (alias: detach)
95    // ```
96    //
97    // tmux ^1.5:
98    // ```text
99    // detach-client [-P] [-s target-session] [-t target-client]
100    // (alias: detach)
101    // ```
102    //
103    // tmux ^0.8:
104    // ```text
105    // detach-client [-t target-client]
106    // (alias: detach)
107
108    let target_session = TargetSession::Raw("2").to_string();
109
110    let detach_client = detach_client!();
111    #[cfg(feature = "tmux_2_2")]
112    let detach_client = detach_client!((detach_client), -a);
113    #[cfg(feature = "tmux_1_5")]
114    let detach_client = detach_client!((detach_client), -P);
115    #[cfg(feature = "tmux_2_4")]
116    let detach_client = detach_client!((detach_client), -E "1");
117    #[cfg(feature = "tmux_1_5")]
118    let detach_client = detach_client!((detach_client), -s & target_session);
119    #[cfg(feature = "tmux_0_8")]
120    let detach_client = detach_client!((detach_client), -t "3");
121
122    #[cfg(not(feature = "cmd_alias"))]
123    let cmd = "detach-client";
124    #[cfg(feature = "cmd_alias")]
125    let cmd = "detach";
126
127    let mut s = Vec::new();
128    s.push(cmd);
129    #[cfg(feature = "tmux_2_2")]
130    s.push("-a");
131    #[cfg(feature = "tmux_1_5")]
132    s.push("-P");
133    #[cfg(feature = "tmux_2_4")]
134    s.extend_from_slice(&["-E", "1"]);
135    #[cfg(feature = "tmux_1_5")]
136    s.extend_from_slice(&["-s", "2"]);
137    #[cfg(feature = "tmux_0_8")]
138    s.extend_from_slice(&["-t", "3"]);
139    let s: Vec<Cow<str>> = s.into_iter().map(|a| a.into()).collect();
140
141    let detach_client = detach_client.build().to_vec();
142
143    assert_eq!(detach_client, s);
144}