tmux_interface/commands/options/
set_option.rs

1use crate::commands::constants::*;
2use crate::TmuxCommand;
3use std::borrow::Cow;
4
5pub type Set<'a> = SetOption<'a>;
6
7/// Structure for setting a pane/window/session/server option
8///
9/// # Manual
10///
11/// tmux ^3.2:
12/// ```text
13/// set-option [-aFgopqsuUw] [-t target-pane] option value
14/// (alias: set)
15/// ```
16///
17/// tmux ^3.0:
18/// ```text
19/// set-option [-aFgopqsuw] [-t target-pane] option value
20/// (alias: set)
21/// ```
22///
23/// tmux ^2.6:
24/// ```text
25/// set-option [-aFgoqsuw] [-t target-session | target-window] option value
26/// (alias: set)
27/// ```
28///
29/// tmux ^1.8:
30/// ```text
31/// set-option [-agoqsuw] [-t target-session | target-window] option value
32/// (alias: set)
33/// ```
34///
35/// tmux ^1.7:
36/// ```text
37/// set-option [-agqsuw] [-t target-session | target-window] option value
38/// (alias: set)
39/// ```
40///
41/// tmux ^1.2:
42/// ```text
43/// set-option [-agsuw] [-t target-session | target-window] option value
44/// (alias: set)
45/// ```
46///
47/// tmux ^1.0:
48/// ```text
49/// set-option [-agu] [-t target-session] option value
50/// (alias: set)
51/// ```
52///
53/// tmux ^0.8:
54/// ```text
55/// set-option [-gu] [-t target-session] option value
56/// (alias: set)
57/// ```
58#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
59pub struct SetOption<'a> {
60    /// `[-a]` - value is appended to the existing setting, if the option expects a string or a style
61    #[cfg(feature = "tmux_1_0")]
62    pub append: bool,
63
64    /// `[-F]` - expand formats in the option value
65    #[cfg(feature = "tmux_2_6")]
66    pub format: bool,
67
68    /// `[-g]` - the global session or window option is set
69    #[cfg(feature = "tmux_0_8")]
70    pub global: bool,
71
72    /// `[-o]` - prevents setting an option that is already set
73    #[cfg(feature = "tmux_1_8")]
74    pub not_overwrite: bool,
75
76    /// `[-p]` - set a pane option
77    #[cfg(feature = "tmux_3_0")]
78    pub pane: bool,
79
80    /// `[-q]` - suppress errors about unknown or ambiguous options
81    #[cfg(feature = "tmux_1_7")]
82    pub quiet: bool,
83
84    /// `[-s]` - set a server option
85    #[cfg(feature = "tmux_1_2")]
86    pub server: bool,
87
88    /// `[-u]` - unset an option, so a session inherits the option from the global options
89    #[cfg(feature = "tmux_0_8")]
90    pub unset: bool,
91
92    /// `[-U]` - unsets an option (like -u) but if the option is a pane option also unsets the option on any panes in the window
93    #[cfg(feature = "tmux_3_2")]
94    pub unset_on_all: bool,
95
96    /// `[-w]` - set a window option
97    #[cfg(feature = "tmux_1_2")]
98    pub window: bool,
99
100    /// `[-t target-pane]` - specify the target-pane
101    #[cfg(feature = "tmux_3_0")]
102    pub target_pane: Option<Cow<'a, str>>,
103
104    /// `[-t target-session | target-window]`
105    #[cfg(all(feature = "tmux_1_2", not(feature = "tmux_3_0")))]
106    pub target: Option<Cow<'a, str>>,
107
108    /// `[-t target-session]`
109    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_2")))]
110    pub target_session: Option<Cow<'a, str>>,
111
112    // FIXME: option valuer pair in one fn
113    /// `option`
114    pub option: Option<Cow<'a, str>>,
115
116    /// `value`
117    pub value: Option<Cow<'a, str>>,
118}
119
120impl<'a> SetOption<'a> {
121    pub fn new() -> Self {
122        Default::default()
123    }
124
125    /// `[-a]` - value is appended to the existing setting, if the option expects a string or a style
126    #[cfg(feature = "tmux_1_0")]
127    pub fn append(mut self) -> Self {
128        self.append = true;
129        self
130    }
131
132    /// `[-F]` - expand formats in the option value
133    #[cfg(feature = "tmux_2_6")]
134    pub fn format(mut self) -> Self {
135        self.format = true;
136        self
137    }
138
139    /// `[-g]` - the global session or window option is set
140    #[cfg(feature = "tmux_0_8")]
141    pub fn global(mut self) -> Self {
142        self.global = true;
143        self
144    }
145
146    /// `[-o]` - prevents setting an option that is already set
147    #[cfg(feature = "tmux_1_8")]
148    pub fn not_overwrite(mut self) -> Self {
149        self.not_overwrite = true;
150        self
151    }
152
153    /// `[-p]` - set a pane option
154    #[cfg(feature = "tmux_3_0")]
155    pub fn pane(mut self) -> Self {
156        self.pane = true;
157        self
158    }
159
160    /// `[-q]` - suppress errors about unknown or ambiguous options
161    #[cfg(feature = "tmux_1_7")]
162    pub fn quiet(mut self) -> Self {
163        self.quiet = true;
164        self
165    }
166
167    /// `[-s]` - set a server option
168    #[cfg(feature = "tmux_1_2")]
169    pub fn server(mut self) -> Self {
170        self.server = true;
171        self
172    }
173
174    /// `[-u]` - unset an option, so a session inherits the option from the global options
175    #[cfg(feature = "tmux_0_8")]
176    pub fn unset(mut self) -> Self {
177        self.unset = true;
178        self
179    }
180
181    /// `[-U]` - unsets an option (like -u) but if the option is a pane option also unsets the option on any panes in the window
182    #[cfg(feature = "tmux_3_2")]
183    pub fn unset_on_all(mut self) -> Self {
184        self.unset_on_all = true;
185        self
186    }
187
188    /// `[-w]` - set a window option
189    #[cfg(feature = "tmux_1_2")]
190    pub fn window(mut self) -> Self {
191        self.window = true;
192        self
193    }
194
195    /// `[-t target-pane]` - specify the target-pane
196    #[cfg(feature = "tmux_3_0")]
197    pub fn target_pane<S: Into<Cow<'a, str>>>(mut self, target_pane: S) -> Self {
198        self.target_pane = Some(target_pane.into());
199        self
200    }
201
202    /// `[-t target-session | target-window]`
203    #[cfg(all(feature = "tmux_1_2", not(feature = "tmux_3_0")))]
204    pub fn target<S: Into<Cow<'a, str>>>(mut self, target: S) -> Self {
205        self.target = Some(target.into());
206        self
207    }
208
209    /// `[-t target-session]`
210    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_2")))]
211    pub fn target_session<S: Into<Cow<'a, str>>>(mut self, target_session: S) -> Self {
212        self.target_session = Some(target_session.into());
213        self
214    }
215
216    // FIXME: option valuer pair in one fn
217
218    /// `option`
219    pub fn option<S: Into<Cow<'a, str>>>(mut self, option: S) -> Self {
220        self.option = Some(option.into());
221        self
222    }
223
224    /// `value`
225    pub fn value<S: Into<Cow<'a, str>>>(mut self, value: S) -> Self {
226        self.value = Some(value.into());
227        self
228    }
229
230    pub fn build(self) -> TmuxCommand<'a> {
231        let mut cmd = TmuxCommand::new();
232
233        cmd.name(SET_OPTION);
234
235        // `[-a]` - value is appended to the existing setting, if the option expects a string or a style
236        #[cfg(feature = "tmux_1_0")]
237        if self.append {
238            cmd.push_flag(A_LOWERCASE_KEY);
239        }
240
241        // `[-F]` - expand formats in the option value
242        #[cfg(feature = "tmux_2_6")]
243        if self.format {
244            cmd.push_flag(F_UPPERCASE_KEY);
245        }
246
247        // `[-g]` - the global session or window option is set
248        #[cfg(feature = "tmux_0_8")]
249        if self.global {
250            cmd.push_flag(G_LOWERCASE_KEY);
251        }
252
253        // `[-o]` - prevents setting an option that is already set
254        #[cfg(feature = "tmux_1_8")]
255        if self.not_overwrite {
256            cmd.push_flag(O_LOWERCASE_KEY);
257        }
258
259        // `[-p]` - set a pane option
260        #[cfg(feature = "tmux_3_0")]
261        if self.pane {
262            cmd.push_flag(P_LOWERCASE_KEY);
263        }
264
265        // `[-q]` - suppress errors about unknown or ambiguous options
266        #[cfg(feature = "tmux_1_7")]
267        if self.quiet {
268            cmd.push_flag(Q_LOWERCASE_KEY);
269        }
270
271        // `[-s]` - set a server option
272        #[cfg(feature = "tmux_1_2")]
273        if self.server {
274            cmd.push_flag(S_LOWERCASE_KEY);
275        }
276
277        // `[-u]` - unset an option, so a session inherits the option from the global options
278        #[cfg(feature = "tmux_0_8")]
279        if self.unset {
280            cmd.push_flag(U_LOWERCASE_KEY);
281        }
282
283        // `[-U]` - unsets an option (like -u) but if the option is a pane option also unsets the option on any panes in the window
284        #[cfg(feature = "tmux_3_2")]
285        if self.unset_on_all {
286            cmd.push_flag(U_UPPERCASE_KEY);
287        }
288
289        // `[-w]` - set a window option
290        #[cfg(feature = "tmux_1_2")]
291        if self.window {
292            cmd.push_flag(W_LOWERCASE_KEY);
293        }
294
295        // `[-t target-pane]` - specify the target-pane
296        #[cfg(feature = "tmux_3_0")]
297        if let Some(target_pane) = self.target_pane {
298            cmd.push_option(T_LOWERCASE_KEY, target_pane);
299        }
300
301        // `[-t target-session | target-window]`
302        #[cfg(all(feature = "tmux_1_2", not(feature = "tmux_3_0")))]
303        if let Some(target) = self.target {
304            cmd.push_option(T_LOWERCASE_KEY, target);
305        }
306
307        // `[-t target-session]`
308        #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_2")))]
309        if let Some(target_session) = self.target_session {
310            cmd.push_option(T_LOWERCASE_KEY, target_session);
311        }
312
313        // FIXME: option valuer pair in one fn
314
315        // `option`
316        if let Some(option) = self.option {
317            cmd.push_param(option);
318        }
319
320        // `value`
321        if let Some(value) = self.value {
322            cmd.push_param(value);
323        }
324
325        cmd
326    }
327}