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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::commands::constants::*;
use crate::TmuxCommand;
use std::borrow::Cow;
use std::marker::PhantomData;

pub type SwitchC<'a> = SwitchClient<'a>;

/// Structure to switch the current session for client `target-client` to `target-session`
///
/// # Manual
///
/// tmux ^3.1:
/// ```text
/// switch-client [-ElnprZ] [-c target-client] [-t target-session] [-T key-table]
/// (alias: switchc)
/// ```
///
/// tmux ^2.1:
/// ```text
/// switch-client [-Elnpr] [-c target-client] [-t target-session] [-T key-table]
/// (alias: switchc)
/// ```
///
/// tmux ^1.6:
/// ```text
/// switch-client [-lnpr] [-c target-client] [-t target-session]
/// (alias: switchc)
/// ```
///
/// tmux ^1.4:
/// ```text
/// switch-client [-lnp] [-c target-client] [-t target-session]
/// (alias: switchc)
/// ```
///
/// tmux ^1.0:
/// ```text
/// switch-client [-c target-client] [-t target-session]
/// (alias: switchc)
/// ```
///
/// tmux ^0.8:
/// ```text
/// switch-client [-c target-client -t target-session]
/// (alias: switchc)
/// ```
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct SwitchClient<'a> {
    /// `[-E]` - update-environment option will not be applied
    #[cfg(feature = "tmux_2_1")]
    pub not_update_env: bool,

    /// `[-l]` - move to the last session
    #[cfg(feature = "tmux_1_4")]
    pub last_session: bool,

    /// `[-n]` - move to the next session
    #[cfg(feature = "tmux_1_4")]
    pub next_session: bool,

    /// `[-p]` - move to the previous session
    #[cfg(feature = "tmux_1_4")]
    pub previous_session: bool,

    /// `[-r]` - toggle whether a client is read-only
    #[cfg(feature = "tmux_1_6")]
    pub read_only: bool,

    /// `[-Z]` - keep the window zoomed if it was zoomed
    #[cfg(feature = "tmux_3_1")]
    pub keep_zoomed: bool,

    /// `[-c target-client]` - specify the target-client
    #[cfg(feature = "tmux_1_0")]
    pub target_client: Option<Cow<'a, str>>,

    /// `[-t target-session]` - specify the target session
    #[cfg(feature = "tmux_1_0")]
    pub target_session: Option<Cow<'a, str>>,

    /// `[-T key-table]` - set the client's key table
    #[cfg(feature = "tmux_2_1")]
    pub key_table: Option<Cow<'a, str>>,

    _phantom_data: PhantomData<&'a ()>,
}

impl<'a> SwitchClient<'a> {
    pub fn new() -> Self {
        Default::default()
    }

    /// `[-E]` - update-environment option will not be applied
    #[cfg(feature = "tmux_2_1")]
    pub fn not_update_env(mut self) -> Self {
        self.not_update_env = true;
        self
    }

    /// `[-l]` - move to the last session
    #[cfg(feature = "tmux_1_4")]
    pub fn last_session(mut self) -> Self {
        self.last_session = true;
        self
    }

    /// `[-n]` - move to the next session
    #[cfg(feature = "tmux_1_4")]
    pub fn next_session(mut self) -> Self {
        self.next_session = true;
        self
    }

    /// `[-p]` - move to the previous session
    #[cfg(feature = "tmux_1_4")]
    pub fn previous_session(mut self) -> Self {
        self.previous_session = true;
        self
    }

    /// `[-r]` - toggle whether a client is read-only
    #[cfg(feature = "tmux_1_6")]
    pub fn read_only(mut self) -> Self {
        self.read_only = true;
        self
    }

    /// `[-Z]` - keep the window zoomed if it was zoomed
    #[cfg(feature = "tmux_3_1")]
    pub fn keep_zoomed(mut self) -> Self {
        self.keep_zoomed = true;
        self
    }

    /// `[-c target-client]` - specify the target-client
    #[cfg(feature = "tmux_1_0")]
    pub fn target_client<S: Into<Cow<'a, str>>>(mut self, target_client: S) -> Self {
        self.target_client = Some(target_client.into());
        self
    }

    /// `[-t target-session]` - specify the target session
    #[cfg(feature = "tmux_1_0")]
    pub fn target_session<S: Into<Cow<'a, str>>>(mut self, target_session: S) -> Self {
        self.target_session = Some(target_session.into());
        self
    }

    /// `[-T key-table]` - set the client's key table
    #[cfg(feature = "tmux_2_1")]
    pub fn key_table<S: Into<Cow<'a, str>>>(mut self, key_table: S) -> Self {
        self.key_table = Some(key_table.into());
        self
    }

    pub fn build(self) -> TmuxCommand<'a> {
        let mut cmd = TmuxCommand::new();

        cmd.name(SWITCH_CLIENT);

        // `[-E]` - update-environment option will not be applied
        #[cfg(feature = "tmux_2_1")]
        if self.not_update_env {
            cmd.push_flag(E_UPPERCASE_KEY);
        }

        // `[-l]` - move to the last session
        #[cfg(feature = "tmux_1_4")]
        if self.last_session {
            cmd.push_flag(L_LOWERCASE_KEY);
        }

        // `[-n]` - move to the next session
        #[cfg(feature = "tmux_1_4")]
        if self.next_session {
            cmd.push_flag(N_LOWERCASE_KEY);
        }

        // `[-p]` - move to the previous session
        #[cfg(feature = "tmux_1_4")]
        if self.previous_session {
            cmd.push_flag(P_LOWERCASE_KEY);
        }

        // `[-r]` - toggle whether a client is read-only
        #[cfg(feature = "tmux_1_6")]
        if self.read_only {
            cmd.push_flag(R_LOWERCASE_KEY);
        }

        // `[-Z]` - keep the window zoomed if it was zoomed
        #[cfg(feature = "tmux_3_1")]
        if self.keep_zoomed {
            cmd.push_flag(Z_UPPERCASE_KEY);
        }

        // `[-c target-client]` - specify the target-client
        #[cfg(feature = "tmux_1_0")]
        if let Some(target_client) = self.target_client {
            cmd.push_option(C_LOWERCASE_KEY, target_client);
        }

        // `[-t target-session]` - specify the target session
        #[cfg(feature = "tmux_1_0")]
        if let Some(target_session) = self.target_session {
            cmd.push_option(T_LOWERCASE_KEY, target_session);
        }

        // `[-T key-table]` - set the client's key table
        #[cfg(feature = "tmux_2_1")]
        if let Some(key_table) = self.key_table {
            cmd.push_option(T_UPPERCASE_KEY, key_table);
        }

        cmd
    }
}