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
use crate::commands::constants::*;
use crate::PaneSize;
use crate::TmuxCommand;
use std::borrow::Cow;

pub type JoinP<'a> = JoinPane<'a>;

/// Like split-window, but instead of splitting `dst-pane` and creating a new pane, split it
/// and move `src-pane` into the space
///
/// # Manual
///
/// tmux ^3.1:
/// ```text
/// join-pane [-bdfhv] [-l size] [-s src-pane] [-t dst-pane]
/// (alias: joinp)
/// ```
///
/// tmux ^1.7:
/// ```text
/// join-pane [-bdhv] [-l size | -p percentage] [-s src-pane] [-t dst-pane]
/// (alias: joinp)
/// ```
///
/// tmux ^1.2:
/// ```text
/// join-pane [-dhv] [-l size | -p percentage] [-s src-pane] [-t dst-pane]
/// (alias: joinp)
/// ```
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct JoinPane<'a> {
    /// `[-b]` - cause src-pane to be joined to left of or above dst-pane
    #[cfg(feature = "tmux_2_6")]
    pub left_above: bool,

    /// `[-d]` -
    #[cfg(feature = "tmux_1_2")]
    pub detached: bool,

    /// `[-f]` - creates a new pane spanning the full window height/width
    #[cfg(feature = "tmux_2_6")]
    pub full_size: bool,

    /// `[-h]` - full height
    #[cfg(feature = "tmux_1_2")]
    pub horizontal: bool,

    /// `[-v]` - full width
    #[cfg(feature = "tmux_1_2")]
    pub vertical: bool,

    /// `[-l size]`
    /// `[-l size | -p percentage]` - specify the size of the new pane in lines/columns
    #[cfg(feature = "tmux_1_2")]
    pub size: Option<&'a PaneSize>,

    /// `[-s src-pane]` - src-pane
    #[cfg(feature = "tmux_1_2")]
    pub src_pane: Option<Cow<'a, str>>,

    /// `[-t dst-pane]` - dst-pane
    #[cfg(feature = "tmux_1_2")]
    pub dst_pane: Option<Cow<'a, str>>,
}

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

    /// `[-b]` - cause src-pane to be joined to left of or above dst-pane
    #[cfg(feature = "tmux_2_6")]
    pub fn left_above(mut self) -> Self {
        self.left_above = true;
        self
    }

    /// `[-d]` -
    #[cfg(feature = "tmux_1_2")]
    pub fn detached(mut self) -> Self {
        self.detached = true;
        self
    }

    /// `[-f]` - creates a new pane spanning the full window height/width
    #[cfg(feature = "tmux_2_6")]
    pub fn full_size(mut self) -> Self {
        self.full_size = true;
        self
    }

    /// `[-h]` - full height
    #[cfg(feature = "tmux_1_2")]
    pub fn horizontal(mut self) -> Self {
        self.horizontal = true;
        self
    }

    /// `[-v]` - full width
    #[cfg(feature = "tmux_1_2")]
    pub fn vertical(mut self) -> Self {
        self.vertical = true;
        self
    }

    /// `[-l size]`
    /// `[-l size | -p percentage]` - specify the size of the new pane in lines/columns
    #[cfg(feature = "tmux_1_2")]
    pub fn size(mut self, size: &'a PaneSize) -> Self {
        self.size = Some(size);
        self
    }

    /// `[-s src-pane]` - src-pane
    #[cfg(feature = "tmux_1_2")]
    pub fn src_pane<S: Into<Cow<'a, str>>>(mut self, src_pane: S) -> Self {
        self.src_pane = Some(src_pane.into());
        self
    }

    /// `[-t dst-pane]` - dst-pane
    #[cfg(feature = "tmux_1_2")]
    pub fn dst_pane<S: Into<Cow<'a, str>>>(mut self, dst_pane: S) -> Self {
        self.dst_pane = Some(dst_pane.into());
        self
    }

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

        cmd.name(JOIN_PANE);

        // `[-b]` - cause src-pane to be joined to left of or above dst-pane
        #[cfg(feature = "tmux_2_6")]
        if self.left_above {
            cmd.push_flag(B_LOWERCASE_KEY);
        }

        // `[-d]` -
        #[cfg(feature = "tmux_1_2")]
        if self.detached {
            cmd.push_flag(D_LOWERCASE_KEY);
        }

        // `[-f]` - creates a new pane spanning the full window height/width
        #[cfg(feature = "tmux_2_6")]
        if self.full_size {
            cmd.push_flag(F_LOWERCASE_KEY);
        }

        // `[-h]` - full height
        #[cfg(feature = "tmux_1_2")]
        if self.horizontal {
            cmd.push_flag(H_LOWERCASE_KEY);
        }

        // `[-v]` - full width
        #[cfg(feature = "tmux_1_2")]
        if self.vertical {
            cmd.push_flag(V_LOWERCASE_KEY);
        }

        // `[-l size]`
        // `[-l size | -p percentage]` - specify the size of the new pane in lines/columns
        #[cfg(feature = "tmux_1_2")]
        if let Some(size) = &self.size {
            #[cfg(all(feature = "tmux_1_2", not(feature = "tmux_3_1")))]
            match size {
                PaneSize::Size(size) => cmd.push_option(L_LOWERCASE_KEY, size.to_string()),
                PaneSize::Percentage(size) => {
                    cmd.push_option(L_LOWERCASE_KEY, format!("{}%", size))
                }
            };

            #[cfg(feature = "tmux_3_1")]
            match size {
                PaneSize::Size(size) => cmd.push_option(L_LOWERCASE_KEY, size.to_string()),
                PaneSize::Percentage(size) => cmd.push_option(P_LOWERCASE_KEY, size.to_string()),
            };
        }

        // `[-s src-pane]` - src-pane
        #[cfg(feature = "tmux_1_2")]
        if let Some(src_pane) = self.src_pane {
            cmd.push_option(S_LOWERCASE_KEY, src_pane);
        }

        // `[-t dst-pane]` - dst-pane
        #[cfg(feature = "tmux_1_2")]
        if let Some(dst_pane) = self.dst_pane {
            cmd.push_option(T_LOWERCASE_KEY, dst_pane);
        }

        cmd
    }
}