tmux_interface/commands/buffers/
load_buffer_macro.rs

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