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
/// # Manual
///
/// tmux ^1.5:
/// ```text
/// confirm-before [-p prompt] [-t target-client] command
/// (alias: confirm)
/// ```
///
/// tmux ^0.9:
/// ```text
/// confirm-before [-t target-client] command
/// (alias: confirm)
/// ```
#[macro_export]
macro_rules! confirm_before {
    (@cmd ($cmd:expr) -p $prompt:expr, $($tail:tt)*) => {{
        $crate::confirm_before!(@cmd ({
            $cmd.prompt($prompt)
        }) $($tail)*)
    }};
    (@cmd ($cmd:expr) -t $target_client:expr, $($tail:tt)*) => {{
        $crate::confirm_before!(@cmd ({
            $cmd.target_client($target_client)
        }) $($tail)*)
    }};
    (@cmd ($cmd:expr) $command:expr, $($tail:tt)*) => {{
        $crate::confirm_before!(@cmd ({
            $cmd.command($command)
        }) $($tail)*)
    }};
    //(@cmd ($cmd:expr) -$unknown:tt, $($tail:tt)*) => {{
        //::std::compile_error!("unknown flag, option or parameter: {}", $unknown);
    //}};
    (@cmd ($cmd:expr)) => {{
        $cmd
    }};
    () => {{
        $crate::ConfirmBefore::new()
    }};
    (($cmd:expr), $($tail:tt)*) => {{
        $crate::confirm_before!(@cmd ($cmd) $($tail)*,)
    }};
    ($($tail:tt)*) => {{
        $crate::confirm_before!(@cmd ({ $crate::ConfirmBefore::new() }) $($tail)*,)
    }};
}

#[test]
fn confirm_before_macro() {
    use std::borrow::Cow;

    // # Manual
    //
    // tmux ^1.5:
    // ```text
    // confirm-before [-p prompt] [-t target-client] command
    // (alias: confirm)
    // ```
    //
    // tmux ^0.9:
    // ```text
    // confirm-before [-t target-client] command
    // (alias: confirm)
    // ```
    let confirm_before = confirm_before!();
    #[cfg(feature = "tmux_1_5")]
    let confirm_before = confirm_before!((confirm_before), -p "1");
    #[cfg(feature = "tmux_0_9")]
    let confirm_before = confirm_before!((confirm_before), -t "2");
    #[cfg(feature = "tmux_0_9")]
    let confirm_before = confirm_before!((confirm_before), "3");

    #[cfg(not(feature = "cmd_alias"))]
    let cmd = "confirm-before";
    #[cfg(feature = "cmd_alias")]
    let cmd = "confirm";

    let mut s = Vec::new();
    s.push(cmd);
    #[cfg(feature = "tmux_1_5")]
    s.extend_from_slice(&["-p", "1"]);
    #[cfg(feature = "tmux_0_9")]
    s.extend_from_slice(&["-t", "2"]);
    #[cfg(feature = "tmux_0_9")]
    s.push("3");
    let s: Vec<Cow<str>> = s.into_iter().map(|a| a.into()).collect();

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

    assert_eq!(confirm_before, s);
}