tmux_interface/commands/buffers/
save_buffer.rs

1use crate::commands::constants::*;
2use crate::TmuxCommand;
3use std::borrow::Cow;
4
5pub type SaveB<'a> = SaveBuffer<'a>;
6
7/// Save the contents of the specified paste buffer to path.
8///
9/// # Manual
10///
11/// tmux ^2.0:
12/// ```text
13/// save-buffer [-a] [-b buffer-name] path
14/// (alias: saveb)
15/// ```
16///
17/// tmux ^1.5:
18/// ```text
19/// save-buffer [-a] [-b buffer-index] path
20/// (alias: saveb)
21/// ```
22///
23/// tmux ^0.8:
24/// ```text
25/// save-buffer [-a] [-b buffer-index] [-t target-session] path
26/// (alias: saveb)
27/// ```
28#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
29pub struct SaveBuffer<'a> {
30    /// `[-a]`
31    #[cfg(feature = "tmux_0_8")]
32    pub append: bool,
33
34    /// `[-b buffer-name]`
35    #[cfg(feature = "tmux_2_0")]
36    pub buffer_name: Option<Cow<'a, str>>,
37
38    /// `[-b buffer-index]`
39    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_2_0")))]
40    pub buffer_index: Option<Cow<'a, str>>,
41
42    /// `[-t target-session]`
43    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
44    pub target_session: Option<Cow<'a, str>>,
45
46    /// `[path]`
47    #[cfg(feature = "tmux_0_8")]
48    pub path: Option<Cow<'a, str>>,
49}
50
51impl<'a> SaveBuffer<'a> {
52    pub fn new() -> Self {
53        Default::default()
54    }
55
56    /// `[-a]`
57    #[cfg(feature = "tmux_0_8")]
58    pub fn append(mut self) -> Self {
59        self.append = true;
60        self
61    }
62
63    /// `[-b buffer-name]`
64    #[cfg(feature = "tmux_2_0")]
65    pub fn buffer_name<S: Into<Cow<'a, str>>>(mut self, buffer_name: S) -> Self {
66        self.buffer_name = Some(buffer_name.into());
67        self
68    }
69
70    /// `[-b buffer-index]`
71    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_2_0")))]
72    pub fn buffer_index<S: Into<Cow<'a, str>>>(mut self, buffer_index: S) -> Self {
73        self.buffer_index = Some(buffer_index.into());
74        self
75    }
76
77    /// `[-t target-session]`
78    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
79    pub fn target_session<S: Into<Cow<'a, str>>>(mut self, target_session: S) -> Self {
80        self.target_session = Some(target_session.into());
81        self
82    }
83
84    /// `[path]`
85    #[cfg(feature = "tmux_0_8")]
86    pub fn path<S: Into<Cow<'a, str>>>(mut self, path: S) -> Self {
87        self.path = Some(path.into());
88        self
89    }
90
91    pub fn build(self) -> TmuxCommand<'a> {
92        let mut cmd = TmuxCommand::new();
93
94        cmd.name(SAVE_BUFFER);
95
96        // `[-a]`
97        #[cfg(feature = "tmux_0_8")]
98        if self.append {
99            cmd.push_flag(A_LOWERCASE_KEY);
100        }
101
102        // `[-b buffer-name]`
103        #[cfg(feature = "tmux_2_0")]
104        if let Some(buffer_name) = self.buffer_name {
105            cmd.push_option(B_LOWERCASE_KEY, buffer_name);
106        }
107
108        // `[-b buffer-index]`
109        #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_2_0")))]
110        if let Some(buffer_index) = self.buffer_index {
111            cmd.push_option(B_LOWERCASE_KEY, buffer_index);
112        }
113
114        // `[-t target-session]`
115        #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
116        if let Some(target_session) = self.target_session {
117            cmd.push_option(T_LOWERCASE_KEY, target_session);
118        }
119
120        // `[path]`
121        #[cfg(feature = "tmux_0_8")]
122        if let Some(path) = self.path {
123            cmd.push_param(path);
124        }
125
126        cmd
127    }
128}