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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/// Choose a specific layout for a window
///
/// # Manual
///
/// tmux ^2.7:
/// ```text
/// select-layout [-Enop] [-t target-pane] [layout-name]
/// (alias: selectl)
/// ```
///
/// tmux ^2.1:
/// ```text
/// select-layout [-nop] [-t target-pane] [layout-name]
/// (alias: selectl)
/// ```
///
/// tmux ^1.5:
/// ```text
/// select-layout [-np] [-t target-pane] [layout-name]
/// (alias: selectl)
/// ```
///
/// tmux ^1.0:
/// ```text
/// select-layout [-t target-pane] [layout-name]
/// (alias: selectl)
/// ```
///
/// tmux ^0.9:
/// ```text
/// select-layout [-t target-pane] layout-name
/// (alias: selectl)
/// ```
#[macro_export]
macro_rules! select_layout {
    // `[-E]` - spread the current pane and any panes next to it out evenly
    (@cmd ($cmd:expr) -E, $($tail:tt)*) => {{
        $crate::select_layout!(@cmd ({
            $cmd.spread()
        }) $($tail)*)
    }};
    // `[-n]` - next-layout equivalent
    (@cmd ($cmd:expr) -n, $($tail:tt)*) => {{
        $crate::select_layout!(@cmd ({
            $cmd.next_layout()
        }) $($tail)*)
    }};
    // `[-o]` - apply the last set layout if possible
    (@cmd ($cmd:expr) -o, $($tail:tt)*) => {{
        $crate::select_layout!(@cmd ({
            $cmd.last_layout()
        }) $($tail)*)
    }};
    // `[-p]` - previous-layout equivalent
    (@cmd ($cmd:expr) -p, $($tail:tt)*) => {{
        $crate::select_layout!(@cmd ({
            $cmd.previous_layout()
        }) $($tail)*)
    }};
    // `[-t target-pane]` - target-pane
    (@cmd ($cmd:expr) -t $target_pane:expr, $($tail:tt)*) => {{
        $crate::select_layout!(@cmd ({
            $cmd.target_pane($target_pane)
        }) $($tail)*)
    }};
    // `[layout-name]` - layout-name
    (@cmd ($cmd:expr) $layout_name:expr, $($tail:tt)*) => {{
        $crate::select_layout!(@cmd ({
            $cmd.layout_name($layout_name)
        }) $($tail)*)
    }};
    //(@cmd ($cmd:expr) -$unknown:tt, $($tail:tt)*) => {{
        //::std::compile_error!("unknown flag, option or parameter: {}", $unknown);
    //}};
    (@cmd ($cmd:expr)) => {{
        $cmd
    }};
    () => {{
        $crate::SelectLayout::new()
    }};
    (($cmd:expr), $($tail:tt)*) => {{
        $crate::select_layout!(@cmd ($cmd) $($tail)*,)
    }};
    ($($tail:tt)*) => {{
        $crate::select_layout!(@cmd ({ $crate::SelectLayout::new() }) $($tail)*,)
    }};
}

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

    // Choose a specific layout for a window
    //
    // # Manual
    //
    // tmux ^2.7:
    // ```text
    // select-layout [-Enop] [-t target-pane] [layout-name]
    // (alias: selectl)
    // ```
    //
    // tmux ^2.1:
    // ```text
    // select-layout [-nop] [-t target-pane] [layout-name]
    // (alias: selectl)
    // ```
    //
    // tmux ^1.5:
    // ```text
    // select-layout [-np] [-t target-pane] [layout-name]
    // (alias: selectl)
    // ```
    //
    // tmux ^1.0:
    // ```text
    // select-layout [-t target-pane] [layout-name]
    // (alias: selectl)
    // ```
    //
    // tmux ^0.9:
    // ```text
    // select-layout [-t target-pane] layout-name
    // (alias: selectl)
    // ```
    let target_pane = TargetPane::Raw("1").to_string();

    let select_layout = select_layout!();
    #[cfg(feature = "tmux_2_7")]
    let select_layout = select_layout!((select_layout), -E);
    #[cfg(feature = "tmux_1_5")]
    let select_layout = select_layout!((select_layout), -n);
    #[cfg(feature = "tmux_2_1")]
    let select_layout = select_layout!((select_layout), -o);
    #[cfg(feature = "tmux_1_5")]
    let select_layout = select_layout!((select_layout), -p);
    #[cfg(feature = "tmux_0_9")]
    let select_layout = select_layout!((select_layout), -t & target_pane);
    #[cfg(feature = "tmux_1_0")]
    let select_layout = select_layout!((select_layout), "2");

    #[cfg(not(feature = "cmd_alias"))]
    let cmd = "select-layout";
    #[cfg(feature = "cmd_alias")]
    let cmd = "selectl";

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

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

    assert_eq!(select_layout, s);
}