tmux_interface/commands/status_line/
display_popup.rs

1use crate::commands::constants::*;
2#[cfg(feature = "tmux_3_3")]
3use crate::commands::PopupBorderLinesType;
4use crate::commands::Size;
5use crate::TmuxCommand;
6use std::borrow::Cow;
7
8pub type Popup<'a> = DisplayPopup<'a>;
9
10/// Structure for displaying a menu on target-client
11///
12/// # Manual
13///
14/// tmux ^3.3:
15/// ```text
16/// display-popup [-BCE] [-b border-lines] [-c target-client] [-d start-directory] [-e environment]
17/// [-h height] [-s style] [-S border-style] [-t target-pane] [-T title] [-w width] [-x position]
18/// [-y position] [shell-command]
19/// ```
20///
21/// tmux ^3.2:
22/// ```text
23/// display-popup [-CE] [-c target-client] [-d start-directory] [-h height] [-t target-pane]
24/// [-w width] [-x position] [-y position] [shell-command]
25/// (alias: popup)
26/// ```
27#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
28#[cfg(feature = "tmux_3_2")]
29pub struct DisplayPopup<'a> {
30    /// `[-B]` - not surround the popup by a border
31    #[cfg(feature = "tmux_3_3")]
32    pub no_border: bool,
33
34    /// `[-C]` - closes any popup on the client
35    #[cfg(feature = "tmux_3_2")]
36    pub close: bool,
37
38    /// `[-E]` - closes the popup automatically when shell-command exits
39    #[cfg(feature = "tmux_3_2")]
40    pub close_on_exit: bool,
41
42    /// `[-EE]` - closes the popup only if shell-command exited with success
43    #[cfg(feature = "tmux_3_2")]
44    pub close_on_success: bool,
45
46    /// `[-b border-lines]` - sets the type of border line for the popup.  When -B is specified, the
47    /// -b option is ignored.  See popup-border-lines for possible values for border-lines
48    #[cfg(feature = "tmux_3_3")]
49    pub border_lines: Option<PopupBorderLinesType>,
50
51    /// `[-c target-client]` - target-client
52    #[cfg(feature = "tmux_3_2")]
53    pub target_client: Option<Cow<'a, str>>,
54
55    /// `[-d start-directory]` -
56    #[cfg(feature = "tmux_3_2")]
57    pub start_directory: Option<Cow<'a, str>>,
58
59    // XXX: struct instead of tuple
60    /// `[-e environment]` - takes the form ‘VARIABLE=value’ and sets an environment variable for
61    /// the popup; it may be specified multiple times
62    #[cfg(feature = "tmux_3_3")]
63    pub environment: Option<(Cow<'a, str>, Cow<'a, str>)>,
64
65    /// `[-h height]` - height of the popup
66    #[cfg(feature = "tmux_3_2")]
67    pub height: Option<Size>,
68
69    // TODO: not imlemented!
70    /// `[-s style]` - sets the style for the popup
71    #[cfg(feature = "tmux_3_3")]
72    pub style: Option<Cow<'a, str>>,
73
74    // TODO: not imlemented!
75    /// `[-S border-style]` - sets the style for the popup border
76    #[cfg(feature = "tmux_3_3")]
77    pub border_style: Option<Cow<'a, str>>,
78
79    /// `[-t target-pane]` - target-pane
80    #[cfg(feature = "tmux_3_2")]
81    pub target_pane: Option<Cow<'a, str>>,
82
83    /// `[-T title]` - is a format for the popup title
84    #[cfg(feature = "tmux_3_3")]
85    pub title: Option<Cow<'a, str>>,
86
87    /// `[-w width]` - width of the popup
88    #[cfg(feature = "tmux_3_2")]
89    pub width: Option<Size>,
90
91    /// `[-x position]` - x position of the popup
92    #[cfg(feature = "tmux_3_2")]
93    pub x: Option<Size>,
94
95    /// `[-y position]` - y position of the popup
96    #[cfg(feature = "tmux_3_2")]
97    pub y: Option<Size>,
98
99    /// `[shell-command]` - shell-command
100    #[cfg(feature = "tmux_3_2")]
101    pub shell_command: Option<Cow<'a, str>>,
102}
103
104impl<'a> DisplayPopup<'a> {
105    pub fn new() -> Self {
106        Default::default()
107    }
108
109    /// `[-B]` - not surround the popup by a border
110    #[cfg(feature = "tmux_3_3")]
111    pub fn no_border(mut self) -> Self {
112        self.no_border = true;
113        self
114    }
115
116    /// `[-C]` - closes any popup on the client
117    #[cfg(feature = "tmux_3_2")]
118    pub fn close(mut self) -> Self {
119        self.close = true;
120        self
121    }
122
123    /// `[-E]` - closes the popup automatically when shell-command exits
124    #[cfg(feature = "tmux_3_2")]
125    pub fn close_on_exit(mut self) -> Self {
126        self.close_on_exit = true;
127        self
128    }
129
130    /// `[-EE]` - closes the popup only if shell-command exited with success
131    #[cfg(feature = "tmux_3_2")]
132    pub fn close_on_success(mut self) -> Self {
133        self.close_on_success = true;
134        self
135    }
136
137    /// `[-b border-lines]` - sets the type of border line for the popup.  When -B is specified, the
138    /// -b option is ignored.  See popup-border-lines for possible values for border-lines
139    #[cfg(feature = "tmux_3_3")]
140    pub fn border_lines(mut self, border_lines: PopupBorderLinesType) -> Self {
141        self.border_lines = Some(border_lines);
142        self
143    }
144
145    /// `[-c target-client]` - target-client
146    #[cfg(feature = "tmux_3_2")]
147    pub fn target_client<S: Into<Cow<'a, str>>>(mut self, target_client: S) -> Self {
148        self.target_client = Some(target_client.into());
149        self
150    }
151
152    /// `[-d start-directory]` - start-directory
153    #[cfg(feature = "tmux_3_2")]
154    pub fn start_directory<S: Into<Cow<'a, str>>>(mut self, start_directory: S) -> Self {
155        self.start_directory = Some(start_directory.into());
156        self
157    }
158
159    // TODO: vec
160    /// `[-e environment]` - takes the form ‘VARIABLE=value’ and sets an environment variable for
161    /// the popup; it may be specified multiple times
162    #[cfg(feature = "tmux_3_3")]
163    pub fn environment<S: Into<Cow<'a, str>>>(mut self, variable: S, value: S) -> Self {
164        self.environment = Some((variable.into(), value.into()));
165        self
166    }
167
168    /// `[-h height]` - height of the popup
169    #[cfg(feature = "tmux_3_2")]
170    pub fn height(mut self, height: Size) -> Self {
171        self.height = Some(height);
172        self
173    }
174
175    /// `[-s style]` - sets the style for the popup
176    #[cfg(feature = "tmux_3_3")]
177    pub fn style<S: Into<Cow<'a, str>>>(mut self, style: S) -> Self {
178        self.style = Some(style.into());
179        self
180    }
181
182    /// `[-S border-style]` - sets the style for the popup border
183    #[cfg(feature = "tmux_3_3")]
184    pub fn border_style<S: Into<Cow<'a, str>>>(mut self, border_style: S) -> Self {
185        self.border_style = Some(border_style.into());
186        self
187    }
188
189    /// `[-t target-pane]` - target-pane
190    #[cfg(feature = "tmux_3_2")]
191    pub fn target_pane<S: Into<Cow<'a, str>>>(mut self, target_pane: S) -> Self {
192        self.target_pane = Some(target_pane.into());
193        self
194    }
195
196    /// `[-T title]` - is a format for the popup title
197    #[cfg(feature = "tmux_3_3")]
198    pub fn title<S: Into<Cow<'a, str>>>(mut self, title: S) -> Self {
199        self.title = Some(title.into());
200        self
201    }
202
203    /// `[-w width]` - width of the pane
204    #[cfg(feature = "tmux_3_2")]
205    pub fn width(mut self, width: Size) -> Self {
206        self.width = Some(width);
207        self
208    }
209
210    /// `[-x position]` - x position of the pane
211    #[cfg(feature = "tmux_3_2")]
212    pub fn x(mut self, x: Size) -> Self {
213        self.x = Some(x);
214        self
215    }
216
217    /// `[-y position]` - y position of the pane
218    #[cfg(feature = "tmux_3_2")]
219    pub fn y(mut self, y: Size) -> Self {
220        self.y = Some(y);
221        self
222    }
223
224    /// `[shell-command]` - shell-command
225    #[cfg(feature = "tmux_3_2")]
226    pub fn shell_command<S: Into<Cow<'a, str>>>(mut self, shell_command: S) -> Self {
227        self.shell_command = Some(shell_command.into());
228        self
229    }
230
231    pub fn build(self) -> TmuxCommand<'a> {
232        let mut cmd = TmuxCommand::new();
233
234        cmd.name(DISPLAY_POPUP);
235
236        // `[-B]` - not surround the popup by a border
237        #[cfg(feature = "tmux_3_3")]
238        if self.no_border {
239            cmd.push_flag(B_UPPERCASE_KEY);
240        }
241
242        // `[-C]` - closes any popup on the client
243        #[cfg(feature = "tmux_3_2")]
244        if self.close {
245            cmd.push_flag(C_UPPERCASE_KEY);
246        }
247
248        // `[-E]` - closes the popup automatically when shell-command exits
249        #[cfg(feature = "tmux_3_2")]
250        if self.close_on_exit {
251            cmd.push_flag(E_UPPERCASE_KEY);
252        }
253
254        // `[-EE]` - closes the popup only if shell-command exited with success
255        #[cfg(feature = "tmux_3_2")]
256        if self.close_on_success {
257            cmd.push_flag(EE_UPPERCASE_KEY);
258        }
259
260        // `[-b border-lines]` - sets the type of border line for the popup
261        #[cfg(feature = "tmux_3_3")]
262        if let Some(border_lines) = self.border_lines {
263            cmd.push_option(B_LOWERCASE_KEY, border_lines.to_string());
264        }
265
266        // `[-c target-client]` - target-client
267        #[cfg(feature = "tmux_3_2")]
268        if let Some(target_client) = self.target_client {
269            cmd.push_option(C_LOWERCASE_KEY, target_client);
270        }
271
272        // `[-d start-directory]` - start-directory
273        #[cfg(feature = "tmux_3_2")]
274        if let Some(start_directory) = self.start_directory {
275            cmd.push_option(D_LOWERCASE_KEY, start_directory);
276        }
277
278        // `[-e environment]` - takes the form ‘VARIABLE=value’ and sets an environment variable for
279        // the popup; it may be specified multiple times
280        #[cfg(feature = "tmux_3_3")]
281        if let Some(environment) = self.environment {
282            cmd.push_option(
283                E_LOWERCASE_KEY,
284                format!("{}={}", environment.0, environment.1),
285            );
286        }
287
288        // `[-h height]` - height of the popup
289        #[cfg(feature = "tmux_3_2")]
290        if let Some(height) = self.height {
291            cmd.push_option(H_LOWERCASE_KEY, height.to_string());
292        }
293
294        // `[-s style]` - sets the style for the popup
295        #[cfg(feature = "tmux_3_3")]
296        if let Some(style) = self.style {
297            cmd.push_option(S_LOWERCASE_KEY, style);
298        }
299
300        // `[-S border-style]` - sets the style for the popup border
301        #[cfg(feature = "tmux_3_3")]
302        if let Some(border_style) = self.border_style {
303            cmd.push_option(S_UPPERCASE_KEY, border_style);
304        }
305
306        // `[-t target-pane]` - target-client
307        #[cfg(feature = "tmux_3_2")]
308        if let Some(target_pane) = self.target_pane {
309            cmd.push_option(T_LOWERCASE_KEY, target_pane);
310        }
311
312        // `[-T title]` - is a format for the popup title
313        #[cfg(feature = "tmux_3_3")]
314        if let Some(title) = self.title {
315            cmd.push_option(T_UPPERCASE_KEY, title);
316        }
317
318        // `[-w width]` - width of the popup
319        #[cfg(feature = "tmux_3_2")]
320        if let Some(width) = self.width {
321            cmd.push_option(W_LOWERCASE_KEY, width.to_string());
322        }
323
324        // `[-x position]` - x position of the popup
325        #[cfg(feature = "tmux_3_2")]
326        if let Some(x) = self.x {
327            cmd.push_option(X_LOWERCASE_KEY, x.to_string());
328        }
329
330        // `[-y position]` - y position of the popup
331        #[cfg(feature = "tmux_3_2")]
332        if let Some(y) = self.y {
333            cmd.push_option(Y_LOWERCASE_KEY, y.to_string());
334        }
335
336        // `[shell-command]` - shell-command
337        #[cfg(feature = "tmux_3_2")]
338        if let Some(shell_command) = self.shell_command {
339            cmd.push_param(shell_command);
340        }
341
342        cmd
343    }
344}