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
92
93
94
95
96
97
98
99
/// Remove and free the history for the specified pane.
///
/// # Manual
///
/// tmux ^1.0:
/// ```text
/// clear-history [-t target-pane]
/// (alias: clearhist)
/// ```
///
/// tmux ^0.9:
/// ```text
/// clear-history [-p pane-index] [-t target-window]
/// (alias: clearhist)
/// ```
#[macro_export]
macro_rules! clear_history {
    (@cmd ($cmd:expr) -t $target_pane:expr, $($tail:tt)*) => {{
        $crate::clear_history!(@cmd ({
            $cmd.target_pane($target_pane)
        }) $($tail)*)
    }};
    (@cmd ($cmd:expr) -t $target_window:expr, $($tail:tt)*) => {{
        $crate::clear_history!(@cmd ({
            $cmd.target_window($target_window)
        }) $($tail)*)
    }};
    (@cmd ($cmd:expr) -p $pane_index:expr, $($tail:tt)*) => {{
        $crate::clear_history!(@cmd ({
            $cmd.pane_index($pane_index)
        }) $($tail)*)
    }};
    //(@cmd ($cmd:expr) -$unknown:tt, $($tail:tt)*) => {{
        //::std::compile_error!("unknown flag, option or parameter: {}", $unknown);
    //}};
    (@cmd ($cmd:expr)) => {{
        $cmd
    }};
    () => {{
        $crate::ClearHistory::new()
    }};
    (($cmd:expr), $($tail:tt)*) => {{
        $crate::clear_history!(@cmd ($cmd) $($tail)*,)
    }};
    ($($tail:tt)*) => {{
        $crate::clear_history!(@cmd ({ $crate::ClearHistory::new() }) $($tail)*,)
    }};
}

#[test]
fn clear_history_macro() {
    use crate::TargetPane;
    use std::borrow::Cow;

    // Remove and free the history for the specified pane.
    //
    // # Manual
    //
    // tmux ^1.0:
    // ```text
    // clear-history [-t target-pane]
    // (alias: clearhist)
    // ```
    //
    // tmux ^0.9:
    // ```text
    // clear-history [-p pane-index] [-t target-window]
    // (alias: clearhist)
    // ```
    let target_pane = TargetPane::Raw("1").to_string();

    let clear_history = clear_history!();
    #[cfg(feature = "tmux_1_0")]
    let clear_history = clear_history!((clear_history), -t target_pane);
    #[cfg(all(feature = "tmux_0_9", not(feature = "tmux_1_0")))]
    let clear_history = clear_history!((clear_history), -p target_pane);
    #[cfg(all(feature = "tmux_0_9", not(feature = "tmux_1_0")))]
    let clear_history = clear_history!((clear_history), -p target_pane);

    #[cfg(not(feature = "cmd_alias"))]
    let cmd = "clear-history";
    #[cfg(feature = "cmd_alias")]
    let cmd = "clearhist";

    let mut s = Vec::new();
    s.push(cmd);

    #[cfg(feature = "tmux_1_0")]
    s.extend_from_slice(&["-t", "1"]);
    #[cfg(all(feature = "tmux_0_9", not(feature = "tmux_1_0")))]
    s.extend_from_slice(&["-p", "1"]);
    #[cfg(all(feature = "tmux_0_9", not(feature = "tmux_1_0")))]
    s.extend_from_slice(&["-t", "1"]);
    let s: Vec<Cow<str>> = s.into_iter().map(|a| a.into()).collect();

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

    assert_eq!(clear_history, s);
}