tmux_interface/commands/windows_and_panes/
resize_pane.rs

1use crate::commands::constants::*;
2use crate::TmuxCommand;
3use std::borrow::Cow;
4
5pub type ResizeP<'a> = ResizePane<'a>;
6
7/// Resize a pane, up, down, left or right
8///
9/// # Manual
10///
11/// tmux ^3.2:
12/// ```text
13/// resize-pane [-DLMRTUZ] [-t target-pane] [-x width] [-y height] [adjustment]
14/// (alias: resizep)
15/// ```
16///
17/// tmux ^2.1:
18/// ```text
19/// resize-pane [-DLMRUZ] [-t target-pane] [-x width] [-y height] [adjustment]
20/// (alias: resizep)
21/// ```
22///
23/// tmux ^1.8:
24/// ```text
25/// resize-pane [-DLRUZ] [-t target-pane] [-x width] [-y height] [adjustment]
26/// (alias: resizep)
27/// ```
28///
29/// tmux ^1.0:
30/// ```text
31/// resize-pane [-DLRU] [-t target-pane] [adjustment]
32/// (alias: resizep)
33/// ```
34///
35/// tmux ^0.9:
36/// ```text
37/// resize-pane [-DU] [-p pane-index] [-t target-pane] [adjustment]
38/// (alias: resizep)
39/// ```
40#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
41pub struct ResizePane<'a> {
42    /// `[-D]` - resize down by adjustment
43    #[cfg(feature = "tmux_0_9")]
44    pub down: bool,
45
46    /// `[-L]` - resize left by adjustment
47    #[cfg(feature = "tmux_1_8")]
48    pub left: bool,
49
50    /// `[-M]` - begin mouse resizing
51    #[cfg(feature = "tmux_2_1")]
52    pub mouse: bool,
53
54    /// `[-R]` - resize right by adjustment
55    #[cfg(feature = "tmux_1_8")]
56    pub right: bool,
57
58    /// `[-T]` - trims all lines below the current cursor position
59    #[cfg(feature = "tmux_3_2")]
60    pub trim: bool,
61
62    /// `[-U]` - resize up by adjustment
63    #[cfg(feature = "tmux_0_9")]
64    pub up: bool,
65
66    /// `[-Z]` - the active pane is toggled between zoomed and unzoomed
67    #[cfg(feature = "tmux_1_8")]
68    pub zoom: bool,
69
70    /// `[-t target-pane]` - target-pane
71    #[cfg(feature = "tmux_0_9")]
72    pub target_pane: Option<Cow<'a, str>>,
73
74    /// `[-x width]` - absolute size
75    #[cfg(feature = "tmux_1_8")]
76    pub width: Option<usize>,
77
78    /// `[-y height]` - absolute size
79    #[cfg(feature = "tmux_1_8")]
80    pub height: Option<usize>,
81
82    /// `[adjustment]` - adjustment
83    #[cfg(feature = "tmux_0_9")]
84    pub adjustment: Option<Cow<'a, str>>,
85}
86
87impl<'a> ResizePane<'a> {
88    pub fn new() -> Self {
89        Default::default()
90    }
91
92    /// `[-D]` - resize down by adjustment
93    #[cfg(feature = "tmux_0_9")]
94    pub fn down(mut self) -> Self {
95        self.down = true;
96        self
97    }
98
99    /// `[-L]` - resize left by adjustment
100    #[cfg(feature = "tmux_1_8")]
101    pub fn left(mut self) -> Self {
102        self.left = true;
103        self
104    }
105
106    /// `[-M]` - begin mouse resizing
107    #[cfg(feature = "tmux_2_1")]
108    pub fn mouse(mut self) -> Self {
109        self.mouse = true;
110        self
111    }
112
113    /// `[-R]` - resize right by adjustment
114    #[cfg(feature = "tmux_1_8")]
115    pub fn right(mut self) -> Self {
116        self.right = true;
117        self
118    }
119
120    /// `[-T]` - trims all lines below the current cursor position
121    #[cfg(feature = "tmux_3_2")]
122    pub fn trim(mut self) -> Self {
123        self.trim = true;
124        self
125    }
126
127    /// `[-U]` - resize up by adjustment
128    #[cfg(feature = "tmux_0_9")]
129    pub fn up(mut self) -> Self {
130        self.up = true;
131        self
132    }
133
134    /// `[-Z]` - the active pane is toggled between zoomed and unzoomed
135    #[cfg(feature = "tmux_1_8")]
136    pub fn zoom(mut self) -> Self {
137        self.zoom = true;
138        self
139    }
140
141    /// `[-t target-pane]` - target-pane
142    #[cfg(feature = "tmux_0_9")]
143    pub fn target_pane<S: Into<Cow<'a, str>>>(mut self, target_pane: S) -> Self {
144        self.target_pane = Some(target_pane.into());
145        self
146    }
147
148    /// `[-x width]` - absolute size
149    #[cfg(feature = "tmux_1_8")]
150    pub fn width(mut self, width: usize) -> Self {
151        self.width = Some(width);
152        self
153    }
154
155    /// `[-y height]` - absolute size
156    #[cfg(feature = "tmux_1_8")]
157    pub fn height(mut self, height: usize) -> Self {
158        self.height = Some(height);
159        self
160    }
161
162    /// `[adjustment]` - adjustment
163    #[cfg(feature = "tmux_0_9")]
164    pub fn adjustment<S: Into<Cow<'a, str>>>(mut self, adjustment: S) -> Self {
165        self.adjustment = Some(adjustment.into());
166        self
167    }
168
169    pub fn build(self) -> TmuxCommand<'a> {
170        let mut cmd = TmuxCommand::new();
171
172        cmd.name(RESIZE_PANE);
173
174        // `[-D]` - resize down by adjustment
175        #[cfg(feature = "tmux_0_9")]
176        if self.down {
177            cmd.push_flag(D_UPPERCASE_KEY);
178        }
179
180        // `[-L]` - resize left by adjustment
181        #[cfg(feature = "tmux_1_8")]
182        if self.left {
183            cmd.push_flag(L_UPPERCASE_KEY);
184        }
185
186        // `[-M]` - begin mouse resizing
187        #[cfg(feature = "tmux_2_1")]
188        if self.mouse {
189            cmd.push_flag(M_UPPERCASE_KEY);
190        }
191
192        // `[-R]` - resize right by adjustment
193        #[cfg(feature = "tmux_1_8")]
194        if self.right {
195            cmd.push_flag(R_UPPERCASE_KEY);
196        }
197
198        // `[-T]` - trims all lines below the current cursor position
199        #[cfg(feature = "tmux_3_2")]
200        if self.trim {
201            cmd.push_flag(T_UPPERCASE_KEY);
202        }
203
204        // `[-U]` - resize up by adjustment
205        #[cfg(feature = "tmux_0_9")]
206        if self.up {
207            cmd.push_flag(U_UPPERCASE_KEY);
208        }
209
210        // `[-Z]` - the active pane is toggled between zoomed and unzoomed
211        #[cfg(feature = "tmux_1_8")]
212        if self.zoom {
213            cmd.push_flag(Z_UPPERCASE_KEY);
214        }
215
216        // `[-t target-pane]` - target-pane
217        #[cfg(feature = "tmux_0_9")]
218        if let Some(target_pane) = self.target_pane {
219            cmd.push_option(T_LOWERCASE_KEY, target_pane);
220        }
221
222        // `[-x width]` - absolute size
223        #[cfg(feature = "tmux_1_8")]
224        if let Some(width) = self.width {
225            cmd.push_option(X_LOWERCASE_KEY, width.to_string());
226        }
227
228        // `[-y height]` - absolute size
229        #[cfg(feature = "tmux_1_8")]
230        if let Some(height) = self.height {
231            cmd.push_option(Y_LOWERCASE_KEY, height.to_string());
232        }
233
234        // `[adjustment]` - adjustment
235        #[cfg(feature = "tmux_0_9")]
236        if let Some(adjustment) = self.adjustment {
237            cmd.push_param(adjustment);
238        }
239
240        cmd
241    }
242}