1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/// This is similar to link-window, except the source and destination windows are swapped
///
/// # Manual
///
/// tmux ^0.8:
/// ```text
/// swap-window [-d] [-s src-window] [-t dst-window]
/// (alias: swapw)
/// ```
#[macro_export]
macro_rules! swap_window {
    // `[-d]`
    (@cmd ($cmd:expr) -d, $($tail:tt)*) => {{
        $crate::swap_window!(@cmd ({
            $cmd.detached()
        }) $($tail)*)
    }};
    // `[-s src-window]`
    (@cmd ($cmd:expr) -s $src_window:expr, $($tail:tt)*) => {{
        $crate::swap_window!(@cmd ({
            $cmd.src_window($src_window)
        }) $($tail)*)
    }};
    // `[-t dst-window]`
    (@cmd ($cmd:expr) -t $dst_window:expr, $($tail:tt)*) => {{
        $crate::swap_window!(@cmd ({
            $cmd.dst_window($dst_window)
        }) $($tail)*)
    }};
    //(@cmd ($cmd:expr) -$unknown:tt, $($tail:tt)*) => {{
        //::std::compile_error!("unknown flag, option or parameter: {}", $unknown);
    //}};
    (@cmd ($cmd:expr)) => {{
        $cmd
    }};
    () => {{
        $crate::SwapWindow::new()
    }};
    (($cmd:expr), $($tail:tt)*) => {{
        $crate::swap_window!(@cmd ($cmd) $($tail)*,)
    }};
    ($($tail:tt)*) => {{
        $crate::swap_window!(@cmd ({ $crate::SwapWindow::new() }) $($tail)*,)
    }};
}

#[test]
fn swap_window_macro() {
    use crate::TargetWindow;
    use std::borrow::Cow;

    // This is similar to link-window, except the source and destination windows are swapped
    //
    // # Manual
    //
    // tmux ^0.8:
    // ```text
    // swap-window [-d] [-s src-window] [-t dst-window]
    // (alias: swapw)
    // ```
    let src_window = TargetWindow::Raw("1").to_string();
    let dst_window = TargetWindow::Raw("2").to_string();

    let swap_window = swap_window!();
    #[cfg(feature = "tmux_0_8")]
    let swap_window = swap_window!((swap_window), -d);
    #[cfg(feature = "tmux_0_8")]
    let swap_window = swap_window!((swap_window), -s & src_window);
    #[cfg(feature = "tmux_0_8")]
    let swap_window = swap_window!((swap_window), -t & dst_window);

    #[cfg(not(feature = "cmd_alias"))]
    let cmd = "swap-window";
    #[cfg(feature = "cmd_alias")]
    let cmd = "swapw";

    let mut s = Vec::new();
    s.push(cmd);
    #[cfg(feature = "tmux_0_8")]
    s.push("-d");
    #[cfg(feature = "tmux_0_8")]
    s.extend_from_slice(&["-s", "1"]);
    #[cfg(feature = "tmux_0_8")]
    s.extend_from_slice(&["-t", "2"]);

    let s: Vec<Cow<str>> = s.into_iter().map(|a| a.into()).collect();

    let swap_window = swap_window.build().to_vec();

    assert_eq!(swap_window, s);
}