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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/// # Manual
///
/// tmux ^2.4:
/// ```text
/// detach-client [-aP] [-E shell-command] [-s target-session] [-t target-client]
/// (alias: detach)
/// ```
///
/// tmux ^2.2:
/// ```text
/// detach-client [-aP] [-s target-session] [-t target-client]
/// (alias: detach)
/// ```
///
/// tmux ^1.5:
/// ```text
/// detach-client [-P] [-s target-session] [-t target-client]
/// (alias: detach)
/// ```
///
/// tmux ^0.8:
/// ```text
/// detach-client [-t target-client]
/// (alias: detach)
/// ```
#[macro_export]
macro_rules! detach_client {
// `[-a]` - kill all but the client client given with `-t`
(@cmd ($cmd:expr) -a, $($tail:tt)*) => {{
$crate::detach_client!(@cmd ({
$cmd.all()
}) $($tail)*)
}};
// `[-P]` - send SIGHUP to the parent process of the client, typically causing it to exit
(@cmd ($cmd:expr) -P, $($tail:tt)*) => {{
$crate::detach_client!(@cmd ({
$cmd.parent_sighup()
}) $($tail)*)
}};
// `[-E shell-command]` - run shell-command to replace the client
(@cmd ($cmd:expr) -E $shell_command:expr, $($tail:tt)*) => {{
$crate::detach_client!(@cmd ({
$cmd.shell_command($shell_command)
}) $($tail)*)
}};
// `[-s target-session]` - specify the session, all clients currently attached
(@cmd ($cmd:expr) -s $target_session:expr, $($tail:tt)*) => {{
$crate::detach_client!(@cmd ({
$cmd.target_session($target_session)
}) $($tail)*)
}};
// `[-t target-client]` - specify the client
(@cmd ($cmd:expr) -t $target_client:expr, $($tail:tt)*) => {{
$crate::detach_client!(@cmd ({
$cmd.target_client($target_client)
}) $($tail)*)
}};
//(@cmd ($cmd:expr) -$unknown:tt, $($tail:tt)*) => {{
//::std::compile_error!("unknown flag, option or parameter");
//}};
(@cmd ($cmd:expr)) => {{
$cmd
}};
() => {{
$crate::DetachClient::new()
}};
(($cmd:expr), $($tail:tt)*) => {{
$crate::detach_client!(@cmd ($cmd) $($tail)*,)
}};
($($tail:tt)*) => {{
$crate::detach_client!(@cmd ({ $crate::DetachClient::new() }) $($tail)*,)
}};
}
#[test]
fn detach_client_macro() {
use crate::detach_client;
use crate::TargetSession;
use std::borrow::Cow;
// Structure for detaching the current client
//
// # Manual
//
// tmux ^2.4:
// ```text
// detach-client [-aP] [-E shell-command] [-s target-session] [-t target-client]
// (alias: detach)
// ```
//
// tmux ^2.2:
// ```text
// detach-client [-aP] [-s target-session] [-t target-client]
// (alias: detach)
// ```
//
// tmux ^1.5:
// ```text
// detach-client [-P] [-s target-session] [-t target-client]
// (alias: detach)
// ```
//
// tmux ^0.8:
// ```text
// detach-client [-t target-client]
// (alias: detach)
let target_session = TargetSession::Raw("2").to_string();
let detach_client = detach_client!();
#[cfg(feature = "tmux_2_2")]
let detach_client = detach_client!((detach_client), -a);
#[cfg(feature = "tmux_1_5")]
let detach_client = detach_client!((detach_client), -P);
#[cfg(feature = "tmux_2_4")]
let detach_client = detach_client!((detach_client), -E "1");
#[cfg(feature = "tmux_1_5")]
let detach_client = detach_client!((detach_client), -s & target_session);
#[cfg(feature = "tmux_0_8")]
let detach_client = detach_client!((detach_client), -t "3");
#[cfg(not(feature = "cmd_alias"))]
let cmd = "detach-client";
#[cfg(feature = "cmd_alias")]
let cmd = "detach";
let mut s = Vec::new();
s.push(cmd);
#[cfg(feature = "tmux_2_2")]
s.push("-a");
#[cfg(feature = "tmux_1_5")]
s.push("-P");
#[cfg(feature = "tmux_2_4")]
s.extend_from_slice(&["-E", "1"]);
#[cfg(feature = "tmux_1_5")]
s.extend_from_slice(&["-s", "2"]);
#[cfg(feature = "tmux_0_8")]
s.extend_from_slice(&["-t", "3"]);
let s: Vec<Cow<str>> = s.into_iter().map(|a| a.into()).collect();
let detach_client = detach_client.build().to_vec();
assert_eq!(detach_client, s);
}