tmux_interface/commands/windows_and_panes/
respawn_pane_macro.rs

1/// Reactivate a pane in which the command has exited
2///
3/// # Manual
4///
5/// tmux ^3.0:
6/// ```text
7/// respawn-pane [-k] [-c start-directory] [-e environment] [-t target-pane] [shell-command]
8/// (alias: respawnp)
9/// ```
10///
11/// tmux ^2.6:
12/// ```text
13/// respawn-pane [-k] [-c start-directory] [-t target-pane] [shell-command]
14/// (alias: respawnp)
15/// ```
16///
17/// tmux ^1.5:
18/// ```text
19/// respawn-pane [-k] [-t target-pane] [shell-command]
20/// (alias: respawnp)
21/// ```
22#[macro_export]
23macro_rules! respawn_pane {
24    // `[-k]` - any existing command is killed
25    (@cmd ($cmd:expr) -k, $($tail:tt)*) => {{
26        $crate::respawn_pane!(@cmd ({
27            $cmd.kill()
28        }) $($tail)*)
29    }};
30    // `[-c start-directory]` - start-directory
31    (@cmd ($cmd:expr) -c $start_directory:expr, $($tail:tt)*) => {{
32        $crate::respawn_pane!(@cmd ({
33            $cmd.start_directory($start_directory)
34        }) $($tail)*)
35    }};
36    // `[-e environment]` - environment
37    (@cmd ($cmd:expr) -e $environment:expr, $($tail:tt)*) => {{
38        $crate::respawn_pane!(@cmd ({
39            $cmd.environment($environment)
40        }) $($tail)*)
41    }};
42    // `[-t target-pane]` - target-pane
43    (@cmd ($cmd:expr) -t $target_pane:expr, $($tail:tt)*) => {{
44        $crate::respawn_pane!(@cmd ({
45            $cmd.target_pane($target_pane)
46        }) $($tail)*)
47    }};
48    // `[shell-command]` - shell-command
49    (@cmd ($cmd:expr) $shell_command:expr, $($tail:tt)*) => {{
50        $crate::respawn_pane!(@cmd ({
51            $cmd.shell_command($shell_command)
52        }) $($tail)*)
53    }};
54    //(@cmd ($cmd:expr) -$unknown:tt, $($tail:tt)*) => {{
55        //::std::compile_error!("unknown flag, option or parameter: {}", $unknown);
56    //}};
57    (@cmd ($cmd:expr)) => {{
58        $cmd
59    }};
60    () => {{
61        $crate::RespawnPane::new()
62    }};
63    (($cmd:expr), $($tail:tt)*) => {{
64        $crate::respawn_pane!(@cmd ($cmd) $($tail)*,)
65    }};
66    ($($tail:tt)*) => {{
67        $crate::respawn_pane!(@cmd ({ $crate::RespawnPane::new() }) $($tail)*,)
68    }};
69}
70
71#[test]
72fn respawn_pane_macro() {
73    use crate::TargetPane;
74    use std::borrow::Cow;
75
76    // Reactivate a pane in which the command has exited
77    //
78    // # Manual
79    //
80    // tmux ^3.0:
81    // ```text
82    // respawn-pane [-k] [-c start-directory] [-e environment] [-t target-pane] [shell-command]
83    // (alias: respawnp)
84    // ```
85    //
86    // tmux ^2.6:
87    // ```text
88    // respawn-pane [-k] [-c start-directory] [-t target-pane] [shell-command]
89    // (alias: respawnp)
90    // ```
91    //
92    // tmux ^1.5:
93    // ```text
94    // respawn-pane [-k] [-t target-pane] [shell-command]
95    // (alias: respawnp)
96    // ```
97    let target_pane = TargetPane::Raw("3").to_string();
98
99    let respawn_pane = respawn_pane!();
100    #[cfg(feature = "tmux_1_5")]
101    let respawn_pane = respawn_pane!((respawn_pane), -k);
102    #[cfg(feature = "tmux_2_6")]
103    let respawn_pane = respawn_pane!((respawn_pane), -c "1");
104    #[cfg(feature = "tmux_3_0")]
105    let respawn_pane = respawn_pane!((respawn_pane), -e "2");
106    #[cfg(feature = "tmux_1_5")]
107    let respawn_pane = respawn_pane!((respawn_pane), -t & target_pane);
108    #[cfg(feature = "tmux_2_6")]
109    let respawn_pane = respawn_pane!((respawn_pane), "4");
110
111    #[cfg(not(feature = "cmd_alias"))]
112    let cmd = "respawn-pane";
113    #[cfg(feature = "cmd_alias")]
114    let cmd = "respawnp";
115
116    let mut s = Vec::new();
117    s.push(cmd);
118    #[cfg(feature = "tmux_1_5")]
119    s.push("-k");
120    #[cfg(feature = "tmux_2_6")]
121    s.extend_from_slice(&["-c", "1"]);
122    #[cfg(feature = "tmux_3_0")]
123    s.extend_from_slice(&["-e", "2"]);
124    #[cfg(feature = "tmux_1_5")]
125    s.extend_from_slice(&["-t", "3"]);
126    #[cfg(feature = "tmux_2_6")]
127    s.push("4");
128    let s: Vec<Cow<str>> = s.into_iter().map(|a| a.into()).collect();
129
130    let respawn_pane = respawn_pane.build().to_vec();
131
132    assert_eq!(respawn_pane, s);
133}