tmux_interface/commands/buffers/
save_buffer_macro.rs

1/// Save the contents of the specified paste buffer to path.
2///
3/// # Manual
4///
5/// tmux ^2.0:
6/// ```text
7/// save-buffer [-a] [-b buffer-name] path
8/// (alias: saveb)
9/// ```
10///
11/// tmux ^1.5:
12/// ```text
13/// save-buffer [-a] [-b buffer-index] path
14/// (alias: saveb)
15/// ```
16///
17/// tmux ^0.8:
18/// ```text
19/// save-buffer [-a] [-b buffer-index] [-t target-session] path
20/// (alias: saveb)
21/// ```
22#[macro_export]
23macro_rules! save_buffer {
24    // `[-a]`
25    (@cmd ($cmd:expr) -a, $($tail:tt)*) => {{
26        $crate::save_buffer!(@cmd ({
27            $cmd.append()
28        }) $($tail)*)
29    }};
30    // `[-b buffer-name]`
31    (@cmd ($cmd:expr) -b $buffer:expr, $($tail:tt)*) => {{
32        $crate::save_buffer!(@cmd ({
33            #[cfg(feature = "tmux_2_0")]
34            {
35                $cmd.buffer_name($buffer)
36            }
37            #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_2_0")))]
38            {
39                $cmd.buffer_index($buffer)
40            }
41        }) $($tail)*)
42    }};
43    // `[-t target-session]`
44    (@cmd ($cmd:expr) -t $target_session:expr, $($tail:tt)*) => {{
45        $crate::save_buffer!(@cmd ({
46            $cmd.target_session($target_session)
47        }) $($tail)*)
48    }};
49    // `[path]`
50    (@cmd ($cmd:expr) $path:expr, $($tail:tt)*) => {{
51        $crate::save_buffer!(@cmd ({
52            $cmd.path($path)
53        }) $($tail)*)
54    }};
55    //(@cmd ($cmd:expr) -$unknown:tt, $($tail:tt)*) => {{
56        //::std::compile_error!("unknown flag, option or parameter: {}", $unknown);
57    //}};
58    (@cmd ($cmd:expr)) => {{
59        $cmd
60    }};
61    () => {{
62        $crate::SaveBuffer::new()
63    }};
64    (($cmd:expr), $($tail:tt)*) => {{
65        $crate::save_buffer!(@cmd ($cmd) $($tail)*,)
66    }};
67    ($($tail:tt)*) => {{
68        $crate::save_buffer!(@cmd ({ $crate::SaveBuffer::new() }) $($tail)*,)
69    }};
70}
71
72#[test]
73fn save_buffer_macro() {
74    use std::borrow::Cow;
75
76    // Save the contents of the specified paste buffer to path.
77    //
78    // # Manual
79    //
80    // tmux ^2.0:
81    // ```text
82    // save-buffer [-a] [-b buffer-name] path
83    // (alias: saveb)
84    // ```
85    //
86    // tmux ^1.5:
87    // ```text
88    // save-buffer [-a] [-b buffer-index] path
89    // (alias: saveb)
90    // ```
91    //
92    // tmux ^0.8:
93    // ```text
94    // save-buffer [-a] [-b buffer-index] [-t target-session] path
95    // (alias: saveb)
96    // ```
97    let save_buffer = save_buffer!();
98    #[cfg(feature = "tmux_0_8")]
99    let save_buffer = save_buffer!((save_buffer), -a);
100    #[cfg(feature = "tmux_2_0")]
101    let save_buffer = save_buffer!((save_buffer), -b "1");
102    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_2_0")))]
103    let save_buffer = save_buffer!((save_buffer), -b "2");
104    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
105    let save_buffer = save_buffer!((save_buffer), -t "3");
106    #[cfg(feature = "tmux_0_8")]
107    let save_buffer = save_buffer!((save_buffer), "4");
108
109    #[cfg(not(feature = "cmd_alias"))]
110    let cmd = "save-buffer";
111    #[cfg(feature = "cmd_alias")]
112    let cmd = "saveb";
113
114    let mut s = Vec::new();
115    s.push(cmd);
116    #[cfg(feature = "tmux_0_8")]
117    s.push("-a");
118    #[cfg(feature = "tmux_2_0")]
119    s.extend_from_slice(&["-b", "1"]);
120    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_2_0")))]
121    s.extend_from_slice(&["-b", "2"]);
122    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
123    s.extend_from_slice(&["-t", "3"]);
124    #[cfg(feature = "tmux_0_8")]
125    s.push("4");
126    let s: Vec<Cow<str>> = s.into_iter().map(|a| a.into()).collect();
127
128    let save_buffer = save_buffer.build().to_vec();
129
130    assert_eq!(save_buffer, s);
131}