tmux_interface/commands/windows_and_panes/
swap_pane.rs

1use crate::commands::constants::*;
2use crate::TmuxCommand;
3use std::borrow::Cow;
4use std::marker::PhantomData;
5
6pub type SwapP<'a> = SwapPane<'a>;
7
8/// Swap two panes
9///
10/// # Manual
11///
12/// tmux ^3.1:
13/// ```text
14/// swap-pane [-dDUZ] [-s src-pane] [-t dst-pane]
15/// (alias: swapp)
16/// ```
17///
18/// tmux ^1.0:
19/// ```text
20/// swap-pane [-dDU] [-s src-pane] [-t dst-pane]
21/// (alias: swapp)
22/// ```
23///
24/// tmux ^0.8:
25/// ```text
26/// swap-pane [-dDU] [-p src-index] [-t target-window] [-q dst-index]
27/// (alias: swapp)
28/// ```
29#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
30pub struct SwapPane<'a> {
31    /// `[-d]` - instruct tmux not to change the active pane
32    #[cfg(feature = "tmux_0_8")]
33    pub detached: bool,
34
35    /// `[-D]` - swap with the next pane
36    #[cfg(feature = "tmux_0_8")]
37    pub previous_pane: bool,
38
39    /// `[-U]` - swap with the previous pane
40    #[cfg(feature = "tmux_0_8")]
41    pub next_pane: bool,
42
43    /// `[-Z]` - keep the window zoomed if it was zoomed
44    #[cfg(feature = "tmux_3_1")]
45    pub keep_zoomed: bool,
46
47    /// `[-s src-pane]` - src-pane
48    #[cfg(feature = "tmux_1_0")]
49    pub src_pane: Option<Cow<'a, str>>,
50
51    /// `[-t dst-pane]` - dst-pane
52    #[cfg(feature = "tmux_1_0")]
53    pub dst_pane: Option<Cow<'a, str>>,
54
55    _phantom_data: PhantomData<&'a ()>,
56}
57
58impl<'a> SwapPane<'a> {
59    pub fn new() -> Self {
60        Default::default()
61    }
62
63    /// `[-d]` - instruct tmux not to change the active pane
64    #[cfg(feature = "tmux_0_8")]
65    pub fn detached(mut self) -> Self {
66        self.detached = true;
67        self
68    }
69
70    /// `[-D]` - swap with the next pane
71    #[cfg(feature = "tmux_0_8")]
72    pub fn previous_pane(mut self) -> Self {
73        self.previous_pane = true;
74        self
75    }
76
77    /// `[-U]` - swap with the previous pane
78    #[cfg(feature = "tmux_0_8")]
79    pub fn next_pane(mut self) -> Self {
80        self.next_pane = true;
81        self
82    }
83
84    /// `[-Z]` - keep the window zoomed if it was zoomed
85    #[cfg(feature = "tmux_3_1")]
86    pub fn keep_zoomed(mut self) -> Self {
87        self.keep_zoomed = true;
88        self
89    }
90
91    /// `[-s src-pane]` - src-pane
92    #[cfg(feature = "tmux_1_0")]
93    pub fn src_pane<S: Into<Cow<'a, str>>>(mut self, src_pane: S) -> Self {
94        self.src_pane = Some(src_pane.into());
95        self
96    }
97
98    /// `[-t dst-pane]` - dst-pane
99    #[cfg(feature = "tmux_1_0")]
100    pub fn dst_pane<S: Into<Cow<'a, str>>>(mut self, dst_pane: S) -> Self {
101        self.dst_pane = Some(dst_pane.into());
102        self
103    }
104
105    pub fn build(self) -> TmuxCommand<'a> {
106        let mut cmd = TmuxCommand::new();
107
108        cmd.name(SWAP_PANE);
109
110        // `[-d]` - instruct tmux not to change the active pane
111        #[cfg(feature = "tmux_0_8")]
112        if self.detached {
113            cmd.push_flag(D_LOWERCASE_KEY);
114        }
115
116        // `[-D]` - swap with the next pane
117        #[cfg(feature = "tmux_0_8")]
118        if self.previous_pane {
119            cmd.push_flag(D_UPPERCASE_KEY);
120        }
121
122        // `[-U]` - swap with the previous pane
123        #[cfg(feature = "tmux_0_8")]
124        if self.next_pane {
125            cmd.push_flag(U_UPPERCASE_KEY);
126        }
127
128        // `[-Z]` - keep the window zoomed if it was zoomed
129        #[cfg(feature = "tmux_3_1")]
130        if self.keep_zoomed {
131            cmd.push_flag(Z_UPPERCASE_KEY);
132        }
133
134        // `[-s src-pane]` - src-pane
135        #[cfg(feature = "tmux_1_0")]
136        if let Some(src_pane) = self.src_pane {
137            cmd.push_option(S_LOWERCASE_KEY, src_pane);
138        }
139
140        // `[-t dst-pane]` - dst-pane
141        #[cfg(feature = "tmux_1_0")]
142        if let Some(dst_pane) = self.dst_pane {
143            cmd.push_option(T_LOWERCASE_KEY, dst_pane);
144        }
145
146        cmd
147    }
148}