tmux_interface/options/pane/
pane_options.rs

1use super::constants::*;
2use crate::options::common::{cow_parse, get_parts, option_to_string};
3use crate::{Error, Switch};
4use std::borrow::Cow;
5use std::collections::HashMap;
6use std::fmt;
7use std::str::FromStr;
8
9#[cfg(feature = "tmux_3_0")]
10use crate::RemainOnExit;
11
12// TODO: check types
13#[derive(PartialEq, Default, Clone, Debug)]
14pub struct PaneOptions<'a> {
15    /// tmux ^3.0:
16    /// ```text
17    /// allow-rename [on | off]
18    /// ```
19    #[cfg(feature = "tmux_3_0")]
20    pub allow_rename: Option<Switch>,
21
22    /// tmux ^3.0:
23    /// ```text
24    /// alternate-screen [on | off]
25    /// ```
26    #[cfg(feature = "tmux_3_0")]
27    pub alternate_screen: Option<Switch>,
28
29    /// tmux ^3.2:
30    /// ```text
31    /// remain-on-exit [on | off | failed]
32    /// ```
33    ///
34    /// tmux ^3.0:
35    /// ```text
36    /// remain-on-exit [on | off]
37    /// ```
38    #[cfg(feature = "tmux_3_0")]
39    pub remain_on_exit: Option<RemainOnExit>,
40
41    /// tmux ^3.0:
42    /// ```text
43    /// window-active-style style
44    /// ```
45    #[cfg(feature = "tmux_3_0")]
46    pub window_active_style: Option<Cow<'a, str>>,
47
48    /// tmux ^3.0:
49    /// ```text
50    /// window-style style
51    /// ```
52    #[cfg(feature = "tmux_3_0")]
53    pub window_style: Option<Cow<'a, str>>,
54
55    /// tmux ^3.2:
56    /// ```text
57    /// synchronize-panes [on | off]
58    /// ```
59    #[cfg(feature = "tmux_3_2")]
60    pub synchronize_panes: Option<Switch>,
61
62    #[cfg(feature = "tmux_3_0")]
63    pub user_options: HashMap<String, Option<Cow<'a, str>>>,
64}
65
66impl<'a> PaneOptions<'a> {
67    // allows selective set by bitmask
68    // NOTE: in tmux_2_6 not exists pane
69    // pub fn set(&self, bitflags: usize) -> Result<(), Error> {
70}
71
72impl<'a> fmt::Display for PaneOptions<'a> {
73    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74        let mut v = Vec::new();
75
76        #[cfg(feature = "tmux_3_0")]
77        option_to_string(&mut v, ALLOW_RENAME, &self.allow_rename);
78        #[cfg(feature = "tmux_3_0")]
79        option_to_string(&mut v, ALTERNATE_SCREEN, &self.alternate_screen);
80        #[cfg(feature = "tmux_3_0")]
81        option_to_string(&mut v, REMAIN_ON_EXIT, &self.remain_on_exit);
82        #[cfg(feature = "tmux_3_0")]
83        option_to_string(&mut v, WINDOW_ACTIVE_STYLE, &self.window_active_style);
84        #[cfg(feature = "tmux_3_0")]
85        option_to_string(&mut v, WINDOW_STYLE, &self.window_style);
86        #[cfg(feature = "tmux_3_2")]
87        option_to_string(&mut v, SYNCHRONIZE_PANES, &self.synchronize_panes);
88        // #[cfg(feature = "tmux_3_0")]
89        // option_to_string(&mut v, , &self.user_options);
90        let s = v.join("\n");
91        write!(f, "{}", s)
92    }
93}
94
95impl<'a> PaneOptions<'a> {
96    pub fn new() -> Self {
97        let pane_options = PaneOptions::default();
98        #[cfg(feature = "tmux_3_0")]
99        let pane_options = pane_options.allow_rename(Some(ALLOW_RENAME_DEFAULT));
100        #[cfg(feature = "tmux_3_0")]
101        let pane_options = pane_options.alternate_screen(Some(ALTERNATE_SCREEN_DEFAULT));
102        #[cfg(feature = "tmux_3_0")]
103        let pane_options = pane_options.remain_on_exit(Some(REMAIN_ON_EXIT_DEFAULT));
104        #[cfg(feature = "tmux_3_0")]
105        let pane_options = pane_options.window_active_style(Some(WINDOW_ACTIVE_STYLE_DEFAULT));
106        #[cfg(feature = "tmux_3_0")]
107        let pane_options = pane_options.window_style(Some(WINDOW_STYLE_DEFAULT));
108        #[cfg(feature = "tmux_3_2")]
109        let pane_options = pane_options.synchronize_panes(Some(SYNCHRONIZE_PANES_DEFAULT));
110        // #[cfg(feature = "tmux_3_0")]
111        // let pane_options = pane_options.user_options();
112        pane_options
113    }
114
115    /// tmux ^3.0:
116    /// ```text
117    /// allow-rename [on | off]
118    /// ```
119    #[cfg(feature = "tmux_3_0")]
120    pub fn allow_rename(mut self, allow_rename: Option<Switch>) -> Self {
121        self.allow_rename = allow_rename;
122        self
123    }
124
125    /// tmux ^3.0:
126    /// ```text
127    /// alternate-screen [on | off]
128    /// ```
129    #[cfg(feature = "tmux_3_0")]
130    pub fn alternate_screen(mut self, alternate_screen: Option<Switch>) -> Self {
131        self.alternate_screen = alternate_screen;
132        self
133    }
134
135    /// tmux ^3.2:
136    /// ```text
137    /// remain-on-exit [on | off | failed]
138    /// ```
139    ///
140    /// tmux ^3.0:
141    /// ```text
142    /// remain-on-exit [on | off]
143    /// ```
144    #[cfg(feature = "tmux_3_0")]
145    pub fn remain_on_exit(mut self, remain_on_exit: Option<RemainOnExit>) -> Self {
146        self.remain_on_exit = remain_on_exit;
147        self
148    }
149
150    /// tmux ^3.0:
151    /// ```text
152    /// window-active-style style
153    /// ```
154    #[cfg(feature = "tmux_3_0")]
155    pub fn window_active_style<S: Into<Cow<'a, str>>>(
156        mut self,
157        window_active_style: Option<S>,
158    ) -> Self {
159        self.window_active_style = window_active_style.map(|s| s.into());
160        self
161    }
162
163    /// tmux ^3.0:
164    /// ```text
165    /// window-style style
166    /// ```
167    #[cfg(feature = "tmux_3_0")]
168    pub fn window_style<S: Into<Cow<'a, str>>>(mut self, window_style: Option<S>) -> Self {
169        self.window_style = window_style.map(|s| s.into());
170        self
171    }
172
173    /// tmux ^3.0:
174    /// ```text
175    /// synchronize-panes [on | off]
176    /// ```
177    #[cfg(feature = "tmux_3_2")]
178    pub fn synchronize_panes(mut self, synchronize_panes: Option<Switch>) -> Self {
179        self.synchronize_panes = synchronize_panes;
180        self
181    }
182}
183impl<'a> FromStr for PaneOptions<'a> {
184    type Err = Error;
185
186    fn from_str(s: &str) -> Result<Self, Self::Err> {
187        let mut pane_options = PaneOptions::default();
188
189        for line in s.lines() {
190            if let Some((name, _, value)) = get_parts(line) {
191                match name {
192                    #[cfg(feature = "tmux_3_0")]
193                    ALLOW_RENAME => pane_options.allow_rename = value.and_then(|s| s.parse().ok()),
194                    #[cfg(feature = "tmux_3_0")]
195                    ALTERNATE_SCREEN => {
196                        pane_options.alternate_screen = value.and_then(|s| s.parse().ok())
197                    }
198                    #[cfg(feature = "tmux_3_0")]
199                    REMAIN_ON_EXIT => {
200                        pane_options.remain_on_exit = value.and_then(|s| s.parse().ok())
201                    }
202                    #[cfg(feature = "tmux_3_0")]
203                    WINDOW_ACTIVE_STYLE => pane_options.window_active_style = cow_parse(value),
204                    #[cfg(feature = "tmux_3_0")]
205                    WINDOW_STYLE => pane_options.window_style = cow_parse(value),
206                    #[cfg(feature = "tmux_3_2")]
207                    SYNCHRONIZE_PANES => {
208                        pane_options.synchronize_panes = value.and_then(|s| s.parse().ok())
209                    }
210                    _ => {
211                        // if user option (@user_option value)
212                        if let Some(name) = name.strip_prefix('@') {
213                            pane_options
214                                .user_options
215                                .insert(name.to_string(), cow_parse(value));
216                        }
217                    }
218                }
219            }
220        }
221
222        Ok(pane_options)
223    }
224}