tmux_interface/options/pane/
pane_options.rs1use 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#[derive(PartialEq, Default, Clone, Debug)]
14pub struct PaneOptions<'a> {
15 #[cfg(feature = "tmux_3_0")]
20 pub allow_rename: Option<Switch>,
21
22 #[cfg(feature = "tmux_3_0")]
27 pub alternate_screen: Option<Switch>,
28
29 #[cfg(feature = "tmux_3_0")]
39 pub remain_on_exit: Option<RemainOnExit>,
40
41 #[cfg(feature = "tmux_3_0")]
46 pub window_active_style: Option<Cow<'a, str>>,
47
48 #[cfg(feature = "tmux_3_0")]
53 pub window_style: Option<Cow<'a, str>>,
54
55 #[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 }
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 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 pane_options
113 }
114
115 #[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 #[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 #[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 #[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 #[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 #[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 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}