tmux_interface/commands/clients_and_sessions/
detach_client.rs

1use crate::commands::constants::*;
2use crate::TmuxCommand;
3use std::borrow::Cow;
4
5pub type Detach<'a> = DetachClient<'a>;
6
7/// Structure for detaching the current client
8///
9/// # Manual
10///
11/// tmux ^2.4:
12/// ```text
13/// detach-client [-aP] [-E shell-command] [-s target-session] [-t target-client]
14/// (alias: detach)
15/// ```
16///
17/// tmux ^2.2:
18/// ```text
19/// detach-client [-aP] [-s target-session] [-t target-client]
20/// (alias: detach)
21/// ```
22///
23/// tmux ^1.5:
24/// ```text
25/// detach-client [-P] [-s target-session] [-t target-client]
26/// (alias: detach)
27/// ```
28///
29/// tmux ^0.8:
30/// ```text
31/// detach-client [-t target-client]
32/// (alias: detach)
33/// ```
34#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
35pub struct DetachClient<'a> {
36    /// `[-a]` - kill all but the client client given with `-t`
37    #[cfg(feature = "tmux_2_2")]
38    pub all: bool,
39
40    /// `[-P]` - send SIGHUP to the parent process of the client, typically causing it to exit
41    #[cfg(feature = "tmux_1_5")]
42    pub parent_sighup: bool,
43
44    /// `[-E shell-command]` - run shell-command to replace the client
45    #[cfg(feature = "tmux_2_4")]
46    pub shell_command: Option<Cow<'a, str>>,
47
48    /// `[-s target-session]` - specify the session, all clients currently attached
49    #[cfg(feature = "tmux_1_5")]
50    pub target_session: Option<Cow<'a, str>>,
51
52    /// `[-t target-client]` - specify the client
53    #[cfg(feature = "tmux_0_8")]
54    pub target_client: Option<Cow<'a, str>>,
55}
56
57impl<'a> DetachClient<'a> {
58    pub fn new() -> Self {
59        Default::default()
60    }
61
62    /// `[-a]` - kill all but the client client given with `-t`
63    #[cfg(feature = "tmux_2_2")]
64    pub fn all(mut self) -> Self {
65        self.all = true;
66        self
67    }
68
69    /// `[-P]` - send SIGHUP to the parent process of the client, typically causing it to exit
70    #[cfg(feature = "tmux_1_5")]
71    pub fn parent_sighup(mut self) -> Self {
72        self.parent_sighup = true;
73        self
74    }
75
76    /// `[-E shell-command]` - run shell-command to replace the client
77    #[cfg(feature = "tmux_2_4")]
78    pub fn shell_command<S: Into<Cow<'a, str>>>(mut self, shell_command: S) -> Self {
79        self.shell_command = Some(shell_command.into());
80        self
81    }
82
83    /// `[-s target-session]` - specify the session, all clients currently attached
84    #[cfg(feature = "tmux_1_5")]
85    pub fn target_session<S: Into<Cow<'a, str>>>(mut self, target_session: S) -> Self {
86        self.target_session = Some(target_session.into());
87        self
88    }
89
90    /// `[-t target-client]` - specify the client
91    #[cfg(feature = "tmux_0_8")]
92    pub fn target_client<S: Into<Cow<'a, str>>>(mut self, target_client: S) -> Self {
93        self.target_client = Some(target_client.into());
94        self
95    }
96
97    pub fn build(self) -> TmuxCommand<'a> {
98        let mut cmd = TmuxCommand::new();
99
100        cmd.name(DETACH_CLIENT);
101
102        // `[-a]` - kill all but the client client given with `-t`
103        #[cfg(feature = "tmux_2_2")]
104        if self.all {
105            cmd.push_flag(A_LOWERCASE_KEY);
106        }
107
108        // `[-P]` - send SIGHUP to the parent process of the client, typically causing it to exit
109        #[cfg(feature = "tmux_1_5")]
110        if self.parent_sighup {
111            cmd.push_flag(P_UPPERCASE_KEY);
112        }
113
114        // `[-E shell-command]` - run shell-command to replace the client
115        #[cfg(feature = "tmux_2_4")]
116        if let Some(shell_command) = self.shell_command {
117            cmd.push_option(E_UPPERCASE_KEY, shell_command);
118        }
119
120        // `[-s target-session]` - specify the session, all clients currently attached
121        #[cfg(feature = "tmux_1_5")]
122        if let Some(target_session) = self.target_session {
123            cmd.push_option(S_LOWERCASE_KEY, target_session);
124        }
125
126        // `[-t target-client]` - specify the client
127        #[cfg(feature = "tmux_0_8")]
128        if let Some(target_client) = self.target_client {
129            cmd.push_option(T_LOWERCASE_KEY, target_client);
130        }
131
132        cmd
133    }
134}